Contents
When to use memento Design pattern?
Use the Memento pattern when you want to produce snapshots of the object’s state to be able to restore a previous state of the object. The Memento pattern lets you make full copies of an object’s state, including private fields, and store them separately from the object.
Where is memento Design pattern used?
The memento pattern is typically used to store the historical state of an object and, therefore, allow previous states to be restored at any time. The pattern shares many similarities with the command pattern , which we explored in our previous Behavioral Design Patterns: Command Design Pattern post.
What is the purpose of memento pattern?
The memento pattern allows one to capture the internal state of an object without violating encapsulation such that later one can undo/revert the changes if required.
How is the memento pattern used in the editor class?
Hence, instead of other objects trying to copy the editor’s state from the “outside,” the editor class itself can make the snapshot since it has full access to its own state. The pattern suggests storing the copy of the object’s state in a special object called memento.
Where are the contents of a memento stored?
The pattern suggests storing the copy of the object’s state in a special object called memento. The contents of the memento aren’t accessible to any other object except the one that produced it.
How is the memento an immutable value object?
The Memento is a value object that acts as a snapshot of the originator’s state. It’s a common practice to make the memento immutable and pass it the data only once, via the constructor. The Caretaker knows not only “when” and “why” to capture the originator’s state, but also when the state should be restored.
How does a caretaker maintain a memento object?
The caretaker maintains the memento object and performs the operation. In case of the need to undo the operation, the caretaker calls the setMemento () method on the originator passing the maintained memento object.