Memento Design Pattern

Thilini Weerasinghe
4 min readJun 6, 2021
source:lynda.com

This article is going to explain another interesting design pattern called the Memento design pattern. It is a pattern that can be used to save and undo or roll back the items that you already added. As it changes the states of the object, it is categorized under the behavioural design pattern. It is useful when the application is progressing and developers want to save the checkpoints in the application and restore back those checkpoints. There are three main components in the memento pattern.

🔥 Originator: It is responsible for maintaining the state of the class. Generally, a memento is inside of the originator

🔥 CareTaker: It is the place where keep the track of the originator. It manages the savepoints of the application.

🔥 Memento: It couples with the originator and passes the states to the CareTaker.

This design pattern is suitable when you want to gurantee the not violating of the encapsulation.

🔎How it works???

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. — geeksforgeeks

Advantages 👍 😃

  • It enables to create a snapshots of its object state without breaking its encapsulation
  • It simplify the code of the originator class

Disadvantages 👎 😢

  • If there is many memento created frequently, it uses lot of spaces from RAM
  • As the careTaker track the state of the originator, it may have chances to destroy the obsolete mementos
  • PHP, Python,Javascript can not ensure that it doesn’t tracking the state of the memeonto.

Implementation 💻

Use case: — Think you are creating a Text Editor application to edit/ format the text or writig documents using text, images and shapes. But sometimes users needs to undo the formatting upto some extend or completely. In such a scenarion we have to manage the state of the application. Can you imagine how you can implement this. Don’t confuse, this the place where we can apply the ememnto pattern. Let’s see how we can implements text editor application.

  • First create a Text class (Text.java) to handle the input texts
  • Then create the text editor application class TextEditorApp.java and inside it create the memeonto class too. To complete the implementation follow these steps.
  1. create an ArrayList<> to hold the values of texts
  2. Then create addText() to add the text
  3. Create a getText() method to get the input text. In here it uses clone() to avoid the changing the value of the original object.
  4. Then you have to create a Memento class inside the TextEditor class and create a constructor and a getText() method inside it.
  5. Finally create a two methods to save or preserve save() the text and undo or revert undo() the save text. In here be sure to return memento object to the these two methods. Otherwise it not works properly.
  • Now create the CareTake class CareTaker.java to track the states. When a user save a text it stores that text and if another text save, then newly added text store ontop of the previous added text. So it just works as a stack. If you want to undo task, it firstly revert the text that you save at last. So it follows Last In First Out (LIFO) method. Follow these steps to complete the care taker class
  1. Create a Stack<> to store the history of the text
  2. Create a method to save the text into the history.
  3. As same as in the second steps create revert method to revert the savig texts.
  • Finally create main application to run the programme. Type different text input and save them and revert them.

See the output and you can clearly understand how it works.

TextEditor[texts=[Text[text=1st messsage], Text[text=2nd messsage]]]TextEditor[texts=[Text[text=1st messsage], Text[text=2nd messsage], Text[text=3rd messsage], Text[text=4th messsage]]]TextEditor[texts=[Text[text=1st messsage], Text[text=2nd messsage], Text[text=3rd messsage], Text[text=4th messsage], Text[text=5th messsage]]]TextEditor[texts=[Text[text=1st messsage], Text[text=2nd messsage], Text[text=3rd messsage], Text[text=4th messsage], Text[text=5th messsage], Text[text=6th messsage]]]TextEditor[texts=[Text[text=1st messsage], Text[text=2nd messsage], Text[text=3rd messsage], Text[text=4th messsage], Text[text=5th messsage], Text[text=6th messsage], Text[text=7th messsage]]]TextEditor[texts=[Text[text=1st messsage], Text[text=2nd messsage], Text[text=3rd messsage], Text[text=4th messsage], Text[text=5th messsage], Text[text=6th messsage], Text[text=7th messsage]]]TextEditor[texts=[Text[text=1st messsage], Text[text=2nd messsage], Text[text=3rd messsage], Text[text=4th messsage], Text[text=5th messsage], Text[text=6th messsage]]]TextEditor[texts=[Text[text=1st messsage], Text[text=2nd messsage], Text[text=3rd messsage], Text[text=4th messsage], Text[text=5th messsage]]]TextEditor[texts=[Text[text=1st messsage], Text[text=2nd messsage], Text[text=3rd messsage], Text[text=4th messsage]]]TextEditor[texts=[Text[text=1st messsage], Text[text=2nd messsage]]]cannot undoTextEditor[texts=[Text[text=1st messsage], Text[text=2nd messsage]]]

Now you can see how the memento pattern can manage the states of the object. For more information refer the video tutorial in the reference.

Stay Safe & Learn New Things!!! 😃

References

--

--

Thilini Weerasinghe

Currently working as an Associate Software Engineer at Virtusa. Has completed degree in B.Sc (Hons) Computing & Information Systems. After all I am a Human...