Memento Design Pattern

The Memento Design Pattern is a behavioral design pattern that allows us to capture the internal state of an object at a particular time without violating the encapsulation principle. This pattern is useful in situations where we need to maintain the state of an object and restore it at a later point in time. In this article, we will explore the Memento Design Pattern in Java and its implementation.

Overview of the Memento Design Pattern

The Memento Design Pattern is based on the idea of capturing the state of an object and storing it in a separate object called the memento. This memento object can then be used to restore the state of the original object later on. The Memento Design Pattern consists of three main components:

  1. Originator: This is the object whose state we want to capture and restore. It has a method to create a memento object that contains its current state, and a method to restore its state from a memento object.
  2. Memento: This is the object that stores the state of the originator. It has methods to get and set the state of the originator.
  3. Caretaker: This is the object that keeps track of the memento objects. It has methods to save and restore memento objects.

Implementing the Memento Design Pattern in Java

// Originator class
public class TextEditor {
private String text;
public void write(String text) {
    this.text = text;
}

public String getText() {
    return text;
}

public TextEditorMemento save() {
    return new TextEditorMemento(text);
}

public void restore(TextEditorMemento memento) {
    text = memento.getText();
}
}

// Memento class
public class TextEditorMemento {
private String text;
public TextEditorMemento(String text) {
    this.text = text;
}

public String getText() {
    return text;
}
}

// Caretaker class
public class TextEditorCaretaker {
private List mementos = new ArrayList<>();
public void addMemento(TextEditorMemento memento) {
    mementos.add(memento);
}

public TextEditorMemento getMemento(int index) {
    return mementos.get(index);
}
}

// Usage example
public class Main {
public static void main(String[] args) {
TextEditor editor = new TextEditor();
TextEditorCaretaker caretaker = new TextEditorCaretaker();
editor.write("Hello world!");
    caretaker.addMemento(editor.save());

    editor.write("This is some new text.");
    caretaker.addMemento(editor.save());

    editor.write("And here's even more text.");
    caretaker.addMemento(editor.save());

    // Restore the second memento (index 1)
    editor.restore(caretaker.getMemento(1));
    System.out.println(editor.getText()); // Output: "This is some new text."
}
}


In this example, the TextEditor class is the originator that holds the state (the text field) and provides methods to modify it (write()) and create a memento (save()). The TextEditorMemento class is the memento that holds a snapshot of the state. The TextEditorCaretaker class is the caretaker that stores the mementos in a list and provides methods to add and retrieve them.

In the usage example, we create a TextEditor instance and write some text to it. We then create a memento of the current state using editor.save() and add it to the caretaker using caretaker.addMemento(). We repeat this process twice more with different text to create a total of three mementos.

Finally, we restore the second memento (index 1) using editor.restore(caretaker.getMemento(1)). This sets the state of the TextEditor instance back to the state it had when the second memento was created. We can verify this by calling editor.getText() and seeing that it returns the text “This is some new text.”