GofPatterns Gofpatterns





Behavioral Patterns  «Prev  Next»

Memento Pattern

Description
The Memento pattern 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
Memento Pattern consisting of Originator, Memento, and Caretaker
Benefits:
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.
memento: an object kept as a reminder or souvenir of a person or event.