CS212: Object Oriented Analysis and Design
Lecture 39: Design Pattern-III
Recap of Lecture 38
• Abstract Factory
• Singleton
• Adapter
Outline of Lecture 39
• Composite Pattern
• Iterator Pattern
• Visitor Pattern
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
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
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.
• …
Composite: Example• 2 + 3
• (2 + 3) + (4 * 6)
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
Mar-15
Catalogue of Design Patterns (GoF)
Purpose
Creational Structural Behavioral Class Factory Method Adapter Interpreter
Template Method
Scop
e
Object Abstract Factory Builder Prototype Singleton
Adapter Bridge Composite Decorator Facade Proxy
Chain of Responsibility Command Iterator Mediator Memento Flyweight Observer State Strategy Visitor
Software Engineering
Iterator: Intent
• Access the elements of an aggregate object sequentially
• Promote to "full object status" the traversal of a collection.
• Polymorphic traversal
Iterator: Problem
• Need to "abstract" the traversal of wildly different data structures
• Algorithms can be defined that are capable of interfacing with each transparently.
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
Iterator: Structure
Iterator: Example
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.
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
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.
Visitor: Discussion
• Encourages designing lightweight element classes
Visitor implements "double dispatch".
Visitor: Structure
Visitor: Example
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.
Thank youNext Lecture: Design Pattern