🎨 Design Patterns (Gang of Four)
Beginner
What is the Memento design pattern?
Answer
The Memento pattern captures and externalizes an object's internal state so it can be restored later, without violating encapsulation. Three roles are involved: the Originator (the object whose state is saved), the Memento (a snapshot of the state), and the Caretaker (stores and manages mementos but never inspects their contents). Text editors implementing undo/redo and database transactions (savepoints) are prime examples. The critical constraint is that only the originator can read the memento's contents, preserving its encapsulation from the caretaker.