Memento Flashcards
Intent
Without breaking encapsulation, capture and externalize an object’s internal state.
Motivation
Store a snapshot of the internal state of another object, originator
Structure
Participants
- Memento
- stores the internal state of the Originator
- Mementos have two interfaces: narrow interface (Caretaker) and wide interface (Originator)
- Originator
- creates a memento containing a snapshot of its current internal state
- uses the memento to restore its internal state
- Caretaker
- responsible for the memento’s safekeeping
- never examines or operates on the contents of memento
Related Patterns
- Command
- Iterator
Collaborations
Caretaker (client) can request a memento from the originator (to save a snapshot) and pass a memento back to the originator (to restore previous state)
AKA
Token
What is the role of a Caretaker
Setting and getting the memento.
Does the Caretaker have access to the contents of a memento?
No
Which participant stores the internal state of which object?
Memento stores the internal state of Originator.
Mementos have two interfaces, narrow and wide. Which participants are what?
- Originator* is wide interface.
- Caretaker* is narrow interface.