Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.

Similar presentations


Presentation on theme: "CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III."— Presentation transcript:

1 CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III

2 Recap of Lecture 38 Abstract Factory Singleton Adapter

3 Outline of Lecture 39 Composite Pattern Iterator Pattern Visitor Pattern

4 Composite: Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly 1-to-many "has a" up the "is a" hierarchy

5 Composite: Problem Application needs to manipulate a hierarchical collection "primitive" and "composite" objects Primitive object is handled one way Composite object is handled differently Composites that contain components, each of which could be a composite

6 Composite: Structure Example Menus that contain menu items, each of which could be a menu. Row-column GUI layout managers that contain widgets, each of which could be a row-column GUI layout manager. Directories that contain files, each of which could be a directory. Containers that contain Elements, each of which could be a Container. …

7 Composite: Example 2 + 3 (2 + 3) + (4 * 6)

8 Composite: Check list Problem is about representing "whole-part“ Divide your domain concepts into container classes, and containee classes All container and containee classes declare an "is a" relationship to the interface All container classes declare a one-to-many "has a" relationship to the interface. Child management methods

9 Mar-15 Catalogue of Design Patterns (GoF) Software Engineering

10 Iterator: Intent Access the elements of an aggregate object sequentially Promote to "full object status" the traversal of a collection. Polymorphic traversal

11 Iterator: Problem Need to "abstract" the traversal of wildly different data structures Algorithms can be defined that are capable of interfacing with each transparently.

12 Iterator: Discussion An aggregate object such as a list should give you a way to access its elements without exposing its internal structure Traverse the list in different ways, depending on what you need to accomplish You might also need to have more than one traversal pending on the same list

13 Iterator: Structure

14 Iterator: Example

15 Iterator: Check List Add a create_iterator() method to the "collection" class, and grant the "iterator" class privileged access. Design an "iterator" class that can encapsulate traversal of the "collection" class. Clients ask the collection object to create an iterator object. Clients use the first(), is_done(), next(), and current_item() protocol to access the elements of the collection class.

16 Visitor: Intent Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates. The classic technique for recovering lost type information. Double dispatch

17 Visitor: Problem Visitor pattern allows to separate the data structures and the algorithms to be applied on the data. Both data structure objects and algorithm objects can evolve separately. Data structure (element) objects have an "accept" method which take in a visitor (algorithmic) object. Algorithmic objects have a "visit" method which take in a data structure object.

18 Visitor: Discussion Encourages designing lightweight element classes Visitor implements "double dispatch".

19 Visitor: Structure

20 Visitor: Example

21 Visitor: Check list Confirm that the current hierarchy Create a Visitor base class with a visit(ElementXxx) method for each Element derived type. Add an accept(Visitor) method to the Element hierarchy Create a Visitor derived class for each "operation" to be performed on Element objects. The client creates Visitor objects and passes each to Element objects by calling accept.

22 Thank you Next Lecture: Design Pattern


Download ppt "CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III."

Similar presentations


Ads by Google