Memento Pattern

Capture and restore an object's internal state without breaking encapsulation. A runnable Java 21 drawing canvas with checkpoints, narrow and wide memento interfaces using a private record, bounded history, immutability for cheap snapshots, and Memento versus Command for undo.

On this page

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.
Memento — class diagram of the example
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 : stores
Save and restore
sequenceDiagram
    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.

DrawingCanvas — originator and memento View source
/** 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);
    }
}
History — caretaker View source
/** 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(); }
}
Output View source
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=red

Step-by-step walkthrough#

  1. Narrow and wide interfaces. The public Memento interface exposes only a label. The private Snapshot record holds the real state. Outside DrawingCanvas, nobody can name Snapshot, so nobody can read its fields. This is how Java expresses GoF’s “wide interface for the originator only” (in C++ GoF used friend).
  2. Restoring checks the type. A memento from a different originator is rejected.
  3. Immutable state makes snapshots cheap. shapes is an immutable list, so save() can store the reference without copying. Every draw() creates a new list, leaving old snapshots untouched. With mutable state, save() must deep-copy, see Prototype.
  4. The caretaker bounds memory. History keeps at most two checkpoints and drops the oldest.

Memento versus Command for undo#

Memento (snapshot)Command (inverse operation)
StoresThe state before a changeThe operation and what is needed to reverse it
UndoRestore the snapshotExecute the inverse
MemoryProportional to state sizeProportional to the change
Works whenInverses 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#

AdvantagesDisadvantages
Preserves encapsulation: state stays privateSnapshots of large state consume memory and time
Simplifies the originator: history management lives elsewhereCaretakers do not know how big mementos are
Easy to implement checkpoints, undo and rollbackIn 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.StateEdit stores 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.

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.