The Memento pattern
In many situations, we need a way to easily take a snapshot of the internal state of an object, so that we can restore the object with it when needed. Memento is a design pattern that can help us implement a solution for such situations.
The Memento design pattern has three key components:
- Memento: A simple object that contains basic state storage and retrieval capabilities
- Originator: An object that gets and sets values of Memento instances
- Caretaker: An object that can store and retrieve all previously created Memento instances
Memento shares many similarities with the Command pattern.
Real-world examples
The Memento pattern can be seen in many situations in real life.
An example could be found in the dictionary we use for a language, such as English or French. The dictionary is regularly updated through the work of academic experts, with new words being added and other words becoming obsolete. Spoken and written languages evolve...