The memento design pattern
Depending on the software we are writing, we might have a requirement to be able to restore the state of an object back to its previous state.
Note
The purpose of the memento design pattern is to provide the ability to execute an undo action in order to restore an object to a previous state.
The original memento design pattern is implemented with the help of three main objects:
Originator
: The object whose state we want to be able to restoreCaretaker
: The object that triggers the changes to theoriginator
object and uses thememento
objects for rollback, if neededMemento
: The object that carries the actual state of the originator and can be used to restore to one of the previous states
It is important to know that the memento
object can be handled only by the originator. The caretaker and all other classes can just store it and nothing else.
Example class diagram
A classic example of the memento design pattern that comes to mind is text editors. We can always undo whatever...