Back to Articles
📄
design patternmemento patternundo redosoftware architecturestate managementencapsulationcommand pattern

What Is the Memento Pattern and How Can It Help?

Capture an object's state and restore it later without exposing internals, enabling reliable undo/redo. The pattern isolates snapshot handling, keeping the originator safe from external tampering while balancing memory costs.

What Is the Memento Pattern and How Can It Help?

The Memento pattern captures an object's state so it can later be restored without exposing the object's internal representation.

The Three Roles

  • Originator: creates and restores snapshots.
  • Memento: stores an opaque snapshot.
  • Caretaker: keeps snapshots but does not interpret them.
class TextEditor {
  private content = '';

  type(text: string) { this.content += text; }
  save(): EditorMemento { return new EditorMemento(this.content); }
  restore(snapshot: EditorMemento) { this.content = snapshot.state(); }
}

class EditorMemento {
  constructor(private readonly value: string) {}
  state() { return this.value; }
}

The exact visibility rules can be stricter in languages with nested access control; the important idea is that the caretaker should not edit the snapshot.
If the caretaker were allowed to modify the snapshot, it could corrupt the originator’s internal invariants, defeating the purpose of encapsulation.

This ensures that the internal workings of the originator remain encapsulated and safe from external interference, preventing misuse or unintended modifications.

Undo and Redo

Two stacks can hold snapshots before and after changes:

class History {
  private undoStack: EditorMemento[] = [];
  private redoStack: EditorMemento[] = [];

  undo(editor: TextEditor) {
    const previous = this.undoStack.pop();
    if (!previous) return false;
    this.redoStack.push(editor.save());
    editor.restore(previous);
    return true;
  }
}

This mechanism allows users to revert to previous states seamlessly, which is particularly useful for text editors and other applications where frequent undos and redoes are necessary.

A complete implementation must define when snapshots are taken and clear redo history after a new branch of edits.

Where It Fits

Memento is useful for text editors, graphics tools, configuration rollback, game saves, and workflow checkpoints.
It ensures that users can safely revert to previous states without losing work or encountering data corruption.

However, it becomes less attractive when state is very large, snapshots are extremely frequent, or state must be shared across processes.
In these cases, alternative strategies like delta storage or command‑based undo might be more suitable.

For large or distributed systems, delta storage, event sourcing, or command‑based undo may be more appropriate.
Each alternative has its own complexity, and choosing the right approach depends on the specific requirements and constraints of the system.

Memento vs Command

Pattern Stores Undo approach
Memento State snapshot Restore a previous state
Command Action and parameters Execute a compensating action

The Memento pattern stores a complete state snapshot and relies on restoring a previous state when an undo operation is requested.
In contrast, the Command pattern records actions and parameters and executes a compensating action to undo changes.

Both patterns have their strengths and are chosen based on the specific needs of the application.
Memento is ideal for situations where the entire state needs to be preserved, while Command is better suited for scenarios where actions can be undone through compensating operations.

The Bottom Line

Memento protects encapsulation while providing rollback.
Its cost is storage: every snapshot consumes memory or persistence space.
Therefore, it is essential to limit the history and measure the state size before adopting the Memento pattern.
This ensures that the pattern is used efficiently and does not lead to unnecessary resource consumption.

Sources & References

  1. [1]
  2. [2]
  3. [3]

Comments (0)

Loading comments...