Memento Design Pattern

The memento design pattern is used when we need to do roll back or undo or revert the changes in our applications. Because, sometimes we may need to go back to the previous state of the object to save the changes.

The memento pattern is fallen under behavioral design pattern. In this pattern what we can do is restore the previous state of the object without doing any harm on it’s internal structure.

Now, you might get confused 😣 is it does same thing as prototype design. The answer is no. The major difference is prototype is creational design pattern while memento is behavioral. Other difference is prototype is initialized an object to be cloned while memento capture and restore only the internal structure of the object 😃.

In memento pattern, we have 3 pillars.

  1. Originator: This is the place where save the current and previous states of original object through set operations.
  2. Memento: This is the place where we can save the internal state of originator object.
  3. Care taker: This is the place where we store the mementos for later use.

In implementation, the originator coupled with memento and pass state in to the care taker. So, when we need to restore the previous state, we called to the care taker and checks the previous state.

Pros and Cons of Memento Design Pattern

👍Pros

  • Provide restore or revert abilities.
  • Keep the saved state and object separately and thus provides cohesion.

👎Cons

  • It can be time consuming task.

Now, let’s discuss about the real implementation of memento design pattern.

|Case Scenario

In the school, the teacher needs to calculate the average marks for English subject in her class. While she entering class, she may enter wrong marks for a student and then she needs to undo that and enter again. Now, we are creating a part of application to handle this revert process.

⭐️At the beginning, I have created a class to show marks details.

⭐️At the beginning, we can create the originator class to get the marks passed by sender. In the same class I used to get Memento class also. If you want, you can implement a separated class for that.

In Care Taker class, we can implement an array list to store the marks received by originator. Because,

Then we implement the add marks feature through addMark() and implement getter for this. Here, what we have done is clone the current state of the Array List instead getting direct instantiation. Because, we know about the behavior of array list. If I modify the directly instantiated array list in getMarks(), it will directly affect for our original array list. That is why we have cloned here instead of instantiation. That is why we have use here ArrayList instead of List here.

Also, here we have implemented here very special 2 methods called save() and revert().

  • save() is used to pass the current state of the Mark for Care Taker class (in this case it is MarksHistory class).
  • revert() is used to roll back the state of Mark.

But if I pass the current state of Mark object it, then we can not manipulate the object without violating its previous state. Therefore, in here we return MarkMemento to the care takerclass.

⭐️Then we can create the Care Taker class (in this case it is MarksHistory).

In here, the values of marks are stored as Stack. Therefore, we have used here Stack to store the values. Then, when we revert values, the stack will revert from the lastly entered item.

⭐️Now, let’s implement this in our main method and see the results.

Now let’s add more marks for English and see the results after adding each mark.

The out put:

It proves that the values for marks are adding as stack. Now let’s see how the revert is happening.

Let’s called revert(). Then what will it print. So, it will print only {[12,13,44,85,52]} or something else 😯.

The output:

So, is that your intended outcome. Nooo.. right? 😯 Let’s see what is happening here. When we revert after entering last value, what will care taker give you is its current state. Before you revert this, you have stored it (69) on the care taker. That is why it returns all 6 values twice as in 5th and 6th lines in the output.

Let’s revert this multiple time and see the results.

See the output:

Now, I hope you could get the proper understand about the how memento design pattern is worked.

Stay Safe !!!

References:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store