Memento design pattern
Memento pattern is a behavioral design pattern. Memento pattern is used to restore state of an object to a previous state. As your application is progressing, you may want to save checkpoints in your application and restore back to those checkpoints later.
UML Diagram Memento design pattern
- originator : the object for which the state is to be saved. It creates the memento and uses it in future to undo.
- memento : the object that is going to maintain the state of originator. Its just a POJO.
- caretaker : the object that keeps track of multiple memento. Like maintaining savepoints.
A Caretaker would like to perform an operation on the Originator while having the possibility to rollback. The caretaker calls the createMemento() method on the originator asking the originator to pass it a memento object. At this point the originator creates a memento object saving its internal state and passes the memento to the caretaker. 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. The originator would accept the memento, using it to restore its previous state.
Let’s see an example of Memento design pattern.
Setting time to 1000 B.C. Saving time to Memento Setting time to 1000 A.D. Saving time to Memento Setting time to 2000 A.D. Saving time to Memento Setting time to 4000 A.D. Time restored from Memento: 1000 B.C.
- We can use Serialization to achieve memento pattern implementation that is more generic rather than Memento pattern where every object needs to have it’s own Memento class implementation.
- If Originator object is very huge then Memento object size will also be huge and use a lot of memory.
Further Read – Memento Method in Python
This article is contributed by Saket Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.