memento
Capture and restore an object's internal state without violating encapsulation.
SKILL.mdskills/code-quality/memento/SKILL.md
GitHubMemento
Saves a snapshot of an object's state into a memento object so it can be restored later, without exposing the object's internals.
When to Use
- Undo/redo functionality
- Transactional operations that may need rollback
- Taking snapshots/checkpoints of an object's state
- When a direct copy of state fields would break encapsulation
How to Apply
- Identify the Originator — the object whose state needs saving
- Create the Memento class (or record) that holds a copy of the state (immutable, opaque to outsiders)
- Add
save(): Mementoandrestore(m: Memento): voidmethods to the Originator - Create a Caretaker that stores mementos and requests save/restore from the Originator
Example
class EditorMemento {
constructor(readonly content: string) {}
}
class Editor {
private content = '';
type(text: string) { this.content += text; }
save() { return new EditorMemento(this.content); }
restore(m: EditorMemento) { this.content = m.content; }
getContent() { return this.content; }
}
// Caretaker
const editor = new Editor();
const history: EditorMemento[] = [];
editor.type('Hello');
history.push(editor.save());
editor.type(' World');
console.log(editor.getContent()); // Hello World
editor.restore(history.pop()!);
console.log(editor.getContent()); // Hello