Memento Pattern

📢 This article was translated by gemini-2.5-flash

Memento Pattern: Object Behavioral Pattern

Intent

Capture an object’s internal state without breaking encapsulation, save this state externally. This allows the object to be restored to its previously saved state later.

Structure

备忘录模式

Here’s the breakdown:

  • Memento: Stores the Originator’s internal state. The Originator decides which parts of its state the Memento should save. Prevents other objects (besides the Originator) from accessing the Memento’s contents.
  • Originator: Creates a Memento to record its current internal state. Uses the Memento to restore its internal state.
  • Caretaker: Responsible for storing the Memento. Cannot operate on or inspect the Memento’s content.

Applicability

The Memento pattern is useful when:

  • You need to save (part of) an object’s state at a certain point so it can be restored later.
  • Providing an interface for other objects to directly access this state would expose implementation details and break encapsulation.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.ArrayList;
import java.util.List;

public class MementoPattern {
    public static void main(String[] args) {
        Caretaker caretaker = new Caretaker();
        Originator originator = new Originator();

        originator.setState("first");
        Memento memento1 = originator.createMemento();
        caretaker.addMemento(memento1);

        originator.setState("second");
        Memento memento2 = originator.createMemento();
        caretaker.addMemento(memento2);

        originator.setState("third");
        Memento memento3 = originator.createMemento();
        caretaker.addMemento(memento3);

        caretaker.showMemento();

        Memento backup = caretaker.getMemento(2);
        originator.setMemento(backup);
        System.out.println("=========");
        System.out.println(originator.getState());

    }
}

class Originator{ // Originator
    private String state;

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

    public String getState(){
        return state;
    }

    public Memento createMemento(){
        return new Memento(state);
    }

    public void setMemento(Memento memento){
        state = memento.getState();
    }
}

class Memento{ // Memento
    private String state;

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

    public String getState(){
        return state;
    }
}

class Caretaker{ // Caretaker
    private List<Memento> mementoList = new ArrayList<>();

    public void addMemento(Memento memento){
        mementoList.add(memento);
    }

    public Memento getMemento(int index){
        return mementoList.get(index - 1);
    }

    public void showMemento(){
        int i = 1;
        for(Memento m : mementoList){
            System.out.println("No." + i + ": " + m.getState());
            i++;
        }
    }
}