The Memento Pattern
archetype Social Roles → Object-Oriented Design
Categories: software-engineering
What It Brings
A memento is a keepsake — a pressed flower, a ticket stub, a photograph — an object kept specifically because it preserves a moment that would otherwise be lost. The GoF Memento pattern maps this sentimental practice onto software: an object’s internal state is captured in a token that can be stored and used later to restore the object to its former condition. The metaphor makes snapshot-and-restore feel intimate and purposeful rather than merely technical.
Key structural parallels:
- Mementos are created deliberately, not automatically — you choose which moments to preserve. The pattern requires an explicit request to capture state, not continuous recording. The metaphor frames this selectivity as natural: you don’t keep a souvenir of every moment, only the ones worth returning to.
- Mementos are opaque to everyone except their owner — a ticket stub means nothing to a stranger but everything to the person who was there. The pattern enforces encapsulation: the memento’s contents are accessible only to the originator that created it. The caretaker holds the memento but cannot inspect or modify it. The metaphor naturalizes this access restriction as privacy rather than arbitrary constraint.
- Mementos capture a moment, not a narrative — a photograph freezes an instant; it doesn’t explain what happened before or after. The pattern captures a snapshot of state at a point in time, without recording the sequence of operations that produced it. The metaphor helps developers understand that a memento is not a log or a history — it is a frozen moment.
- Mementos allow you to return to a previous state — looking at old photographs can transport you back. The pattern allows an object to be restored to the captured state. The sentimental metaphor makes “undo” feel like revisiting the past rather than debugging.
- Multiple mementos form a collection — people keep boxes of keepsakes, photo albums, scrapbooks. The pattern supports maintaining multiple snapshots for multi-level undo. The metaphor makes this accumulation feel organized rather than wasteful.
Where It Breaks
- Real mementos are partial and subjective; software mementos are complete — a photograph captures one angle, one instant, filtered through the photographer’s eye. A software memento captures the entire internal state of an object, perfectly and objectively. The sentimental metaphor imports incompleteness and nostalgia where the pattern actually delivers mechanical precision.
- Mementos don’t actually restore the past — looking at a wedding photo doesn’t make you newly married again. A software memento genuinely does restore an object to its previous state. The metaphor undersells the pattern’s power: real mementos evoke; software mementos resurrect.
- The emotional weight is absent — people keep mementos because of emotional attachment. Software stores state snapshots for functional reasons (undo, checkpointing, error recovery). Calling a state snapshot a “memento” imports sentimentality where none exists. Nobody feels nostalgic about a serialized object graph.
- Mementos are typically small; software mementos can be enormous — a keepsake fits in a drawer. A memento of a complex object might contain megabytes of serialized state. The metaphor suggests lightness and portability when the reality can be heavyweight and expensive. The cozy connotation of “keepsake” can mask serious memory costs.
- The caretaker role has no sentimental parallel — in the pattern, a separate caretaker object holds and manages mementos without understanding their contents. In real life, you keep your own mementos. The pattern’s three-role structure (originator, memento, caretaker) stretches beyond what the keepsake metaphor naturally supports.
- Mementos age and degrade; software snapshots don’t — old photographs yellow, memories fade. A serialized state snapshot is perfectly preserved indefinitely (barring schema changes, which are a different problem entirely). The metaphor imports entropy where the pattern is lossless.
Expressions
- “Save a memento” — capturing state, treating a snapshot as a keepsake worth preserving
- “Restore from the memento” — returning to a saved state, revisiting the past
- “The caretaker holds the memento” — delegating storage to a separate object, custody without comprehension
- “Memento stack” — a collection of saved states for undo, a box of keepsakes ordered by time
- “Externalize the object’s state” — the formal description, though the word “memento” adds warmth that “state snapshot” lacks
- “Checkpoint” — a near-synonym that uses a different metaphor (journey rather than sentiment), revealing how much work “memento” does to humanize the concept
Origin Story
The Memento pattern was codified by the Gang of Four in Design Patterns (1994). The name is notably more evocative than most GoF pattern names — where “Factory” and “Observer” are functional descriptions, “Memento” is poetic. The Latin root memento means “remember” (as in memento mori, “remember that you will die”). The choice to name a state-capture mechanism after keepsakes rather than calling it “Snapshot” or “Checkpoint” was a deliberate framing decision. It emphasizes the purpose (preserving something meaningful) over the mechanism (serializing state), and it lends emotional resonance to what is fundamentally a serialization pattern.
References
- Gamma, E. et al. Design Patterns: Elements of Reusable Object- Oriented Software (1994), Chapter 5: Behavioral Patterns
- Fowler, M. Patterns of Enterprise Application Architecture (2002) — discusses related concepts of snapshotting in enterprise contexts