externalize an objectʼs internal state so that the object ... · •memento pattern solves the...
TRANSCRIPT
![Page 1: externalize an objectʼs internal state so that the object ... · •Memento pattern solves the problem »Memento stores a snapshot of the internal state of another object – the](https://reader034.vdocuments.site/reader034/viewer/2022050304/5f6c9b304b7822677b1d4e03/html5/thumbnails/1.jpg)
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
![Page 2: externalize an objectʼs internal state so that the object ... · •Memento pattern solves the problem »Memento stores a snapshot of the internal state of another object – the](https://reader034.vdocuments.site/reader034/viewer/2022050304/5f6c9b304b7822677b1d4e03/html5/thumbnails/2.jpg)
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
![Page 3: externalize an objectʼs internal state so that the object ... · •Memento pattern solves the problem »Memento stores a snapshot of the internal state of another object – the](https://reader034.vdocuments.site/reader034/viewer/2022050304/5f6c9b304b7822677b1d4e03/html5/thumbnails/3.jpg)
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
![Page 4: externalize an objectʼs internal state so that the object ... · •Memento pattern solves the problem »Memento stores a snapshot of the internal state of another object – the](https://reader034.vdocuments.site/reader034/viewer/2022050304/5f6c9b304b7822677b1d4e03/html5/thumbnails/4.jpg)
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
![Page 5: externalize an objectʼs internal state so that the object ... · •Memento pattern solves the problem »Memento stores a snapshot of the internal state of another object – the](https://reader034.vdocuments.site/reader034/viewer/2022050304/5f6c9b304b7822677b1d4e03/html5/thumbnails/5.jpg)
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
![Page 6: externalize an objectʼs internal state so that the object ... · •Memento pattern solves the problem »Memento stores a snapshot of the internal state of another object – the](https://reader034.vdocuments.site/reader034/viewer/2022050304/5f6c9b304b7822677b1d4e03/html5/thumbnails/6.jpg)
Memento-6© Gunnar Gotshalks
Abstract Architecture
![Page 7: externalize an objectʼs internal state so that the object ... · •Memento pattern solves the problem »Memento stores a snapshot of the internal state of another object – the](https://reader034.vdocuments.site/reader034/viewer/2022050304/5f6c9b304b7822677b1d4e03/html5/thumbnails/7.jpg)
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
![Page 8: externalize an objectʼs internal state so that the object ... · •Memento pattern solves the problem »Memento stores a snapshot of the internal state of another object – the](https://reader034.vdocuments.site/reader034/viewer/2022050304/5f6c9b304b7822677b1d4e03/html5/thumbnails/8.jpg)
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
![Page 9: externalize an objectʼs internal state so that the object ... · •Memento pattern solves the problem »Memento stores a snapshot of the internal state of another object – the](https://reader034.vdocuments.site/reader034/viewer/2022050304/5f6c9b304b7822677b1d4e03/html5/thumbnails/9.jpg)
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
![Page 10: externalize an objectʼs internal state so that the object ... · •Memento pattern solves the problem »Memento stores a snapshot of the internal state of another object – the](https://reader034.vdocuments.site/reader034/viewer/2022050304/5f6c9b304b7822677b1d4e03/html5/thumbnails/10.jpg)
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
![Page 11: externalize an objectʼs internal state so that the object ... · •Memento pattern solves the problem »Memento stores a snapshot of the internal state of another object – the](https://reader034.vdocuments.site/reader034/viewer/2022050304/5f6c9b304b7822677b1d4e03/html5/thumbnails/11.jpg)
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
![Page 12: externalize an objectʼs internal state so that the object ... · •Memento pattern solves the problem »Memento stores a snapshot of the internal state of another object – the](https://reader034.vdocuments.site/reader034/viewer/2022050304/5f6c9b304b7822677b1d4e03/html5/thumbnails/12.jpg)
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
![Page 13: externalize an objectʼs internal state so that the object ... · •Memento pattern solves the problem »Memento stores a snapshot of the internal state of another object – the](https://reader034.vdocuments.site/reader034/viewer/2022050304/5f6c9b304b7822677b1d4e03/html5/thumbnails/13.jpg)
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
![Page 14: externalize an objectʼs internal state so that the object ... · •Memento pattern solves the problem »Memento stores a snapshot of the internal state of another object – the](https://reader034.vdocuments.site/reader034/viewer/2022050304/5f6c9b304b7822677b1d4e03/html5/thumbnails/14.jpg)
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