Behavioural Patterns
GoF pg. 221 -222
Iterator GoF pg. 257 – 271
Memento GoF pg. 283-291
By: Dan Sibbernsen
Overview
Behavioural
Concerned with algorithms and responsibilitiesamong objects.
Also concerned with methods of communicationbetween them
Allow you to put your “interconnected” hat onwhile taking off your “control” hat.
Iterator
Intent
Provide a means to Iterate through a series ofnodes in specific way(s)
Motivation
To traverse lists of information without having toknow anything too specific about the objects
To implement multiple algorithms to traversethose nodes.
Class Diagram
iterator.gif
Iterator:Implementation
Who Controls the Iteration?
External Iterator
Client controls iteration
Client must explicitly request next() on each node
Internal Iterator
Iterator controls iteration
Client hands an operation to the Iterator, Iterator tellsevery child in the aggregate to perform it.
Implementation Benefits
External Iterator
More flexible than Internal.  Can compare 2collections easily
Internal Iterator
Easier to use, as they define the iteration logic foryou.  Makes portability easier.
Who defines the traversalAlgorithm?
Algorithm can be held by a class outside of theIterator.  It could be held by the aggregate canhold it.
Called “the cursor.”
Allows the algorithm to access private members ofthe aggregate (and not break encapsulation)
Algorithm is held by the iterator
Allows algorithms to be swapped out easily.
How Robust is the Iterator?
If an iterator is robust it
Will allow for removal of nodes in the middle oftraversal.
It does this through registering the iterator withthe aggregate.  If any nodes are removed, it isnotified.
Additional Iterator Operations
All come with first, next, isdone, andcurrentItem
Previous
Goes to the previous node that was iterated
Can be helpful in certain situations
SkipTo
Skips to a certain node.
Goes to a node matching specific criteria
Using Polymorphic Iterators in c++
Drawbacks
Allocated dynamically by a factory method.
Client is responsible for deleting them.  Can beerror prone due to multiple levels of calls, and alsobecause of exceptions
Proxy can be used to remedy this.
Only use them when Polymorphism isrequired.
Iterators may have privilegedaccess
Makes coupling tighter between Iterator andAggregate
Can be problematic if there are multipleiterators you want to apply
Solution: make private iterator-specific functions“protected” so that only Iterator subclasses canaccess them
Iterators for composites
Use an Internal iterator to keep track of thelevels-deep it has traversed.  This happensrecursively so everything is stored on thestack.
Null Iterators
Makes traversal of Composite classes easier.
Always returns IsDone() as true.
Related Patterns
Composite
Factory Method
Memento
Consequences (good)
“Supports variations in the traversal of anaggregate.”
“Iterators simplify the Aggregate interface.”
“More than one traversal can be pending onan aggregate.”
Memento
Intent
“Without violating encapsulation, capture andexternalize an object’s internal state so that theobject can be restored to this state later.”
Motivation
When we want to store off an object’s internalstate without adding any complication to theobject’s interface.
Perhaps for an undo mechanism
Class Diagram
memento.gif
Undo Mechanism Complications
Constraint-Solver problem
Uses multiple levels deep of objects to drawobjects.
What if we want to support undo but can’tlocalize the changes to just one class?
Propose a Memento that gathers informationfrom objects contained in a certain class
Applicability
Use this
when you want to save state on a hierarchy’selements.
When the hierarchy’s interface would be broken ifimplementation details were exposed.
Participants
Memento
 stores the state of the Originator
Originator
Creates the memento
“Uses the memento to restore its internal state”
CareTaker
Keeps track of the Memento
Never uses the Memento’s Interface to theOriginator
Collaboration
Pg. 286 (can’t find image)
Caretaker requests a memento from anOriginator.
Caretaker passes back memento.
Originator uses it to restore state.
Consequences (good)
“Preserves Encapsulation Boundaries”
“It simplifies Originator”
You can name your class“mementos_TheFreshMaker”!!!
Consequences (bad)
Might be expensive
Difficulty defining interfaces to keepOriginator encapsulated
Hidden costs in caring for mementos
Caretaker could have to keep track of a lot ofinformation for the memento
You can name your class“mementos_TheFreshMaker” =(
Implementation
Language Support
C++ lets you call Originator a friend of Memento,thus giving it access to all private members ofMemento that it needs
Storing Incremental Changes
If storing state happens incrementally, thenwe can just record the changes of what’shappened in a new memento object.
This helps with memory difficulties.
Related Patterns
Command
Iterator