Encapsulate a request as an object, allowing parameterization and queuing. You need undo/redo. You want to queue or log operations. Pros: Suppor
Command
Overview
Encapsulate a request as an object, allowing parameterization and queuing.
When to use
- You need undo/redo.
- You want to queue or log operations.
Java example
TypeScript example
Pros and cons
Pros:
- Supports undo/redo and logging.
- Decouples invoker from receiver.
Cons:
- Many small classes.
- Indirection can be heavy for simple actions.
Common pitfalls
- Not capturing all necessary state for undo.
- Creating commands for trivial operations.