Top Banner
Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257 – 271 Memento GoF pg. 283-291 By: Dan Sibbernsen
26

Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257 271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Jan 18, 2018

Download

Documents

Iterator Intent – Provide a means to Iterate through a series of nodes in specific way(s) Motivation – To traverse lists of information without having to know anything too specific about the objects – To implement multiple algorithms to traverse those nodes.
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: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Behavioural Patterns

GoF pg. 221 -222Iterator GoF pg. 257 – 271Memento GoF pg. 283-291

By: Dan Sibbernsen

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

among objects.– Also concerned with methods of communication

between them– Allow you to put your “interconnected” hat on

while taking off your “control” hat.

Page 3: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Iterator

• Intent– Provide a means to Iterate through a series of

nodes in specific way(s)• Motivation– To traverse lists of information without having to

know anything too specific about the objects– To implement multiple algorithms to traverse

those nodes.

Page 4: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Class Diagram

Page 5: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

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 tells

every child in the aggregate to perform it.

Page 6: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Implementation Benefits

• External Iterator– More flexible than Internal. Can compare 2

collections easily• Internal Iterator– Easier to use, as they define the iteration logic for

you. Makes portability easier.

Page 7: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Who defines the traversal Algorithm?

• Algorithm can be held by a class outside of the Iterator. It could be held by the aggregate can hold it.– Called “the cursor.”– Allows the algorithm to access private members

of the aggregate (and not break encapsulation)• Algorithm is held by the iterator– Allows algorithms to be swapped out easily.

Page 8: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

How Robust is the Iterator?

• If an iterator is robust it– Will allow for removal of nodes in the middle of

traversal.– It does this through registering the iterator with

the aggregate. If any nodes are removed, it is notified.

Page 9: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Additional Iterator Operations

• All come with first, next, isdone, and currentItem

• 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

Page 10: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Using Polymorphic Iterators in c++

• Drawbacks– Allocated dynamically by a factory method.– Client is responsible for deleting them. Can be

error prone due to multiple levels of calls, and also because of exceptions• Proxy can be used to remedy this.

• Only use them when Polymorphism is required.

Page 11: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Iterators may have privileged access

• Makes coupling tighter between Iterator and Aggregate

• Can be problematic if there are multiple iterators you want to apply– Solution: make private iterator-specific functions

“protected” so that only Iterator subclasses can access them

Page 12: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Iterators for composites

• Use an Internal iterator to keep track of the levels-deep it has traversed. This happens recursively so everything is stored on the stack.

Page 13: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Null Iterators

• Makes traversal of Composite classes easier.• Always returns IsDone() as true.

Page 14: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Related Patterns

• Composite• Factory Method• Memento

Page 15: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Consequences (good)

• “Supports variations in the traversal of an aggregate.”

• “Iterators simplify the Aggregate interface.”• “More than one traversal can be pending on

an aggregate.”

Page 16: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Memento

• Intent– “Without violating encapsulation, capture and

externalize an object’s internal state so that the object can be restored to this state later.”

• Motivation– When we want to store off an object’s internal

state without adding any complication to the object’s interface.

– Perhaps for an undo mechanism

Page 17: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Class Diagram

Page 18: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Undo Mechanism Complications

• Constraint-Solver problem– Uses multiple levels deep of objects to draw

objects.– What if we want to support undo but can’t

localize the changes to just one class?– Propose a Memento that gathers information

from objects contained in a certain class

Page 19: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Applicability

• Use this– when you want to save state on a hierarchy’s

elements.– When the hierarchy’s interface would be broken if

implementation details were exposed.

Page 20: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

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 the

Originator

Page 21: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Collaboration

• Pg. 286 (can’t find image)• Caretaker requests a memento from an

Originator.• Caretaker passes back memento.• Originator uses it to restore state.

Page 22: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Consequences (good)

• “Preserves Encapsulation Boundaries”• “It simplifies Originator”• You can name your class

“mementos_TheFreshMaker”!!!

Page 23: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Consequences (bad)

• Might be expensive• Difficulty defining interfaces to keep

Originator encapsulated• Hidden costs in caring for mementos– Caretaker could have to keep track of a lot of

information for the memento• You can name your class

“mementos_TheFreshMaker” =(

Page 24: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Implementation

• Language Support– C++ lets you call Originator a friend of Memento,

thus giving it access to all private members of Memento that it needs

Page 25: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Storing Incremental Changes

• If storing state happens incrementally, then we can just record the changes of what’s happened in a new memento object.

• This helps with memory difficulties.

Page 26: Behavioural Patterns GoF pg. 221 -222 Iterator GoF pg. 257  271 Memento GoF pg. 283-291 By: Dan Sibbernsen.

Related Patterns

• Command• Iterator