cs212: object oriented analysis and design lecture 39: design pattern-iii

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

Upload: gloria-shaw

Post on 18-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

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

CS212: Object Oriented Analysis and Design

Lecture 39: Design Pattern-III

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

Recap of Lecture 38

• Abstract Factory

• Singleton

• Adapter

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

Outline of Lecture 39

• Composite Pattern

• Iterator Pattern

• Visitor Pattern

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

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

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

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

Composite: Example• 2 + 3

• (2 + 3) + (4 * 6)

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

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

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

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

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

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

Iterator: Structure

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

Iterator: Example

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

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

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

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

Visitor: Discussion

• Encourages designing lightweight element classes

Visitor implements "double dispatch".

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

Visitor: Structure

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

Visitor: Example

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

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

Thank youNext Lecture: Design Pattern