design patterns · medium
Memento Pattern
Memento is a behavioral GoF pattern that captures and externalizes an object's internal state so the object can be restored to that state later, without violating encapsulation. It underpins undo/redo, checkpoints, and restore-previous-version features. The naive alternative — exposing all fields via getters so external code can save and reload them — wrecks encapsulation and couples everyone to the internals; Memento instead hands out an opaque token that only the owning object can read back. It has three roles: the Originator (e.g. Editor) whose state matters, which creates a memento in save() and reloads from one in restore(); the Memento, the snapshot itself, which presents a wide interface (full access) to its originator but a narrow interface (none) to everyone else — in C++ this dual interface is enforced with private state plus 'friend class Editor' so only the originator can read it; and the Caretaker (e.g. a history vector) which decides when to snapshot, stores the mementos, and hands them back for undo but never opens them. That role split preserves encapsulation while still letting history be stored externally. Pros: encapsulated undo/redo and checkpoints with a simple originator. The main con is memory: each snapshot copies state, so full snapshots get expensive for large objects or long histories — mitigate with incremental mementos that store only the diff, a capped history, or a replayable command log (event sourcing). Memento commonly pairs with Command to build undo stacks, where each command stores a memento to reverse itself.
Memento (behavioral): capture an object's internal state as an OPAQUE token so it can be restored later (undo/redo, checkpoints) WITHOUT exposing its internals. Three roles: Originator (creates/restores from the memento, wide interface), Memento (the snapshot, sealed to others — C++ uses 'friend'), Caretaker (stores/returns mementos via a narrow interface, never reads them). Cost: each snapshot copies state — mitigate with incremental diffs or a command log; pairs with Command for undo stacks.
The code
// Memento — capture an object's internal state so it can be RESTORED// later (undo/redo), WITHOUT exposing its internals (encapsulation).class Memento { // opaque snapshot friend class Editor; // only the Originator may read it std::string state_; explicit Memento(std::string s) : state_(std::move(s)) {}};
class Editor { // Originator: owns the real state std::string text_;
public: void type(const std::string& s) { text_ += s; } Memento save() const { return Memento(text_); } // snapshot void restore(const Memento& m) { text_ = m.state_; } // roll back};
// Caretaker: keeps the history but NEVER looks inside a Memento.std::vector<Memento> history;Editor ed;ed.type("hello");history.push_back(ed.save()); // checkpointed.type(" world");ed.restore(history.back()); // undo -> back to "hello"What this lesson walks through
- 01Intent — snapshot & restore, encapsulation intact
- 02Three roles — Originator, Memento, Caretaker
- 03Save pushes a snapshot; restore pops one
- 04Gotcha — snapshot cost & encapsulation
Memento captures an object's internal state so it can be restored later (undo), WITHOUT exposing that state to the outside world. The object hands out a sealed snapshot only it can read back — encapsulation stays intact.
See it animated — step by step, at your own pace
Unlock the full interactive walkthrough of Memento Pattern and 100+ animated C++ interview lessons.