What is Memento pattern?

The Memento pattern is a behavioral design pattern that allows you to capture and restore the internal state of an object without violating encapsulation. It provides a way to save and restore the state of an object to support undo/redo operations or to revert an object to a previous state.

The Memento pattern involves three main components: the originator, the memento, and the caretaker.

  • Originator: The originator is the object whose state needs to be saved and restored. It creates a memento object that represents the internal state of the originator at a particular point in time. The originator can also restore its state from a memento.
  • Memento: The memento is an object that stores the state of the originator. It provides a way to access the saved state but does not expose the details of that state to other objects. Only the originator can access the memento’s internal state.
  • Caretaker: The caretaker is responsible for managing the memento objects. It can request the originator to save its state by creating a memento and later request the originator to restore its state from a memento. The caretaker holds and organizes the mementos and provides an interface to the client code for performing undo/redo or state restoration operations.

Key benefits and use cases of the Memento pattern:

  1. Undo/Redo functionality: The Memento pattern enables the implementation of undo/redo functionality by saving and restoring the state of an object. It allows users to revert actions or restore an object to a previous state.
  2. State history tracking: The Memento pattern is useful for tracking the history of an object’s state over time. It provides a way to store multiple mementos representing different states of an object and allows for easy traversal and restoration of those states.
  3. Caching and rollback mechanisms: The pattern can be used to implement caching and rollback mechanisms. An object can save its state before performing critical operations, and if the operations fail or produce undesired results, the object can restore its state from the saved memento.
  4. Snapshotting and serialization: The Memento pattern can be used to capture a snapshot of an object’s state for serialization or persistence purposes. The memento can be serialized and stored in a file or database, and later the object’s state can be restored by deserializing the memento.

Example of the Memento pattern in Java:

// Originator
class Editor {
    private String content;

    public EditorState createState() {
        return new EditorState(content);
    }

    public void restoreState(EditorState state) {
        content = state.getContent();
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

// Memento
class EditorState {
    private final String content;

    public EditorState(String content) {
        this.content = content;
    }

    public String getContent() {
        return content;
    }
}

// Caretaker
class History {
    private List<EditorState> states = new ArrayList<>();

    public void push(EditorState state) {
        states.add(state);
    }

    public EditorState pop() {
        int lastIndex = states.size() - 1;
        EditorState lastState = states.get(lastIndex);
        states.remove(lastIndex);
        return lastState;
    }
}

// Usage:
Editor editor = new Editor();
History history = new History();

editor.setContent("Line 1");
history.push(editor.createState());

editor.setContent("Line 2");
history.push(editor.createState());

editor.setContent("Line 3");
System.out.println("Current content: " + editor.getContent());

EditorState prevState = history.pop();
editor.restoreState(prevState);
System.out.println("Restored content: " + editor.getContent());

In the above example, the Memento pattern is used to capture and restore the state of an Editor object. The Editor class represents the originator and has methods to save and restore its state. It creates an EditorState object to represent the state and provides access methods for getting and setting the content.

The EditorState class represents the memento object and holds the content of the Editor at a specific point in time. The memento’s content is immutable and can only be accessed through getter methods.

The History class acts as the caretaker and manages a list of mementos representing the state history of the Editor. It provides methods to push a new memento to the history and pop the last memento from the history.

In the usage example, the Editor object’s content is modified multiple times, and a memento is created and pushed to the history after each modification. The history is then used to restore the previous state of the editor, reverting it to a previous content.

By using the Memento pattern, the Editor object can save and restore its state without exposing the internal details to other objects. The pattern allows for undo/redo functionality, state history tracking, or snapshotting and serialization of object state.

error: Content is protected !!