Memento Design Pattern In Java
Another behavioral design pattern called the Memento Design Pattern which is used to restore the state of an object to a previous state.
Join the DZone community and get the full member experience.Join For Free
Today, I would like to discuss another behavioral design pattern called the Memento Design Pattern which is used to restore the state of an object to a previous state.
Memento Design Pattern
- The Memento Design Pattern is one of the twenty-three well-known GoF design patterns that provide the ability to restore an object to its previous state.
- The Memento Design Pattern is implemented with the help of three objects: the originator, a caretaker, and a memento.
- Originator — The object whose internal state we like to store. The Originator object creates a memento object to store its internal state. So, the Originator object knows how to save and restore itself. The object which gets and sets the values of Memento objects.
- Caretaker — The object which knows why and when the Originator needs to save and restore itself. The object operates on the Originator while having the possibility to rollback. It maintains the history of the Memento objects created. The caretaker takes a snapshot of Originator before operating.
- Memento — The POJO object that contains basic
statestorage and retrieval capabilities. The Memento object is immutable in general. The object holds the internal state of the Originator and allows it to restore it.
- The classic example of the Memento Pattern is a pseudorandom number generator or finite state machine.
- Git stashing is another example of the Memento Design Pattern.
- The internal state of the Originator object should be saved externally so that the object can be restored to this state later. Also, the object's encapsulation must not be violated.
- The caretaker requests a Memento from Originator before operating. And use that Memento to restore the Originator to its previous state if needed.
- We can make Memento Design Pattern implementation more generic by using Serialization; that will eliminate the requirement of every class having its own Memento class.
- The Memento Design Pattern can also be used with the Command Design Pattern for achieving undo of the commands.
To understand the Memento Design Pattern let's take the example of the Employee class storing and retrieving its state using EmployeeMemento class.
Employee Class Storing State Using Memento Design Pattern
Code for Employee class:
Here we can see that the originator Employee class creates and restores EmployeeMemento. Also as you can see, I made EmpId to be set only while creating Employee object and I like to keep EmpId unique as well.
Code for EmployeeMemento class:
Now code for EmployeeCaretaker class:
I am using EmpId and the Message String as key while storing Memento. The same I will use while restoring the Employee using Memento.
Now, its time to write Main class to execute and test the output:
And the below is the the output:
I hope by the example, we get a fair idea about the implementation of the Memento Design Pattern.
Source Code can be found here: Memento-Design-Pattern-Sample-Code
Liked the article? Please don't forget to press that like button. Happy coding!
Opinions expressed by DZone contributors are their own.