Memento
Intent
Without violating encapsulation, capture and externalize an object's
internal state so that the object can be restored to this state later.
Participants
-
Originator - provides a method that creates a Memento and a
method with a Memento parameter that can be invoked later.
-
Memento - stores all or part of the internal state of the
Originator object.
-
Caretaker - obtains the Memento from the Originator and later
invokes an Originator method to restore its state, using the Memento
as a parameter.
Notes
The Originator sets the Memento's private state in its method for
creating a Memento.
This low-level violation of encapsulation is used to avoid having to
declare public methods for the Memento that would violate encapsulation
at a higher level.
Examples
-
UndoableEdit
defines the interface for a variety of undoable edit actions.
Swing text editors create their own implementations of this
interface, typically using nested classes.