"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Understanding the Memento Design Pattern in Java

Understanding the Memento Design Pattern in Java

Published on 2024-08-07
Browse:467

Understanding the Memento Design Pattern in Java

Problem

The Memento pattern addresses the need to capture and restore an object's internal state without violating its encapsulation. This is useful in scenarios where you want to implement undo/redo functionality, allowing an object to revert to a previous state.

Solution

The Memento pattern involves three main components:

  1. Originator: The object whose internal state needs to be saved and restored.
  2. Memento: An object that stores the internal state of the originator. The memento is immutable.
  3. Caretaker: Responsible for requesting the originator to save or restore its state from a memento.

The originator creates a memento containing a snapshot of its current state. This memento can then be stored by the caretaker and used to restore the originator's state when needed.

Pros and Cons

Pros

  • Preserves Encapsulation: Allows an object's internal state to be saved and restored without exposing its implementation details.
  • Simple Undo/Redo: Facilitates the implementation of undo/redo functionality, making the system more robust and user-friendly.
  • State History: Allows maintaining a history of previous states of the object, enabling navigation between different states.

Cons

  • Memory Consumption: Storing multiple mementos can consume significant memory, especially if the object's state is large.
  • Additional Complexity: Introduces additional complexity to the code, with the need to manage the creation and restoration of mementos.
  • Caretaker Responsibility: The caretaker needs to manage mementos efficiently, which can add responsibility and complexity to the system.

Example of Real-World Application

A practical example of the Memento pattern is in text editors that offer undo/redo functionality. Each change to the document can be saved as a memento, allowing the user to revert to previous states of the document as needed.

Example Code in Java

Memento pattern in code:

// Originator
public class Editor {
    private String content;

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

    public String getContent() {
        return content;
    }

    public Memento save() {
        return new Memento(content);
    }

    public void restore(Memento memento) {
        content = memento.getContent();
    }

    // Memento
    public static class Memento {
        private final String content;

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

        private String getContent() {
            return content;
        }
    }
}

// Caretaker
public class History {
    private final Stack history = new Stack();

    public void save(Editor editor) {
        history.push(editor.save());
    }

    public void undo(Editor editor) {
        if (!history.isEmpty()) {
            editor.restore(history.pop());
        }
    }
}

// Client code
public class Client {
    public static void main(String[] args) {
        Editor editor = new Editor();
        History history = new History();

        editor.setContent("Version 1");
        history.save(editor);
        System.out.println(editor.getContent());

        editor.setContent("Version 2");
        history.save(editor);
        System.out.println(editor.getContent());

        editor.setContent("Version 3");
        System.out.println(editor.getContent());

        history.undo(editor);
        System.out.println(editor.getContent());

        history.undo(editor);
        System.out.println(editor.getContent());
    }
}
Release Statement This article is reproduced at: https://dev.to/diegosilva13/understanding-the-memento-design-pattern-in-java-2c72?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3