# Memento

- Purpose
- Structure
- Interaction
- Applications
- Consequences

## Purpose

- Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.

## Structure

- 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. 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. uses the memento to restore its internal state.
- Caretaker(undo mechanism): is responsible for the memento's safekeeping. never operates on or examines the contents of a memento.

## Interaction

- 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.

## Applications

- 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.

## Consequences

- Preserving encapsulation boundaries. Memento avoids exposing information that only an originator should manage but that must be stored nevertheless 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. See the discussion of incrementality in the Implementation section.
- 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.