Memento Design Pattern

dilshan ukwattage
Nerd For Tech
Published in
4 min readJun 6, 2021

--

If you can go back to past what are the things that you want to change in your life? let’s say if you want to change something but the thing is you can’t do it in real life. But in Memento design pattern you can restore the state of an object to a previous state. So welcome back to another design pattern article. If you are new to design pattern you can always refer my previous articles. As you know this article is all about memento design pattern which is under the behavioral type. 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. So Memento design pattern lets you make full copies of an object’s state, including private fields, and store them separately from the object. Let’s look at the below UML diagram of the memento design pattern.

Memento design pattern mainly have three components. Those are,

  1. originator : the object which the state is need to be saved. It creates the memento and uses it in future to undo.
  2. memento : the object that is going to maintain the state of originator.
  3. caretaker : the object that keeps track of multiple memento. Like maintaining save points.

Now let’s get a simple example and learn this. When we writing articles we can undo and go back to previous state when ever we want. Let’s get this as a example and implement using memento design pattern. So first you need to create a class called Article. This article class is the originator class which means this article class object is need to be saved. Inside the article class I have three attributes called id,title and content. When implementing this scenario I only implement to undo the content of the article. So remember that. I implement the constructor for the class as well as setter and getter for the content attribute. Then you need to implement save and revert functions like in the below. Save and revert functions are the two functions we mainly use to save the object and to undo, we can use revert function. So when we saving we will create a ArticleMemento instance and put the current content of the article to that. When we use the revert function we get the previous state of the content and set it to the current content using ArticleMemento.

Then you need to create a memento class. In here I created a class called ArticleMemento. So inside the ArticleMemento you can put the attributes that you wish to undo. As I mentioned earlier in here I only implementing the content of the article to undo. If you want to the add other features to make undo you only need to do some simple modification to this classes.

Next you need to create the caretaker class so in here I have created a class called ArticleHistory as a caretaker class. Let’s say when you typing a word document you do some changes. Then when you undo first you will get the state before the last change. Then if you undo again you will get the state before the previous state. Same as this article scenario. So it’s First In First Out (FIFO)order. We have to implement a stack for that because in stack we can maintain FIFO. So in here I have implement a stack and like in the below. Then we have save and revert function. Save will save the state of and object pushing that to the stack. Revert will give back the previous state of the object.

Then in the main program you have create a instance from the Article class and ArticleHistory class. Next you can save that using save function and then you can do some changes to the content of the article using the setContent() method and save like in the below. Then you can undo the changes using the revert() method and print the instance to see whether the program works correctly.

The output of the program is given below.

Advantages of Memento Design Pattern

  1. Can always discard the unwanted changes and restore it to an intended or stable state.
  2. Provides an easy recovery technique.

Disadvantage of Memento Design Pattern

  1. If Originator object is very huge then Memento object size will also be huge and use a lot of memory.
  2. A high number of mementos require more storage. At the same time, they put additional burdens on a caretaker.

When to use memento pattern

  1. When you want to produce snapshots of the object’s state to be able to restore a previous state of the object.

That’s it guys. That’s the end of this article. I hope you learn about memento design pattern. I’ll upload the code to the GitHub and put the link in the references so you can refer that too. Then time to say good bye and let’s meet from another article. Stay in touch.

--

--

dilshan ukwattage
Nerd For Tech

Software Engineer at IFS R&D International (Pvt) Ltd