Skip to main content

Memento: Snapshot Ownership and History

What this concept is​

Memento separates the object that owns state (originator), the saved state (memento), and the history manager that stores it (caretaker). The originator defines what constitutes a complete restorable state. The caretaker need not understand or edit its internals. A snapshot can be a small immutable value; the pattern does not require a whole-object deep copy.

Why it matters here​

Command names an action; Memento captures state needed to restore it. A lossy operation such as clear cannot be undone from its output alone. However, saving only text when cursor and selection are public observations is incomplete. Define the restoration boundary before deciding what to save.

Concrete example​

L03 owns only immutable text. For text 'cat', replacing index 0 with 'b' gives 'bat'. Save before='cat' and after='bat'. Undo restores the first, redo the second. For a richer editor save (text, cursor, selection) under a declared invariant. If a snapshot contains a list of mutable shapes, copying the outer list still shares the shapes. Mutating a shape can silently mutate the apparent past.

An opaque restore token may include an originator identity and format version. Reject a token from another document before mutation. In languages without strict privacy, encapsulation is an API convention, not a security boundary. A memento should not be treated as an untrusted serialized object to execute.

Common confusion / misconception​

Deepcopy is not a universal solution: file handles, sockets, identity-sensitive objects and outside effects need their own contracts. Restoring a memory image does not reverse an email. A collaboration history also needs conflict semantics; restoring an old whole-document snapshot can erase someone else's edit.

How to use it​

List observations to restore, select immutable values/deltas/persistent versions, and document retention cost. For n full snapshots of a document of size s, a rough unshared storage bound is O(ns); shared structures need a reachability analysis. Decide whether a no-op edit enters history and whether a new edit drops redo. State what happens when capture or restoration fails.

Check yourself​

Why can a shallow snapshot be invalid even when its outer container is immutable? Which state is sufficient for L03 and insufficient for a graphical editor? What must happen before accepting a token belonging to another originator?

Mini drill or application​

P11–P12 and L03: trace append, clear, undo, undo, redo, new edit, redo. Then extend the model with cursor position and show the old snapshot's failure. Write a retention proposal and an external-effect counterexample. This extension is manual evidence, not a claim that the supplied kit models collaboration.

Read this only if stuck​

GoF book index, 077-memento-implementation.md and 066-command-implementation.md. Use the exact local locators in the reading guide; the links here open the book index.