Understanding Memento Pattern

Jalitha Dewapura
Design Patterns with Java
5 min readMay 24, 2021

--

In the previous blog posts, we walked over to the few design patterns. If you haven’t read them yet, I suggest you read that before proceeding with this article.

Article series:- Design Patterns with Java

Did you ever make trouble with an undo feature? If yes, this article will give you a good understanding of implementing an undo feature in your application.

Actually, the Memento pattern is specially designed to restore the state of an object to its previous state. This pattern belongs to behavioral design patterns which deal with interaction or communication between these objects. First, you need to understand if you need to restore the previous states, you should store all states in a collection. Assume we want to implement a text editor. To implement the undo feature, the text editor has to store its previous content in a collection. Let’s divide this process into three parts.

First of all, we need to create a class called Editor. it consists of features related to maintaining the content. For example, we need to implement methods like setContent(), getContent(), restore(), etc.

As I explained, the restore feature works with previous states. In this case, it is previous content. Actually, this state is like a snapshot of the Editor class. But the Editor class has methods, fields which are not related to the content. So, we create a new class to maintain all revertible fields called EditorState class.

Now we have the editor tool and the content state. We need some other collection to store these states. Let’s take a separate class called History. It will maintain the collection of states.

Actually, we can implement these three parts in a single class. But that solution will be violating a very important principle in object-oriented design. That is the single responsibility principle. It basically means, every class should have a single responsibility.

In the Memento pattern, these three classes are called Originator, Memento, and Caretaker. Let’s map into our previous example.

Originator(Editor) class — This is the place that is required to include undo feature.

Memento(EditorState) class — This class consists of all revertible fields(in this example, it keeps the content field). An instance of this class is taken as the state.

Caretaker(History) class — This is the place that maintains the collection of states.

Let’s see the common UML diagram to understand it.

UML diagram for Memento pattern

UML diagram for the previous example (editor),

Don’t worry about the UML diagram. You just need to remember the responsibilities of three different classes. Let’s start with the EditorState class.

The EditorState class consists of reversible fields. In this case, it is the content variable. It would be nice to initialized with the constructor. As I explained, we store an instance of this class as a state. It means the content field should be initialized only when the instance is created. Let’s keep the content field as final to ensure it will not be changed. At last, I create the getter method for the content field.

Let’s move on to the Editor class. It also consists of the content field. Then we need to create the getter and setter methods of the content field. There should be a method to create states. So, that method creates a new instance of EditorState and the instance will be returned. And also, we create a restore() method. It takes a state as an argument and the content value of the state is assigned to the class content value.

Finally, we need to create the History class. Actually, the undo feature is running with the last-in-first-out method. So, I used a stack to store all states. Then we need to implement a save() method that pushes the current state to the stack. Furthermore, we need to create a revert() method that takes the last state and sends it to the restore method of the Editor class. Before we restore the state, we need to check whether this stack is empty or not. Because if the state is empty, it will throw an EmptyStackException.

Then we completely implement all three classes. Let’s see how to use it in the application.

First, we need to create two instances, one is from the Editor class and the other is from the History class. The Editor class has the setContent() method to set the current state content value. We always have to save the state after changing the content. So, the History class has the save() method to save the current state and the Editor class has the createState() method to create the state.

Then we need to revert it. For that, we can use the revert() method in History class. If it is the last element of the stack, it will throw an error called ‘cannot undo’.

Let’s see the output of this program,

Did you ever see some text editors’ undo feature works by letter to letter? But some text editors’ undo feature works by word to word. I think now you can understand.

Finally, we completed the implementation. I hope you understand the concept behind this pattern. If I miss some important points, let me know in the comment section. Keep in touch. I will frequently post about these design patterns. Happy learning !

--

--