externalize an objectʼs internal state so that the object ... · •Memento pattern solves the...
Transcript of externalize an objectʼs internal state so that the object ... · •Memento pattern solves the...
Memento-1© Gunnar Gotshalks
Memento Pattern – Behavioural
• Intent» Without invalidating encapsulation, capture and
externalize an objectʼs internal state so that theobject can be restored to this state later
• Also known as» Token
Memento-2© Gunnar Gotshalks
Motivation
• Sometimes it is necessary to record the internal stateof an object» Implementing checkpoints» Undo mechanisms
• Save the state to be able to restore it later
• Do not want to comprise an objectʼs reliability andextensibility by exposing the internal state
Memento-3© Gunnar Gotshalks
Example Graphic
• Consider a graphical editor that supports connectivitybetween objects» As the grey rectangle is moved the line stretches
and moves its end points» A ConstraintSolver object is used to encapsulate
this functionality» Supporting an undo function for this is not easy
Memento-4© Gunnar Gotshalks
Example Graphic – 2
• Just recording the original position of the grey box isinsufficient» UNDO: moves the box to original position and re-
computing the connections may not work> There usually is often slack in the computation> Not all connections may be as in the original
Memento-5© Gunnar Gotshalks
Example Graphic – 3
• Memento pattern solves the problem» Memento stores a snapshot of the internal state of
another object – the originator» The undo mechanism requests a memento from
the originator when it needs to checkpoint the state» The original initializes the memento with its current
state> Only the originator can store and retrieve
information from the memento
• In the example, the ConstraintSolver acts as anoriginator
Memento-6© Gunnar Gotshalks
Abstract Architecture
Memento-7© Gunnar Gotshalks
Scenario
1 Request memento originator.createMemento2 Create and set memento state3 Set state from memento originator.setFrom(memento)4 get_state
Scenario: Create & use memento
Memento-8© Gunnar Gotshalks
Participants
• Memento (SolverState)» Stores internal state of the Originator object
> Enough state for the purpose
» Has two interfaces> Caretaker sees narrow interface
– Can only pass memento to other objects> Originator sees a wide interface
– To be able to store & access all necessaryinformation
Memento-9© Gunnar Gotshalks
Participants – 2
• Originator (ConstraintSolver)» Creates a memento containing a snapshot of its
current internal state» Uses a memento to restore its internal state
• Caretaker (undo mechanism)» Is responsible for the mementoʼs safekeeping» Never operates on or examines the contents of a
memento
Memento-10© Gunnar Gotshalks
Applicability
• Use when» A snapshot of some portion of an objectʼs state
must be saved so that it can be restored to thatstate later
» A direct interface to obtaining the state wouldexpose implementation details and break theobjectʼs encapsulation
AND
Memento-11© Gunnar Gotshalks
Consequences
• Preserving encapsulation boundaries» Avoids exposing information that only an
originator should manage but must be storedelsewhere
• Simplifies Originator» In other encapsulating-preserving designs
Originator state is only in the Originator putting astorage and retrieval burden on the Originator
Memento-12© Gunnar Gotshalks
Consequences – 2
• Mementos might be expensive» There may be considerable overhead if the state is
large
» Clients create and return mementos frequently
• Defining narrow and wide interfaces» May be difficult in some languages to restrict
internal state to only Originator access
OR
Memento-13© Gunnar Gotshalks
Consequences – 3
• Hidden costs in Caretaker» Is responsible for saving mementos» Does not know how large mementos may be» Otherwise lightweight Caretakers may incur large
storage costs
Memento-14© Gunnar Gotshalks
Related Patterns
• Command can use mementos to maintain state forundoable operations
• Mementos can be used in iterators» Memento-based iteration doesnʼt require breaking
the collectionʼs encapsulation> All information is interpreted only by the
collection