Capture and externalize an object's internal state so it can be restored later. You need undo/redo. You want to keep state history without exposing
Memento
Overview
Capture and externalize an object's internal state so it can be restored later.
When to use
- You need undo/redo.
- You want to keep state history without exposing internals.
Java example
TypeScript example
Pros and cons
Pros:
- Enables undo/redo without exposing internals.
- Clear separation of state snapshots.
Cons:
- Memory usage can grow quickly.
- Snapshot creation may be expensive.
Common pitfalls
- Saving too frequently without limits.
- Storing large, deep object graphs.