728x90 AdSpace

Sunday, 6 August 2017

Design Patterns - Memento Pattern

Memento Pattern


To record an object internal state without violating encapsulation and reclaim it later without knowledge of the original object. A memento is an object that stores a snapshot of the internal state of another object.

Where to use

•When letting some info in an object be available by another object.
•When you want to create snapshots of a state for an object.
•When you need undo/redo features.


Ability to restore an object to its previous state.


Care should be taken if the originator may change other objects or resources - the memento pattern operates on a single object. Using memento to store large amounts of data from Originator might be expensive if clients create and return mementos frequently.

Memento Pattern Class Diagram

In the class-diagram above:
•An instance from the Originator class is the object that knows how to save itself. Uses memento to restore itself.
•An instance from the Caretaker class is the object that knows why and when the Originator needs to save and restore itself. Never operates or examines the contents of memento
•An instance of the Memento class is the lock box that is written and read by the Originator, and shepherded by the Caretaker.

The originator is any object that has an internal state, that we want to take a snapshot of. The caretaker is going to do something to the originators state, but wants to be able to later restore the originators state. The caretaker first asks the originator for a memento object, containing the snapshot. Then it performs the sequence of operations it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.

Memento Pattern Example

public class Originator {
private String state;
/* lots of memory using private data that does not have to be
saved. Instead we use a small memento object. */
public void set(String state) {
System.out.println("Originator: Setting state to " + state);
this.state = state;
public Object saveToMemento() {
System.out.println("Originator: Saving to Memento.");
return new Memento(state);
public void restoreFromMemento(Object m) {
if (m instanceof Memento) {
Memento memento = (Memento) m;
state = memento.getSavedState();
System.out.println("Originator: State after restoring
from Memento: " + state);
import java.util.ArrayList;
import java.util.List;
public class Caretaker {
private List<Object> savedStates = new ArrayListList<Object>();
public void addMemento(Object m) {
public Object getMemento(int index) {
return savedStates.get(index);
public class Memento {
private String state;
public Memento(String stateToSave) {
state = stateToSave;
public String getSavedState() {
return state;

public class MementoExample {
public static void main(String[] args) {
Caretaker caretaker = new Caretaker();
Originator originator = new Originator();
When MementoExample is executed the result is:
c:>Originator: Setting state to State1
c:>Originator: Setting state to State2
c:>Originator: Saving to Memento.
c:>Originator: Setting state to State3
c:>Originator: Saving to Memento.
c:>Originator: Setting state to State4
c:>Originator: State after restoring from Memento: State3

Usage example

Often used in database transactions and undo/redo situations.

Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments


Post a Comment

Item Reviewed: Design Patterns - Memento Pattern Rating: 5 Reviewed By: Annamalai Thangaraj