Memento Design Pattern
Start course
3h 59m

In this course, we will learn the concepts of microservice and spring framework with a focus on design patterns.

Learning Objectives

  • Learn about various design patterns

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud


  • Basic Java knowledge

Hello, dear friends. In this video, we will examine the Memento Design Pattern, so, let's begin. As your application is progressing, you may want to save checkpoints in your application and restore back to those checkpoints later. The memento design pattern described by the Gang of Four in their book is a behavioral design pattern. The memento design pattern offers a solution to implement undoable actions. We can do this by saving the state of an object at a given instant and restoring it if the actions performed since need to be undone. Memento pattern is used to restore the state of an object to a previous state in other words. 

The memento object should expose as little information as possible to the caretaker. This is to ensure that we don't expose the internal state of the originator to the outside world as it would break encapsulation principles. However, the originator should access enough information in order to restore to the original state. The memento design pattern is one of the 23 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.

The memento pattern is a software design pattern that exposes the private internal state of an object. One example of how this can be used is to restore an object to its previous state, undo via rollback. Another is versioning and another is custom serialization. The memento pattern is implemented with three objects like I said before: the originator, a caretaker, and a memento. The originator is some object that has an internal state. The caretaker is going to do something to the originator but wants to be able to undo the change. The caretaker first asked the originator for a memento object, then it does whatever operation it was going to do. 

To roll back to the state before the operations, it returns the memento object to the originator. The memento object itself is an opaque object. When using this pattern, care should be taken if the originator may change other objects or resources. The memento pattern operates on a single object. So, let's suppose you're going to an event like a birthday party or a marriage. You'll buy some gift for that person, I should hope. If that person sees that gift after five years, it could remind them of your presence at that event, so the gift is acting as a memento. Let's consider the example of a bank. If your bank debit some amount mistakenly, you would call your bank and raise a query.

The bank checks your transaction. And if the operation was done by mistake, the bank would return you the debited amount. Here the bank is rolled back in your transaction by changing the state to the previous state. As you can see in the UML diagram, the memento pattern has three participants. Originator is 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. Originator is the object which gets and sets the values of memento objects. Caretaker is 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 roll back. It maintains the history of the memento objects created. The caretaker takes a snapshot of the originator before operating. Memento is the POJO object that contains basic state storage 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. Memento preserves encapsulation boundaries and it simplifies the originator. I think that's the most important advantage of memento to remember. Now, I think we'll understand a little better with an example. So, let's create a new project and name it Memento.

First, create an originator class that will use memento object to restore its previous state. In this, we need a state parameter. Let's add getter and setter for state. Now, we need two methods also to saveStateMemento and getStateFromMemento. Now, create a memento class that will store internal state of the originator object. Add state parameter and generate setter and getter. Now, we will create a caretaker class that will be responsible for keeping the memento. Name will be CareTaker.

First, we have to import list libraries. Create a list instance from memento. Now we need add and get methods. Okay, done. Structure is ready. Let's use in main. First, create an instance from originator. Create another instance from careTaker. First, we will setState to originator. Now, we will save to memento. Set State2. Now, save memento again. Let's copy for State3.

Now, we will set State4. Let's show the state changes to the users. Current state. First saved, second and third. Let's try and look the result. As you see, current state is State4. First saved is one, second is two, and this is three. We have now completed the memento design pattern. I hope you now have a better understanding of how the memento pattern works. We'll continue with the observer design pattern in the next video. So, I'll see you in the following video my friends.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.