Applied Design Patterns with Java
Behavioral :: Memento (283) {C ch 21}
Applicability
Use the Memento pattern when:
- a snapshot of (some portion of) an object's state must
be saved so that it can be restored to that state later, and
- a direct interface to obtaining the state would expose
implementation details and break the object's encapsulation.
Structure
Participants
- Memento
(SolverState) stores internal state of the Originator object. The Memento may store as much
or as little of the originator's internal state as necessary at its originator's discretion; and protects against
access by objects other than the originator. Mementos have effectively two interfaces. Caretaker sees a narrow interface
to the Memento - it can only pass the Memento to other objects. Originator, in contrast, sees a wide interface,
one that lets it access all the data necessary to restore itself to its previous state. Ideally, only the originator
that produced the Memento would be permitted to access the Memento's internal state.
- Originator (ConstraintSolver) creates a Memento containing a snapshot of its current internal state, and uses the
Memento to restore its internal state.
- Caretaker (undo
mechanism) is
responsible for the Memento's safekeeping, and never operates on or examines the contents of a Memento.
Catalog Behavioral Prev Next