BEHAVIORAL PATTERNS / OBJECT DESIGN BRIEF

Memento pattern

The Memento pattern captures an object's internal state as a snapshot (memento) so it can be restored later, without exposing that state to other objects.

IntermediatePhase 06 / Topic 9 of 10ResponsibilitiesCollaborationsExtensibility
01

Overview

The Memento pattern captures an object's internal state as a snapshot (memento) so it can be restored later, without exposing that state to other objects. The originator creates and restores mementos; a caretaker (history manager) stores them but cannot read or modify their contents.

Memento enables undo, checkpoints, versioning, and transactional rollback while preserving encapsulation. It is the snapshot-based alternative to Command's inverse operations, simpler to implement but more memory-hungry for large states.

A save point in a video game

The game saves your progress into a slot you cannot edit. If you fail, you reload the save and continue from exactly that state.

02

When to use it

  • Undo or rollback by restoring previous states.
  • Checkpoints in games, editors, or wizards.
  • State is complex to reverse operation by operation.
03

Where it shows up in interviews

Snapshot undo

Recognize it when: restore previous states.

  • Design a text editor with undo
  • Design a game with save points
  • Design a form wizard
04

Where it is used in real software

Database savepoints

SAVEPOINT and ROLLBACK TO restore transaction state to a checkpoint.

Redux time-travel

Stored immutable states let developer tools jump to any previous state.

VM snapshots

Hypervisors snapshot VM state for rollback.

05

Key terms

Originator
Object whose state is saved.
Memento
Opaque snapshot of the state.
Caretaker
Stores mementos; never inspects them.
Incremental memento
Stores only changes to save memory.
06

How it works, step by step

  1. 1
    Identify the state to save

    Only what is needed to restore.

  2. 2
    Create an immutable memento type

    Opaque to outsiders.

  3. 3
    Originator: save() and restore(m)

    Only it can read the memento.

  4. 4
    Caretaker stores a history

    Stack or list, with a cap.

  5. 5
    Restore on undo

    Pop and pass back to the originator.

07

Editor snapshots

Caretaker pushes a memento before each change

Step 1 / 4
ActionTextHistory (mementos)
type 'Hi'Hi['']
type ' there'Hi there['', 'Hi']
undoHi['']
undo(empty)[]

NOWAction: type 'Hi' | Text: Hi | History (mementos): ['']

Each undo restores an exact previous state without the caretaker knowing how text is stored.

08

Implementation

class EditorMemento {  // Opaque: fields are private and readonly; only Editor uses them via the accessor below  constructor(private readonly text: string, private readonly cursor: number) {}  /** @internal */ restoreInto(apply: (text: string, cursor: number) => void) { apply(this.text, this.cursor); }} class Editor {  private text = "";  private cursor = 0;  type(s: string) {    this.text = this.text.slice(0, this.cursor) + s + this.text.slice(this.cursor);    this.cursor += s.length;  }  save() { return new EditorMemento(this.text, this.cursor); }  restore(m: EditorMemento) { m.restoreInto((t, c) => { this.text = t; this.cursor = c; }); }  toString() { return this.text; }} class History {  private stack: EditorMemento[] = [];  constructor(private editor: Editor, private limit = 100) {}  checkpoint() { this.stack.push(this.editor.save()); if (this.stack.length > this.limit) this.stack.shift(); }  undo() { const m = this.stack.pop(); if (m) this.editor.restore(m); }} const editor = new Editor();const history = new History(editor);history.checkpoint(); editor.type("Hi");history.checkpoint(); editor.type(" there");history.undo(); // "Hi"
09

Complexity and performance

SaveO(state size)

Full snapshot.

History memoryO(snapshots x state)

Cap or use diffs.

10

Trade-offs

Simplicity vs memory

Snapshots are easy and reliable but costly for large states; commands with inverse operations or structural sharing reduce memory.

Encapsulation in some languages

Keeping mementos opaque is easier with nested classes (Java) than in JavaScript.

11

Variants and related techniques

Persistent data structures

Immutable structures share unchanged parts across versions.

Serialized mementos

Save to disk for crash recovery.

12

Common mistakes

  • Mementos sharing mutable collections with the originator.

    Fix: Copy collections into the snapshot.

  • Unbounded history.

    Fix: Limit size or compress older snapshots.

13

Interview questions

Memento vs Command for undo?

Memento stores full state snapshots and restores them, which is simple but memory heavy. Command stores the operation and knows how to reverse it, which is compact but requires correct inverse logic for every operation.

Why is the caretaker not allowed to read the memento?

To preserve encapsulation: only the originator understands its internal state, so the caretaker just stores and returns snapshots.

14

Practice problems

ProblemDifficultyWhat it trains
Form wizard with back buttonEasySnapshots.
Drawing canvas with capped undo historyMediumMemory limits.