Memento Design Pattern in Java

Table of Contents

Introduction

Memento Pattern is one of the behavioral patterns. Memento pattern is a software design pattern that provides the ability to restore state of an object to its previous state.

The memento design pattern is implemented with three objects: the originator, a caretaker and a memento. The originator is an object that has an internal state. The caretaker asks the originator for a memento object. Then caretaker does something which make changes to the originator, but has the ability to undo the change. To roll back to the state before operations, it returns the memento object to the originator.

The real life example would be when we write something to the notepad, wordpad etc. then we can undo the text or characters using ctrl+z in Windows Operating System.

Prerequisites

Java

Memento Design Pattern Implementation

The following Memento class will track a state of an object:

public class Memento {

	private String state;

	public Memento(String state) {
		this.state = state;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

}

Next create a class (originator) which will save to and restore from Memento object:

public class Originator {

	private String state;

	public void set(String state) {
		System.out.println("Originator: Setting time to " + state);
		this.state = state;
	}

	public Memento saveToMemento() {
		System.out.println("Originator: Saving to Memento.");
		return new Memento(state);
	}

	public void restoreFromMemento(Memento memento) {
		this.state = memento.getState();
		System.out.println("Originator: Time restored from Memento: " + state);
	}

}

Now create a class (care taker) to add objects to and get from Memento:

public class CareTaker {

	private List<Memento> mementos = new ArrayList<>();

	public void addToMemento(Memento memento) {
		mementos.add(memento);
	}

	public Memento getFromMemento(int index) {
		return mementos.get(index);
	}

}

Main Class

Create a test class for testing the memento design pattern:

public class MementoDesignPatternApp {

	public static void main(String[] args) {

		Originator originator = new Originator();
		CareTaker careTaker = new CareTaker();

		originator.set("1 year ago");

		careTaker.addToMemento(originator.saveToMemento());

		originator.set("6 months ago");

		careTaker.addToMemento(originator.saveToMemento());

		originator.set("7 days ago");

		careTaker.addToMemento(originator.saveToMemento());

		originator.restoreFromMemento(careTaker.getFromMemento(0));

		originator.restoreFromMemento(careTaker.getFromMemento(1));

		originator.restoreFromMemento(careTaker.getFromMemento(2));
	}

}

Output

Running the above main class will give you the following output:

Originator: Setting time to 1 year ago
Originator: Saving to Memento.
Originator: Setting time to 6 months ago
Originator: Saving to Memento.
Originator: Setting time to 7 days ago
Originator: Saving to Memento.
Originator: Time restored from Memento: 1 year ago
Originator: Time restored from Memento: 6 months ago
Originator: Time restored from Memento: 7 days ago

Source Code

Download

2 thoughts on “Memento Design Pattern in Java

Leave a Reply

Your email address will not be published.