ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.

Slides:



Advertisements
Similar presentations
Behavioral Pattern: Visitor C h a p t e r 5 – P a g e 224 When an algorithm is separated from an object structure upon which it operates, new operations.
Advertisements

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Composite Design Pattern (1) –A structural design pattern.
INTERPRETER Main Topics What is an Interpreter. Why should we learn about them.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Design Patterns CS is not simply about programming
Visitor Pattern Jeff Schott CS590L Spring What is the Purpose of the Visitor Pattern ? n Represent an operation to be performed on the elements.
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
1 PH Chapter 1 (pp. 1-10) GoF Composite Pattern (pp ) PH Ch 2 through Fundamentals (pp ) Presentation by Julie Betlach 5/28/2009.
Composite Design Pattern. Motivation – Dynamic Structure.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Design Patterns.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
© Spiros Mancoridis 27/09/ Software Design Topics in Object-Oriented Design Patterns Material drawn from [Gamma95] and [Coplien95] Revised and augmented.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Design Patterns – II Lecture IV. Singleton Pattern Intent – Ensure a class only has one instance, and provide a global point of access to it Motivation.
GoF: Document Editor Example Rebecca Miller-Webster.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
....and other creepy things from John Vlissides The Visitor Pattern EXISTS! And its INTENT is to represent an operation to be performed on the elements.
Structural Design Patterns
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IV Structural Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
08 - StructuralCSC4071 Structural Patterns concerned with how classes and objects are composed to form larger structures –Adapter interface converter Bridge.
Structural Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
OO Methodology Elaboration Iteration 2 - Design Patterns -
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
Design Patterns Introduction
CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
COMPOSITE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Reference – Object Oriented Software Development Using Java - Jia COP 3331 Object Oriented Analysis and Design Chapter 10 – Patterns Jean Muhammad.
CSE 332: Design Patterns (Part II) Last Time: Part I, Familiar Design Patterns We’ve looked at patterns related to course material –Singleton: share a.
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Composite Pattern Himanshu Gupta Shashank Hegde CSE776 – Design Patterns Fall 2011 Good composition is like a suspension bridge - each line adds strength.
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
Introduction to Design Patterns
object oriented Principles of software design
Iterator and Composite Design Patterns
Object-Oriented Design
Design Patterns - A few examples
Jim Fawcett CSE776 – Design Patterns Summer 2003
Informatics 122 Software Design II
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Informatics 122 Software Design II
Visitor Pattern Intent
Iterator Design Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014
Presentation transcript:

ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns

ECE450 - Software Engineering II2 Think of drawing/diagramming editors Drawing/diagramming editors let users build complex diagrams out of simple components –The user can group components to form larger components... –...which in turn can be grouped to form still larger components

ECE450 - Software Engineering II3 The problem Although we grouped the shapes, the system should not treat the result as a “primitive” shape –Even if most of the time we treat them identically –We may decide to ungroup them Or to modify an attribute of one of the components of the grouped shape The Composite pattern describes how to use recursive composition so that clients don’t have to make this distinction.

ECE450 - Software Engineering II4 The Composite pattern Composite: Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly –The key to the pattern is the abstract class that represents both primitives and their containers.

ECE450 - Software Engineering II5 Structure and participants Component –Declares the interface for objects in the composition –Implements default behavior for the interface common to all classes –Declares an interface for accessing and managing its child components –(Optionally) defines an interface for accessing a component’s parent in the structure, and implements it if appropriate Leaf –Represents leaf objects in the composition. A leaf has no children –Defines behavior for primitive objects in the composition

ECE450 - Software Engineering II6 Structure and participants (cont) Composite –Defines behavior for components having children –Stores child components –Implements child-related operations in the Component interface Client –Manipulates objects in the composition through the Component interface

ECE450 - Software Engineering II7 Applicability Use the Composite pattern when... –You want to represent part-whole hierarchies of objects –You want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly A second example: file systems –Directories contain entries, each of which could be a directory

