CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.
CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.
-
Upload
gloria-shaw -
Category
Documents
-
view
213 -
download
0
Transcript of CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.
![Page 1: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/1.jpg)
CS212: Object Oriented Analysis and Design
Lecture 39: Design Pattern-III
![Page 2: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/2.jpg)
Recap of Lecture 38
• Abstract Factory
• Singleton
• Adapter
![Page 3: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/3.jpg)
Outline of Lecture 39
• Composite Pattern
• Iterator Pattern
• Visitor Pattern
![Page 4: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/4.jpg)
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
![Page 5: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/5.jpg)
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
![Page 6: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/6.jpg)
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.
• …
![Page 7: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/7.jpg)
Composite: Example• 2 + 3
• (2 + 3) + (4 * 6)
![Page 8: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/8.jpg)
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
![Page 9: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/9.jpg)
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
![Page 10: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/10.jpg)
Iterator: Intent
• Access the elements of an aggregate object sequentially
• Promote to "full object status" the traversal of a collection.
• Polymorphic traversal
![Page 11: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/11.jpg)
Iterator: Problem
• Need to "abstract" the traversal of wildly different data structures
• Algorithms can be defined that are capable of interfacing with each transparently.
![Page 12: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/12.jpg)
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
![Page 13: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/13.jpg)
Iterator: Structure
![Page 14: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/14.jpg)
Iterator: Example
![Page 15: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/15.jpg)
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.
![Page 16: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/16.jpg)
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
![Page 17: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/17.jpg)
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.
![Page 18: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/18.jpg)
Visitor: Discussion
• Encourages designing lightweight element classes
Visitor implements "double dispatch".
![Page 19: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/19.jpg)
Visitor: Structure
![Page 20: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/20.jpg)
Visitor: Example
![Page 21: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/21.jpg)
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.
![Page 22: CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.](https://reader035.fdocuments.net/reader035/viewer/2022062805/5697bff41a28abf838cbcbf6/html5/thumbnails/22.jpg)
Thank youNext Lecture: Design Pattern