Applied Design Patterns with Java
Behavioral :: Memento (283) {C ch 21}
Collaborations
- A caretaker requests a Memento from an originator,
holds it for a time, and passes it back to the originator, as the following Interaction Diagram illustrates:
Sometimes the caretaker won't pass the Memento
back to the originator, because the originator might never need to revert to an earlier state.
- Mementos
are passive. Only the originator that created a Memento will assign or retrieve its state.
Consequences
Memento
has the following benefits and liabilities:
- Preserving encapsulation boundaries. Memento avoids exposing
information that only an originator should manage but that must be stored outside the originator. The pattern shields
other objects from potentially complex Originator internals, thereby preserving encapsulation boundaries.
- It simplifies Originator. In other encapsulation-preserving designs, Originator keeps the versions of internal state
that clients have requested. That puts all the storage management burden on Originator. Having clients manage the
state they ask for simplifies Originator and keeps clients from having to notify originators when they're done.
- Using mementos might be expensive. Mementos might incur
considerable overhead if Originator must copy large amounts of information to store in the Memento or if clients
create and return Mementos to the originator often enough. Unless encapsulating and restoring Originator state is
cheap, the pattern might not be appropriate.
- Defining narrow and wide interfaces. It may be difficult in some languages to ensure that only the originator can access the
Memento's
state.
- Hidden costs in caring for mementos. A caretaker is responsible for deleting the Mementos it cares for. However, the caretaker has no idea how much state is in the Memento. Hence
an otherwise lightweight caretaker might incur large storage costs when it stores Mementos.
Catalog Behavioral Prev Next