Top Banner
Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-1 PS95&96-MEF-L15-1 Dr. M.E. Fayad Creation al Paradigm Shift, Inc. Software Factory Behavior al Structur al Lesson 8: Behavioral Patterns Object- Object- Oriented Oriented Design Design Pattern Pattern s s
39

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.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-1

PS95&96-MEF-L15-1Dr. M.E. Fayad

Creational

Paradigm Shift, Inc.Software Factory

Behavioral

Structural

Lesson 8:Behavioral Patterns

Lesson 8:Behavioral Patterns

Object-Object-OrientedOriented

DesignDesign PatternPatternss

Page 2: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-2

PS95&96-MEF-L15-2Dr. M.E. Fayad

Lesson ObjectivesLesson Objectives

Objectives

Discuss the philosophy behind behavioral patterns

Explore the world of behavioral patterns

Present the following Patterns:

Command

Iterator

Page 3: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-3

PS95&96-MEF-L15-3Dr. 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

Page 4: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-4

PS95&96-MEF-L15-4Dr. M.E. Fayad

Behavioral Patterns

Command Pattern

• Topics– Command Definition

– Structure

– Menu-Command Example

– Problems it Solves

– Participants & Collaborations

– Benefits

– Related Patterns

Page 5: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-5

PS95&96-MEF-L15-5Dr. 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

Page 6: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-6

PS95&96-MEF-L15-6Dr. M.E. Fayad

Command Pattern: Structure

target->Action();

Target

Action()

ConcreteCommand

Execute()

Command

Execute()Client Invoker

state

target

Page 7: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-7

PS95&96-MEF-L15-7Dr. 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

Page 8: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-8

PS95&96-MEF-L15-8Dr. 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

Page 9: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-9

PS95&96-MEF-L15-9Dr. 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)

Page 10: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-10

PS95&96-MEF-L15-10Dr. M.E. Fayad

More Menu Example

for all c in commandsc->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

Page 11: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-11

PS95&96-MEF-L15-11Dr. 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

Page 12: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-12

PS95&96-MEF-L15-12Dr. 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

Page 13: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-13

PS95&96-MEF-L15-13Dr. 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

Page 14: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-14

PS95&96-MEF-L15-14Dr. 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

Page 15: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-15

PS95&96-MEF-L15-15Dr. 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

Page 16: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-16

PS95&96-MEF-L15-16Dr. M.E. Fayad

Command Pattern: Interaction Diagram

Target InvokerCommandClient

Command()

ExecuteAction()

AdoptCommand()

Page 17: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-17

PS95&96-MEF-L15-17Dr. 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

Page 18: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-18

PS95&96-MEF-L15-18Dr. 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

Page 19: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-19

PS95&96-MEF-L15-19Dr. M.E. Fayad

Behavioral Patterns

Iterator Pattern

• Topics– Iterator Definition

– Structure

– Problems it Solves

– Participants & Collaborations

– Solutions & Benefits

– Implementation Issues

– Related Patterns

Page 20: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-20

PS95&96-MEF-L15-20Dr. 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

Page 21: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-21

PS95&96-MEF-L15-21Dr. M.E. Fayad

Iteration: Basic Idea

Initialize iteration for (first element;

iteration not over;

next element)

Process element

Page 22: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-22

PS95&96-MEF-L15-22Dr. 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

Page 23: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-23

PS95&96-MEF-L15-23Dr. M.E. Fayad

Iterator Pattern: Structure

return new ConcreteIterator

ConcreteIterator

Iterator

First()Next()IsDone()CurrentItem()

ConcreteObjectStructure

CreateIterator() First()Next()IsDone()CurrentItem()

ObjectStructure

CreateIterator()

Client

Page 24: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-24

PS95&96-MEF-L15-24Dr. 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

Page 25: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-25

PS95&96-MEF-L15-25Dr. 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.

Page 26: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-26

PS95&96-MEF-L15-26Dr. 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

Page 27: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-27

PS95&96-MEF-L15-27Dr. 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())

}

}

Page 28: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-28

PS95&96-MEF-L15-28Dr. M.E. Fayad

Iterator Pattern: Developer Requirements

• Shouldn’t expose the internals of the object structure

• Maintainable

• Extensible

• Reusable

Page 29: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-29

PS95&96-MEF-L15-29Dr. M.E. Fayad

Iterator Pattern: Design Solution #1

Using this scheme, client would write

template <Class Element>

void foo(Container<Element> &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

Page 30: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-30

PS95&96-MEF-L15-30Dr. M.E. Fayad

• Requirements/Advantages1. 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

• Disadvantages1. 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

Page 31: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-31

PS95&96-MEF-L15-31Dr. M.E. Fayad

Iterator Pattern: Design Solution #2

Using this scheme, client would write

template <Class Element>

void foo(Container<Element> &container)

{

Iterator<Element> *it = container.currentIterator():

for (it->First(); !it->IsDone(); it->Next() ) {

cout << it->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.

Page 32: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-32

PS95&96-MEF-L15-32Dr. M.E. Fayad

Iterator

StackIterator

StackARIterator StackLLIterator

Container

Stack

StackAR StackLL

Iterator Pattern: More Design Solution #2

Page 33: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-33

PS95&96-MEF-L15-33Dr. M.E. Fayad

• Requirements/Advantages1. 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

• Disadvantages1. 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

Page 34: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-34

PS95&96-MEF-L15-34Dr. M.E. Fayad

Iterator Pattern: Design Solution #3

Using this scheme, client would write

template <Class Element>

void foo(Container<Element> &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

Page 35: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-35

PS95&96-MEF-L15-35Dr. 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

Page 36: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-36

PS95&96-MEF-L15-36Dr. 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

Page 37: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-37

PS95&96-MEF-L15-37Dr. 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

Page 38: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-38

PS95&96-MEF-L15-38Dr. 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

Page 39: 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.

Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Behavioral Patterns - Page L8-39

PS95&96-MEF-L15-39Dr. M.E. Fayad

Discussion QuestionsDiscussion 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.