All PatternsBehavioral
Memento
Captures and restores an object's internal state.
Undo systemsCheckpointsState snapshots
Understanding Memento
The Memento pattern captures and externalizes an object's internal state so it can be restored later, without violating encapsulation. Perfect for undo mechanisms.
How It Works
Originator
Editor
state: "Hello"
📦 Memento
"Hello"
Caretaker
History
M
Object has state
1
Object State
Object has internal state that can change.
1 / 4
Basic Implementation
Text editor with undo history:
main.go
Loading editor...
Real-World Example: Game Save System
Game with save slots:
main.go
Loading editor...