Applied Design Patterns with Java

Behavioral :: Memento (283) {C ch 21}


Collaborations

Sometimes the caretaker won't pass the Memento back to the originator, because the originator might never need to revert to an earlier state.

Consequences

Memento has the following benefits and liabilities:

  1. 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.
  2. 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.
  3. 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.
  4. Defining narrow and wide interfaces. It may be difficult in some languages to ensure that only the originator can access the Memento's state.
  5. 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