Skip to main content

🧩 Memento Pattern

✅ Intent

  • Save and restore an object's internal state without exposing its internal structure
  • Provide the ability to undo or rollback to a previous state

✅ Motivation

  • Useful when you want to save a snapshot of the system at a specific moment
  • Designed for full-state restoration, not just inverse actions

✅ When to Use

  • Form inputs, save features in games, autosave systems in editors
  • When state management is critical and you need reversible transitions

✅ Code Example

// Memento
class EditorMemento {
constructor(private readonly state: string) {}

getState(): string {
return this.state;
}
}

// Originator(状態を持つ)
class TextEditor {
private content = "";

type(text: string): void {
this.content += text;
}

getContent(): string {
return this.content;
}

createMemento(): EditorMemento {
return new EditorMemento(this.content);
}

restore(memento: EditorMemento): void {
this.content = memento.getState();
}
}

// Caretaker(履歴管理)
const editor = new TextEditor();
const history: EditorMemento[] = [];

editor.type("Hello ");
history.push(editor.createMemento());

editor.type("World");
history.push(editor.createMemento());

console.log(editor.getContent()); // Hello World

// Undo
history.pop(); // 現在の状態は捨てる
const last = history[history.length - 1];
editor.restore(last);

console.log(editor.getContent()); // Hello

✅ Explanation

This code uses the Memento pattern to save and restore the state of a text editor.
The TextEditor (originator) creates EditorMemento (mementos) to store its state and uses them to restore past content.

1. Overview of the Memento Pattern

  • Memento: Stores the internal state of the originator

    • Here: EditorMemento
  • Originator: Creates and restores mementos

    • Here: TextEditor
  • Caretaker: Manages the history of mementos

    • Here: A history list holds the saved mementos

2. Key Classes and Their Roles

  • EditorMemento

    • Holds a snapshot of the editor’s content
    • Exposes a getState() method for recovery
  • TextEditor

    • Originator that maintains current text
    • Has createMemento() to save state and restore(memento) to revert
  • Caretaker

    • Stores the mementos and handles when to save/restore
    • Here, the history array manages this role

3. UML Class Diagram

4. Benefits of the Memento Pattern

  • State Preservation: Saves and restores internal state without exposing implementation details
  • Encapsulation Friendly: Originator's internal data remains hidden from external classes
  • History Management: Enables clean implementation of undo/redo systems

This pattern is ideal when you need to capture full snapshots of an object's state and revert to previous versions.
It is particularly useful in applications like form editors, games, and any situation where rollback is critical.