ECE450 - Software Engineering II8 Consequences The pattern defines class hierarchies consisting of primitive objects and composite objects –Primitive objects can be composed into more complex objects, which in turn can be composed, and so on recursively. –Wherever client code expects a primitive object, it can also take a composite object Makes the client simple –Clients can treat composite structures and individual objects uniformly. –Clients don’t know (and shouldn’t care) whether they’re dealing with a leaf or a composite component But can make your design overly general –The disadvantage of making it easy to add new components is that it makes it harder to restrict the components of a composite Sometimes you want only some kinds of components, but you’ll have to use run-time checks to guarantee your constraints.

ECE450 - Software Engineering II9 Implementation Explicit parent references? –You may want to maintain references from child components to their parent to simplify traversal and management of a composite structure –The usual place to define the parent reference is in the Component class Declaring the child management operations? –Our diagram has the Component abstract class declare the Add and Remove operations for managing children, but leaf nodes never use them –Is it better to have those operations declared in the Composite class? –Tradeoff between safety and transparency: Defining the child management interface at the root gives you transparency (you can treat all components uniformly) but costs you safety (clients may try to do meaningless things like add and remove objects from leaves!) Defining child management in the Composite class gives you safety (any attempt to add or remove objects from leaves will be caught at compile-time), but you lose transparency (leaves and composites have different interfaces). What is the best data structure for storing components? –Depends on efficiency. It is possible that you don’t even need a general-purpose data structure at all!

ECE450 - Software Engineering II10 That was it for structurals... Now we need to tackle behaviorals We’ve gone through the creational patterns... –Factory method –Abstract factory –Builder –Prototype –Singleton... the structural ones... –Decorator –Adapter –Facade –Proxy –Flyweight –Bridge...and two behaviorals –Template method –Null object A (short) review is in order...

ECE450 - Software Engineering II11 Behavioral patterns Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects –They describe not just patterns of objects or classes, but also the patterns of communication between them

ECE450 - Software Engineering II12 We’ll start with a simple one... Assume you have an aggregate object such as a list This aggregate object should give you a way to access its elements without exposing its internal structure. –And you may want to traverse the list in different ways However, adding the operations for different traversals to the List interface would bloat it –The List is there for a purpose different than supporting our traversals And remember classes should do just one thing, if possible (maximizing cohesion) –Not only that, but you may want to traverse it more than once at the same time How can we address this problem?

ECE450 - Software Engineering II13 The Iterator pattern Iterator: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation –You probably knew of this one already Java’s Iterator interface is an application of this principle

ECE450 - Software Engineering II14 Structure and participants Iterator –Defines an interface for accessing and traversing elements ConcreteIterator –Implements the Iterator interface –Keeps track of the current position in the traversal of the aggregate Aggregate –Defines an interface for creating an Iterator object ConcreteAggregate –Implements the Iterator creation interface to return an instance of the proper ConcreteIterator

ECE450 - Software Engineering II15 Applicability Use the Iterator pattern... –To access an aggregate object’s contents without exposing its internal representation –To support multiple traversals of aggregate objects –To provide a uniform interface for traversing different aggregate structures That is, to support polymorphic iteration

ECE450 - Software Engineering II16 Consequences Iterators support variations in the traversal of an aggregate –Complex aggregates may be traversed in many ways. Iterator subclasses (or Iterator instances) allow clients to traverse the aggregate in the way they desire More than one traversal can be pending on an aggregate –An iterator keeps track of its own traversal state. Therefore, you can have more than one traversal in progress at once

