Revision Notes/Design Patterns
Field Entry - Design Patterns
Memento Pattern
Captures and externally stores an object's internal state so it can be restored later — enabling undo/redo without exposing private implementation details.
Field Note - Real-world Analogy
“Like a time capsule. A person (Originator) writes a letter about their current life and seals it in a capsule (Memento). A librarian (Caretaker) stores the sealed capsule and can retrieve it later — but never opens or reads it. When the person wants to remember their past self, they open their own capsule. Nobody else can read it, preserving privacy (encapsulation).”
Field Observations - Key Topics Covered
The problem: you want to implement undo for a text editor. The naive approach is to save a copy of the entire editor state somewhere. But if you just expose the editor's internal fields (cursor position, text content, formatting) to the outside class that manages history, you've broken encapsulation — external code now knows about and depends on internal details.
4 more observations inside
Sign in to read every key point in full
Ready to go deeper?
Open the complete revision note.
Sign in to read every field observation in full, including a runnable code example and side-by-side comparisons with related patterns.
Read Full NotesSee Also in Design Patterns