Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-1 PS95&96-MEF-L15-1 Dr. M.E. Fayad Creationa l.

Slides:



Advertisements
Similar presentations
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-1 PS95&96-MEF-L11-1 Dr. M.E. Fayad Creationa l Paradigm.
Advertisements

Behavioral Design Patterns May 5, 2015May 5, 2015May 5, 2015.
GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-1 PS95&96-MEF-L13-1 Dr. M.E. Fayad Creationa l Paradigm.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Design Patterns In OPM Presented by: Galia Shlezinger Instructors: Prop. Dov Dori, Dr. Iris Berger.
Software Design & Documentation – Design Pattern: Command Design Pattern: Command Christopher Lacey September 15, 2003.
Dept. of Computer Engineering, Amirkabir University of Tech. 1 Design Patterns Dr. Noorhosseini Introduction.
Design Patterns CS is not simply about programming
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Copyright © Active Frameworks, Inc.. - All Rights Reserved - V2.0 Introduction - Page L1-1 PS95&96-MEF-L1-1 Dr. M.E. Fayad Creationa l Paradigm.
Copyright © Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-1 PS95&96-MEF-L16-1 Dr. M.E. Fayad Creationa l.
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
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 ( ) ( ) ( )
GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Document Editor - Page L2-5 PS95&96-MEF-L9-5 Dr. M.E. Fayad  Document Structure.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
C++ fundamentals.
More OOP Design Patterns
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain.
Programming With Java ICS201 University Of Hail1 Chapter 12 UML and Patterns.
Object Oriented Software Development
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
The Design Discipline.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Lexi case study (Part 2) Presentation by Matt Deckard.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
ECE450S – Software Engineering II
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Behavioral Pattern: Iterator C h a p t e r 5 – P a g e 159 Software can become difficult to manage when a variety of different traversals of a variety.
DESIGN PATTERNS -BEHAVIORAL PATTERNS WATTANAPON G SUTTAPAK Software Engineering, School of Information Communication Technology, University of PHAYAO 1.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Design Pattern Catalog - Page L3-1 PS95&96-MEF-L10-1 Dr. M.E. Fayad Creationa.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Design Patterns Introduction
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Reference – Object Oriented Software Development Using Java - Jia COP 3331 Object Oriented Analysis and Design Chapter 10 – Patterns Jean Muhammad.
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
Design Patterns: Behavioral Design Patterns General and reusable solutions to common problems in software design Software University
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Review of last class. Design patterns CreationalStructuralBehavioral Abstract Factory Builder Factory Singleton etc. Adapter Bridge Composite Decorator.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Design Patterns: MORE Examples
Chapter 10 Design Patterns.
Structural Patterns Structural patterns control the relationships between large portions of your applications. Structural patterns affect applications.
MPCS – Advanced java Programming
Introduction to Design Patterns
Behavioral Design Patterns
Programming Design Patterns
Chapter 9 Behavioral Design Patterns
Behavioral Patterns Part-I introduction UNIT-VI
PH Chapter 3 Thanks for the Memory Leaks Pushme-Pullyu (pp
The iterator and memento patterns
Lesson 5: More on Creational Patterns
Software Design Lecture : 39.
Presentation transcript:

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-1 PS95&96-MEF-L15-1 Dr. M.E. Fayad Creationa l Paradigm Shift, Inc. Software Factory Behaviora l Structural Lesson 8: Behavioral Patterns Object-Oriented Design Patterns

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-2 PS95&96-MEF-L15-2 Dr. M.E. Fayad Lesson Objectives oDiscuss the philosophy behind behavioral patterns oExplore the world of behavioral patterns oPresent the following Patterns: Command Iterator

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-3 PS95&96-MEF-L15-3 Dr. M.E. Fayad Behavioral Patterns Behavioral patterns characterize the ways in which classes and objects interact and distribute responsibilities Behavioral class patterns capture how classes cooperate with their subclasses to fulfill their semantics –Example: Template Method pattern Behavioral object patterns describe how a group of peer objects cooperate to perform a task that no single object can carry out by itself –Examples: Mediator, Observer, Command, and Strategy Behavioral compound patterns deal with behavior in recursive object structures –Examples: Iterator

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-4 PS95&96-MEF-L15-4 Dr. M.E. Fayad Behavioral Patterns  Command Pattern Topics –Command Definition –Structure –Menu-Command Example –Problems it Solves –Participants & Collaborations –Benefits –Related Patterns

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-5 PS95&96-MEF-L15-5 Dr. M.E. Fayad Command Pattern: Definition Define an object that encapsulates a request. This is useful for: –Configuring objects with different requests –Implementing undoable operations –Queuing or logging requests The requestor should not be required to: –Have specific knowledge of the request –Handle any dependencies that may be required to execute the request –May or may not specify the target of the request Command pattern is also known as Action or Transaction

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-6 PS95&96-MEF-L15-6 Dr. M.E. Fayad Command Pattern: Structure target->Action(); Target Action() ConcreteCommand Execute() Command Execute() Client Invoker state target

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-7 PS95&96-MEF-L15-7 Dr. M.E. Fayad Menu Example command->Execute(); Menu Add(MenuItem) Document Open() Close() Cut() Copy() Paste() Command Execute() command MenuItem Clicked() Application Add(Document) Document(Current) Manus can be implemented easily with Command objects Each choice in a Menu is an instance of a MenuItem class An Application class creates these menus and their menu items along with the rest of the user interface The Application class also keeps track of Document objects that a user has opened Manus can be implemented easily with Command objects Each choice in a Menu is an instance of a MenuItem class An Application class creates these menus and their menu items along with the rest of the user interface The Application class also keeps track of Document objects that a user has opened

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-8 PS95&96-MEF-L15-8 Dr. M.E. Fayad More Menu Example document->Paste(); PasteCommand Execute() Document Open() Close() Cut() Copy() Paste() Command Execute() document Command subclasses store the target of the request and invoke one or more specific requests on the target. For example, PasteCommand supports pasting text from the clipboard into a Document PasteCommand’s target is the Document object it is supplied upon instantiation The execute operation invokes Paste on the target Document Command subclasses store the target of the request and invoke one or more specific requests on the target. For example, PasteCommand supports pasting text from the clipboard into a Document PasteCommand’s target is the Document object it is supplied upon instantiation The execute operation invokes Paste on the target Document

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-9 PS95&96-MEF-L15-9 Dr. M.E. Fayad More Menu Example name = AskUse() doc = new Document(name) application->Add(doc) doc->Open() OpenCommand Execute() AskUse() Command Execute() application OpenCommand’s Execute operation: Prompts the user for document name Creates a corresponding Document’s object Adds the document to the target application Opens the document OpenCommand’s Execute operation: Prompts the user for document name Creates a corresponding Document’s object Adds the document to the target application Opens the document Application Add(Document) Document(Current)

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-10 PS95&96-MEF-L15-10 Dr. M.E. Fayad More Menu Example for all c in commands c->Execute(); MacroCommand Execute() Command Execute() commands MacroCommand is a concrete command subclass that simply executes a sequence of commands MacroCommand has no explicit target, because the commands it sequences define their own target MacroCommand is a concrete command subclass that simply executes a sequence of commands MacroCommand has no explicit target, because the commands it sequences define their own target

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-11 PS95&96-MEF-L15-11 Dr. M.E. Fayad Command Pattern: When & Where to Use Use when it is desirable to decouple the client from the target When flexibility and adaptability are required on a frequent basis

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-12 PS95&96-MEF-L15-12 Dr. M.E. Fayad Command Functions These can be implemented as a single operation on a target Multiple operations on a target (similar to a MacroCommand) No operations on the target, the function is contained within the command

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-13 PS95&96-MEF-L15-13 Dr. M.E. Fayad Command Pattern: Extensibility Adding a new Command type: –Initialize a menu item - simply another instance with a different label –Create another concrete Command class for the new Command type Adding a new target type: –Extend each Command to provide the function for the new target. In this case, the target type should be supplied to the Command OR –Create another set of concrete Command classes specific to the target type How to Expand:1. Macro Commands 2. History Files, such as Trace 3. Undo which requires the saving of state How to Expand:1. Macro Commands 2. History Files, such as Trace 3. Undo which requires the saving of state

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-14 PS95&96-MEF-L15-14 Dr. M.E. Fayad Command Pattern: Participants Command –an interface for executing an operation (Do, Execute) ConcreteCommand (PasteCommand, OpenCommand) –knows its target –implements Execute by invoking the corresponding operation or operations on target Client (Application) –creates a ConcreteCommand, sets its target Invoker (MenuItem) –requests the Command to execute its operation Target (Document, Application) –knows how to carry out the operations requested from commands. Any class may serve as a Target

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-15 PS95&96-MEF-L15-15 Dr. M.E. Fayad Command Pattern: Collaborations The Client creates a ConcreteCommand and specifies its target The ConcreteCommand is stored by an invoker The invoker asks the command to perform its duties by calling Execute The ConcreteCommand invokes operations on its target to carry out the request

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-16 PS95&96-MEF-L15-16 Dr. M.E. Fayad Command Pattern: Interaction Diagram Target Invoker CommandClient Command() Execute Action() AdoptCommand()

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-17 PS95&96-MEF-L15-17 Dr. M.E. Fayad Command Pattern: Benefits Command decouples the object that invokes the operation from the one that actually has the knowledge to perform it Commands are first-class objects Commands can be assembled into composite Commands Command enforces an architecture that can be extended easily with new kinds of commands

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-18 PS95&96-MEF-L15-18 Dr. M.E. Fayad Command Pattern: Related Patterns Composite can be used to implement MacroCommands A Memento can keep state the Command requires to undo its effect A Command that must be copied before being placed on the history list acts as a prototype

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-19 PS95&96-MEF-L15-19 Dr. M.E. Fayad Behavioral Patterns  Iterator Pattern Topics –Iterator Definition –Structure –Problems it Solves –Participants & Collaborations –Solutions & Benefits –Implementation Issues –Related Patterns

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-20 PS95&96-MEF-L15-20 Dr. M.E. Fayad Iterator Pattern: Definition Iterators provide a standard interface for enumerating the components of a collection (Lists, Bags, Sets,...) or a composite object without exposing the object’s internal structure This “hides the gory details” of the implementation of the collection from the user of the collection Iterators are also known as Cursors or Streams There can be more than one iterator traversing over a collection at any time Each iterator maintains an index into the collection Each object is responsible for returning its own iterator

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-21 PS95&96-MEF-L15-21 Dr. M.E. Fayad Iteration: Basic Idea Initialize iteration for (first element; iteration not over; next element) Process element

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-22 PS95&96-MEF-L15-22 Dr. M.E. Fayad Iterator Pattern: When to Use It To provide a uniform interface for iterating over different structures To avoid exposing internal representation and implementation of the structure To provide multiple traversal strategies for aggregate object structures, such as collections or composite objects To simplify maintenance and extensibility when traversal algorithms are distributed across multiple classes

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-23 PS95&96-MEF-L15-23 Dr. M.E. Fayad Iterator Pattern: Structure return new ConcreteIterator ConcreteIterator Iterator First() Next() IsDone() CurrentItem() ConcreteObjectStructure CreateIterator() First() Next() IsDone() CurrentItem() ObjectStructure CreateIterator() Client

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-24 PS95&96-MEF-L15-24 Dr. M.E. Fayad Iterator Pattern: Participants Iterator –provides an interface for enumerating elements ConcreteIterator –encapsulates and defines a traversal strategy over ObjectStructure –keeps track of the current position during traversal of the ObjectStructure ObjectStructure –defines an interface for returning an iterator ConcreteObjectStructure –accesses its elements –provides a ConcreteIterator for itself Client –uses a ConcreteIterator to access the elements of an ObjectStructure

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-25 PS95&96-MEF-L15-25 Dr. M.E. Fayad Iterator Pattern: Collaborations The Client repeatedly requests the next element of the ObjectStructure from iterator. The iterator: –keeps track of a “current object” in the ObjectStructure –can return the “current object” to the Client –can compute the next “current object” in sequence The Client will then perform some operation on the element If the ObjectStructure is a Composite then the Iterator has to store a path through the Composite to keep track of the current object and computing the next current object requires accessing the Composite.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-26 PS95&96-MEF-L15-26 Dr. M.E. Fayad Iterator Pattern: Alternative Implementations Who controls the Iteration? Who defines the traversal algorithm? How robust is the Iterator? Using polymorphic iterators in C++ Iterators may have privileged access Iterators for Composites Null Iterator

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-27 PS95&96-MEF-L15-27 Dr. M.E. Fayad Iterator Pattern: Client Requirements Easy to use Able to use different traversal algorithms on the collection Able to use the same interface on several different collections Shouldn’t affect the internals of the collection Performance efficiency Sample Client Code int foo(Clntype &collection) { for (collection.First(); !collection.IsDone(); collection.next() ) { process(collection. CurrentItem()) }

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-28 PS95&96-MEF-L15-28 Dr. M.E. Fayad Iterator Pattern: Developer Requirements Shouldn’t expose the internals of the object structure Maintainable Extensible Reusable

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-29 PS95&96-MEF-L15-29 Dr. M.E. Fayad Iterator Pattern: Design Solution #1 Using this scheme, client would write template void foo(Container &container) { for (collection.First(); !collection.IsDone(); collection.Next() ) cout << container.CurrentElement(); } Idea Each container type defines an iteration scheme. The class implements the iteration algorithm as well as any data structures needed for the iteration. This means that the object structure itself has all the methods and data to iterate over its own elements. Container Stack StackAR StackLL

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-30 PS95&96-MEF-L15-30 Dr. M.E. Fayad Requirements/Advantages 1. Easy to use interface 2. Interface is similar across different containers 3. Performance efficient 4. Containers can be iterated polymorphically 5. Flexible enough to allow inserts and deletes during iteration Disadvantages 1. If a client wants to subclass one of the containers, it forces the client to define an iteration algorithm for the new subclass 2. Creates a large class interface 3. Doesn’t allow simultaneous iterations over a container 4. We combined two concepts in a single class Iterator Pattern: Solution #1 Analysis

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-31 PS95&96-MEF-L15-31 Dr. M.E. Fayad Iterator Pattern: Design Solution #2 Using this scheme, client would write template void foo(Container &container) { Iterator *it = container.currentIterator(): for (it->First(); !it->IsDone(); it->Next() ) { cout CurrentElement(); } delete it; } Idea In the previous scheme, we have two concepts merged into one implementation. Now let’s try to separate the Iterator concept from the Container. The result is two class hierarchies. The iterator object defines the traversal algorithm and maintains the current state of the iteration.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-32 PS95&96-MEF-L15-32 Dr. M.E. Fayad Iterator StackIterator StackARIterator StackLLIterator Container Stack StackAR StackLL Iterator Pattern: More Design Solution #2

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-33 PS95&96-MEF-L15-33 Dr. M.E. Fayad Requirements/Advantages 1. Easy to use interface 2. Interface is similar across different containers 3. Performance efficient 4. Containers can be iterated polymorphically 5. More than one iteration scheme can easily be provided 6. Iterator implementation can change without affecting container classes 7. Allows simultaneous iteration of a container Disadvantages 1. Inserts and deletes may leave Iterators in an undefined state 2. The Iterator breaks the encapsulation of the object structure Iterator Pattern: Solution #2 Analysis

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-34 PS95&96-MEF-L15-34 Dr. M.E. Fayad Iterator Pattern: Design Solution #3 Using this scheme, client would write template void foo(Container &container) { Cursor c; for (collection.First(c); !collection.IsDone(c); collection.next(c) ) { cout << container.CurrentElement(c); } Idea Let the object structure define the iteration algorithm while maintaining the state in another object called a Cursor. The Cursor is then an argument to each iteration function of the object. Container Stack StackAR StackLL

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-35 PS95&96-MEF-L15-35 Dr. M.E. Fayad Requirements/Advantages 1. Easy to use interface 2. Interface is similar across different containers 3. Performance efficient 4. Containers can be iterated polymorphically 5. Allows simultaneous iterations over the same container Disadvantages 1. Inserts and deletes may leave Iterators in an undefined state 2. Users have to pass parameters to the iteration functions Iterator Pattern: Solution #3 Analysis

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-36 PS95&96-MEF-L15-36 Dr. M.E. Fayad Idea Each container object will accept an operation (in the form of an object) and apply it to every element in the container. This becomes a framework which allows clients to “plug-in” their own objects. Container Stack StackAR StackLL Iterator Pattern: Design Solution #4 InternalIterator UserSupplied

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-37 PS95&96-MEF-L15-37 Dr. M.E. Fayad Requirements/Advantages 1. Easy to use interface 2. Interface is similar across different containers 3. Performance efficient 4. Containers can be iterated polymorphically 5. Removes the iteration syntax from the client, which no longer has to worry about incrementing the counter or checking for the ending condition Disadvantages 1. Can only work with one container object at a time. We cannot compare elements from different containers 2. Cannot have simultaneous iterations over the same container Iterator Pattern: Solution #4 Analysis

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-38 PS95&96-MEF-L15-38 Dr. M.E. Fayad Iterator Pattern: Related Patterns Composite: Iterators are often applied to collections and recursive structures like Composites Memento: The iterator can use a Memento to capture the state of an iteration where the Iterator stores the Memento internally Visitors: They can be combined with Iterators so that responsibility for an algorithm over a complex structure is distributed between both of them –The Iterator’s Responsibility: Traversing the compound structure –The Visitor’s Responsibility: Implementing algorithm steps for each node in the structure

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-39 PS95&96-MEF-L15-39 Dr. M.E. Fayad Discussion Questions Define the relationship between the Iterator pattern and Memento, Composite, and Visitor. Describe the following patterns: State, Strategy, Steams, and Transaction. Mark (T) for true or (F) for false ( ) 1. The Command pattern is used when it is desirable to decouple the client from the target. ( ) 2. The Commands are first-class objects. ( ) 3. Macro Commands, History files, and Undo are useful techniques for extensibility in the Command pattern. ( ) 4. The Iterator pattern provide a uniform interface for iterating over different aggregate structures. ( ) 5. For a Composite to support an Iterator, each component must be able to enumerate its children. (This leads naturally to Nested Iterators.) ( ) 6. Iterators simplify the class of the aggregate objects.