ECE450 - Software Engineering II17 Implementation Who controls the iteration? –When the client controls the iteration, the iterator is called an external iterator Clients must advance the traversal and request the next element explicitly –When the iterator controls it, the iterator is an internal iterator Clients hand an internal iterator an operation to perform, and the iterator applies that operation to every element in the aggregate Who defines the traversal algorithm? –It could be defined in the iterator (making it easy to use different iteration algorithms in the same aggregate) or in the aggregate (probably necessary if we need to access private variables of the aggregate) Iterators for Composites –External iterators can be difficult to implement over recursive aggregate structures like Composites They need to store a path through the Composite to keep track of the current object Null iterator –A null iterator always “is done” with the traversal –They can make traversing tree-structured aggregates (like Composites) easier Aggregate elements return a concrete iterator as usual But leaf elements return an instance of NullIterator

ECE450 - Software Engineering II18 Operations on a compiler Consider a compiler that represents programs as abstract syntax trees –Every node is a token (a variable, an assignment, etc.) –The compiler needs to perform operations on the trees for various kinds of analyses Type-checking Code optimization Flow analysis... –Most of these operations will need to treat nodes that represent assignment statements differently from nodes that represent variables or arithmetic expressions There will be one class for assignment statements, another for variable accesses, etc. –Distributing all our operations across the various node classes leads to a system that is hard to understand, maintain, and change It will be confusing to have type-checking code mixed with pretty-printing code –It would be better if each new operation could be added separately, and the node classes were independent on the operations that apply to them

ECE450 - Software Engineering II19 The Visitor pattern 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

ECE450 - Software Engineering II20 Participants and structure Visitor –Declares a Visit operation for each class of ConcreteElement in the object structure –The operation’s name and signature identifies the class that sends the Visit request to the visitor, which lets the visitor determine the concrete class of the element being visited ConcreteVisitor –Implements each operation declared by Visitor –Each operation implements a fragment of the algorithm defined for the corresponding class of object in the structure

ECE450 - Software Engineering II21 Participants and structure Element –Defines an Accept operation that takes a visitor as an argument ConcreteElement –Implements an Accept operation that takes a visitor as an argument ObjectStructure –Can enumerate its elements –May provide a high-level interface to allow the visitor to visit its elements –May either be a Composite or a collection such as a list or set

ECE450 - Software Engineering II22 Collaborations A client that uses the Visitor pattern must create a ConcreteVisitor object and then traverse the object structure, visiting each element with the visitor When the element is visited, it calls the Visitor operation that corresponds to its class. The element supplies itself as an argument to this operation to let the visitor access its state, if necessary

ECE450 - Software Engineering II23 Applicability Use the Visitor pattern when... –An object structure contains many classes of objects with differing interfaces, and you want to perform operations on these objects that depend on their concrete classes –Many distinct and unrelated operations need to be performed on objects in an object structure, and you want to avoid “polluting” their classes with these operations –The classes defining the object structure rarely change, but you often want to define new operations over the structure

ECE450 - Software Engineering II24 Consequences Visitor makes adding new operations easy –You can define a new operation simply by adding a new visitor –In contrast, if you spread functionality over many classes, then you must change each class to define a new operation A visitor gathers related operations and separates unrelated ones –Related behavior isn’t spread over the classes defining the object structure; it’s localized in a visitor –Unrelated sets of behavior are partitioned in their own visitor classes Adding new ConcreteElement classes is hard –The Visitor pattern makes it hard to add new subclasses of Element –Each new ConcreteElement gives rise to a new abstract operation on Visitor and a corresponding implementation in every ConcreteVisitor class! Accumulating state –Visitors can accumulate state as they visit each element Breaking encapsulation –Visitor’s approach assumes that the ConcreteElement interface is powerful enough to let visitors do their job –The pattern often forces you to provide public operations that access an element’s internal state, which may compromise its encapsulation

ECE450 - Software Engineering II25 Implementation Who is responsible for traversing the object structure? –A visitor must visit each element of the object structure. How does it get there? –We can put responsibility for traversal in any of three places: In the object structure, in the visitor, or in a separate Iterator object Having the traversal code in the visitor is the least preferred option, as it forces you to repeat the code in every ConcreteVisitor for each ConcreteElement; so you would only follow this approach for a particularly complex traversal (e.g. one that depends on the results of the operations on the object structure)