Memento Pattern

The Memento is a behavioral design pattern that allows capturing and restoring an object's internal state without exposing its implementation details. It enables undo/redo operations by storing snapshots (mementos) of the object's state externally.

Key Principles

Structure

Python Example: Text Editor with Undo

from typing import List
from datetime import datetime

# Memento (stores state)
class EditorMemento:
    def __init__(self, content: str, timestamp: datetime):
        self._content = content          # Private state
        self._timestamp = timestamp

    def get_content(self) -> str:
        return self._content

    def get_timestamp(self) -> datetime:
        return self._timestamp

# Originator (creates/restores mementos)
class TextEditor:
    def __init__(self):
        self._content = ""

    def type(self, text: str):
        self._content += text
        print(f"Current text: {self._content}")

    def save(self) -> EditorMemento:
        print("Saving state...")
        return EditorMemento(self._content, datetime.now())

    def restore(self, memento: EditorMemento):
        self._content = memento.get_content()
        print(f"Restored to: {self._content} (from {memento.get_timestamp()})")

# Caretaker (manages mementos)
class History:
    def __init__(self):
        self._mementos: List[EditorMemento] = []

    def push(self, memento: EditorMemento):
        self._mementos.append(memento)

    def pop(self) -> EditorMemento:
        if self._mementos:
            return self._mementos.pop()
        raise IndexError("No saved states")

# Usage (Client Code)
if __name__ == "__main__":
    editor = TextEditor()
    history = History()

    editor.type("Hello ")
    history.push(editor.save())

    editor.type("World!")
    history.push(editor.save())

    editor.type(" This will be undone.")

    print("\nUndoing last change...")
    editor.restore(history.pop())

    print("\nUndoing another...")
    editor.restore(history.pop())

Output:

Current text: Hello 
Saving state...
Current text: Hello World!
Saving state...
Current text: Hello World! This will be undone.

Undoing last change...
Restored to: Hello World! (from 2025-... )
Undoing another...
Restored to: Hello  (from 2025-... )

When to Use Memento

Summary: Memento captures and externalizes an object's state in immutable snapshots, allowing restoration (e.g., undo) while preserving encapsulation.