Simple definition#
The Memento pattern lets an object save a snapshot of its internal state and later restore it, while keeping that state private: other objects can store snapshots but cannot read or change what is inside them.
GoF intent: “Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.” It is also known as Token.
The problem#
A drawing app needs “checkpoints”: save now, experiment, and roll back if the experiment fails. The obvious solution, adding getters and setters for every field so a history class can copy them, exposes the canvas’s internals to the world. Any class could then put the canvas into an invalid state, and changing the internal representation breaks the history class.
Real-world analogy#
A save point in a video game. The game writes an opaque save file; you can keep several and load one later, but you do not edit its bytes. Only the game knows how to read it.
Structure#
- Originator (
DrawingCanvas) — creates a memento of its current state and restores from one. - Memento (
DrawingCanvas.Memento/Snapshot) — stores the state; offers a narrow interface to everyone and a wide one only to the originator. - Caretaker (
History) — keeps mementos and decides when to restore; never inspects them.
classDiagram
direction LR
class DrawingCanvas {
-List~String~ shapes
-String strokeColour
+save(String) Memento
+restore(Memento)
}
class Memento {
<<interface, narrow>>
+label() String
}
class Snapshot {
<<private record, wide>>
label
shapes
strokeColour
}
class History {
-Deque~Memento~ checkpoints
-int limit
+push(Memento)
+pop() Memento
}
Memento <|.. Snapshot
DrawingCanvas ..> Snapshot : creates / reads
History o--> Memento : storessequenceDiagram
participant U as Client
participant C as DrawingCanvas
participant H as History
U->>C: save("after triangle")
C-->>U: Memento
U->>H: push(memento)
U->>C: draw("scribble")
U->>H: pop()
H-->>U: Memento
U->>C: restore(memento)Java implementation#
The code is in examples/java/src/hub/patterns/memento/MementoDemo.java.
/** Originator: creates mementos of its own state and restores from them. */
final class DrawingCanvas {
/** Narrow interface: all that the caretaker may see of a memento. */
interface Memento {
String label();
}
/** Wide interface: private to the originator, so nobody else can read or forge the state. */
private record Snapshot(String label, List<String> shapes, String strokeColour) implements Memento { }
private List<String> shapes = List.of();
private String strokeColour = "black";
void draw(String shape) { shapes = append(shapes, shape); }
void stroke(String colour) { strokeColour = colour; }
Memento save(String label) {
return new Snapshot(label, shapes, strokeColour); // shapes is immutable: no copy needed
}
void restore(Memento memento) {
if (!(memento instanceof Snapshot s)) {
throw new IllegalArgumentException("Not a memento from this canvas");
}
shapes = s.shapes();
strokeColour = s.strokeColour();
}
@Override public String toString() { return shapes + " stroke=" + strokeColour; }
private static List<String> append(List<String> list, String item) {
var copy = new java.util.ArrayList<>(list);
copy.add(item);
return List.copyOf(copy);
}
}/** Caretaker: stores mementos and decides when to restore. It never looks inside them. */
final class History {
private final Deque<DrawingCanvas.Memento> checkpoints = new ArrayDeque<>();
private final int limit;
History(int limit) { this.limit = limit; }
void push(DrawingCanvas.Memento m) {
checkpoints.push(m);
if (checkpoints.size() > limit) {
checkpoints.removeLast(); // bound the memory used by snapshots
}
}
DrawingCanvas.Memento pop() { return checkpoints.pop(); }
List<String> labels() { return checkpoints.stream().map(DrawingCanvas.Memento::label).toList(); }
}Now: [circle, square, triangle, scribble] stroke=red
Checkpoints: [after triangle, after red square] (oldest dropped, limit 2)
Restore 'after triangle': [circle, square, triangle] stroke=red
Restore 'after red square': [circle, square] stroke=redStep-by-step walkthrough#
- Narrow and wide interfaces. The public
Mementointerface exposes only a label. The privateSnapshotrecord holds the real state. OutsideDrawingCanvas, nobody can nameSnapshot, so nobody can read its fields. This is how Java expresses GoF’s “wide interface for the originator only” (in C++ GoF usedfriend). - Restoring checks the type. A memento from a different originator is rejected.
- Immutable state makes snapshots cheap.
shapesis an immutable list, sosave()can store the reference without copying. Everydraw()creates a new list, leaving old snapshots untouched. With mutable state,save()must deep-copy, see Prototype. - The caretaker bounds memory.
Historykeeps at most two checkpoints and drops the oldest.
Memento versus Command for undo#
| Memento (snapshot) | Command (inverse operation) | |
|---|---|---|
| Stores | The state before a change | The operation and what is needed to reverse it |
| Undo | Restore the snapshot | Execute the inverse |
| Memory | Proportional to state size | Proportional to the change |
| Works when | Inverses are hard to compute (filters, complex edits) | Inverses are simple (insert/delete) |
Editors often combine them: each command stores a memento of the part of the state it changes.
Advantages and disadvantages#
| Advantages | Disadvantages |
|---|---|
| Preserves encapsulation: state stays private | Snapshots of large state consume memory and time |
| Simplifies the originator: history management lives elsewhere | Caretakers do not know how big mementos are |
| Easy to implement checkpoints, undo and rollback | In languages without access control tricks, the narrow interface is hard to enforce |
When to use it#
- Undo, checkpoints, “cancel” in dialogs, rollback of a failed multi-step operation.
- Exposing the state directly would break encapsulation.
When not to use it#
- The state is very large and changes are small: prefer Command with inverses, or store diffs.
- The state is already immutable and shareable: simply keep references to previous versions (persistent data structures).
Common mistakes#
- Unbounded histories in long-running sessions.
- Mementos that expose setters or public fields, so caretakers can tamper with state.
- Serializing mementos to disk without versioning, so old saves cannot be loaded after a release.
Real-world uses#
- Undo stacks in editors and IDEs, often combined with Command.
javax.swing.undo.StateEditstores before/after state snapshots.- Database savepoints and MVCC snapshots are mementos at the storage level; see Transactions and Isolation Levels.
- Event-sourced systems take periodic snapshots of aggregates so they do not replay every event.
Interview questions and answers#
What is the Memento pattern? A behavioral pattern in which an originator captures its state in an opaque memento object that a caretaker stores, so the originator can be restored later without exposing its internals.
What are the roles? Originator (creates and restores from mementos), memento (holds state), caretaker (stores mementos, never looks inside).
How do you keep a memento opaque in Java? Expose a narrow public interface and implement it with a private nested class or record of the originator, as in this example.
Memento or Command for undo? Memento when computing an inverse is hard and state is small; Command when operations have simple inverses or state is large.
Summary#
- Memento saves and restores state without exposing it.
- Use a narrow public interface and a private implementation.
- Immutable state makes snapshots cheap; bound the history.
- Often combined with Command for undo.
Related topics#
- Command Pattern
- Prototype Pattern — copying state
- Modern Java: records
References#
- Gamma, Helm, Johnson, Vlissides. Design Patterns. Addison-Wesley, 1994. Memento, p. 283.
- Eric Freeman, Elisabeth Robson. Head First Design Patterns (2nd edition). O’Reilly, 2020. Appendix: Leftover patterns.