Behavioral Patterns  «Prev  Next»

Memento Pattern | Behavioral Pattern


The Memento pattern[1] contains a snapshot of the state of an object, so that the object can return to its original state without having to reveal its content to the rest of the world.
The figure below illustrates the Memento pattern.

Memento Pattern consisting of Originator, Memento, and Caretaker


The following lists the benefits of using the Memento pattern:
  1. Preserves encapsulation boundaries
  2. Simplifies the originator

When to Use:

You should use the Memento pattern when:
  1. A snapshot of an object's state must be saved so that it can be restored to that state later.
  2. Using a direct interface to obtain the state would expose implementation details and break the object's encapsulation.
The Memento pattern allows the state of an object to be captured and stored. The object can be put back to this (previous) state, when needed.
[1]memento: an object kept as a reminder or souvenir of a person or event.