Memento pattern is a behavioral design pattern. 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. Intent of Memento Design pattern is without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.
UML Diagram Memento design pattern

Design components
- originator : the object for which the state is to be saved. It creates the memento and uses it in future to undo.
- memento : the object that is going to maintain the state of originator. Its just a POJO.
- caretaker : the object that keeps track of multiple memento. Like maintaining savepoints.
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. Let’s see an example of Memento design pattern.
Java
import java.util.List;
import java.util.ArrayList;
class Life
{
private String time;
public void set(String time)
{
System.out.println("Setting time to " + time);
this .time = time;
}
public Memento saveToMemento()
{
System.out.println("Saving time to Memento");
return new Memento(time);
}
public void restoreFromMemento(Memento memento)
{
time = memento.getSavedTime();
System.out.println("Time restored from Memento: " + time);
}
public static class Memento
{
private final String time;
public Memento(String timeToSave)
{
time = timeToSave;
}
public String getSavedTime()
{
return time;
}
}
}
class Design
{
public static void main(String[] args)
{
List<Life.Memento> savedTimes = new ArrayList<Life.Memento>();
Life life = new Life();
life.set(" 1000 B.C.");
savedTimes.add(life.saveToMemento());
life.set(" 1000 A.D.");
savedTimes.add(life.saveToMemento());
life.set(" 2000 A.D.");
savedTimes.add(life.saveToMemento());
life.set(" 4000 A.D.");
life.restoreFromMemento(savedTimes.get( 0 ));
}
}
|
Output:
Setting time to 1000 B.C.
Saving time to Memento
Setting time to 1000 A.D.
Saving time to Memento
Setting time to 2000 A.D.
Saving time to Memento
Setting time to 4000 A.D.
Time restored from Memento: 1000 B.C.
Advantage
- We can use Serialization to achieve memento pattern implementation that is more generic rather than Memento pattern where every object needs to have it’s own Memento class implementation.
Disadvantage
- If Originator object is very huge then Memento object size will also be huge and use a lot of memory.
Use the Memento pattern when:
- A snapshot of (some portion of) an object’s state must be saved so that it can be restored to that state later.
- A direct interface to obtaining the state would expose implementation details and break the object’s encapsulation.
Further Read – Memento Method in Python If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the vast world of System Design? It's time for a transformation! Enroll in our
Mastering System Design From Low-Level to High-Level Solutions - Live Course and embark on an exhilarating journey to efficiently master system design concepts and techniques.
What We Offer:
- Comprehensive Course Coverage
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world System Design Project
- Proven Track Record with 100,000+ Successful Enthusiasts
Last Updated :
31 Oct, 2023
Like Article
Save Article