Memento Pattern
Source:: Software Design Patterns - Best Practices for Software Developers
Definition
The memento pattern let’s us capture the internal state of an object without exposing its internal structure so that the object can be restored to this state later.
The literal meaning of memento is an object kept as a reminder or souvenir of a person or an event.
The object whose state we capture is called the Originator. The originator’s snapshot is called the memento. The memento object is held by another object called the Caretaker.
The interaction between these three entities happens as follows:
- The caretaker requests the originator for a snapshot of its internal state.
- The originator produces a memento.
- The memento is held by the caretaker and passed back to the originator when required to revert its state to that captured in the memento If that need doesn’t arise, the memento is eventually discarded by the caretaker.