JS Command Pattern
Encapsulate operations as objects to support undo, queue, and logging
When to Use
- You need undo/redo functionality
- Operations should be queued, deferred, or logged
- You want to decouple the sender of an operation from its executor
Instructions
- Define a command interface with
execute()and optionallyundo()methods. - Each command class captures the receiver and parameters needed to perform (and reverse) the operation.
- Store executed commands in a history stack for undo support.
- The invoker (button, menu, keyboard shortcut) calls
command.execute()without knowing the implementation.
class AddTextCommand {
constructor(editor, text) {
this.editor = editor;
this.text = text;
this.prevContent = null;
}
execute() {
this.prevContent = this.editor.content;
this.editor.content += this.text;
}
undo() {
this.editor.content = this.prevContent;
}
}
const editor = { content: 'Hello' };
const history = [];
const cmd = new AddTextCommand(editor, ' World');
cmd.execute();
history.push(cmd);
console.log(editor.content); // 'Hello World'
history.pop().undo();
console.log(editor.content); // 'Hello'
Details
The Command pattern turns function calls into first-class objects. This makes operations serializable, loggable, and reversible. Redux actions are a functional variant of this pattern.
Trade-offs:
- More boilerplate than a direct function call
- Memory cost for storing command history
- Undo logic can be complex for operations with side effects
When NOT to use:
- When undo/redo is not needed and operations are one-shot
- For simple event handlers — a plain callback is sufficient
Source
https://patterns.dev/javascript/command-pattern
Process
- Read the instructions and examples in this document.
- Apply the patterns to your implementation, adapting to your specific context.
- Verify your implementation against the details and edge cases listed above.
Harness Integration
- Type: knowledge — this skill is a reference document, not a procedural workflow.
- No tools or state — consumed as context by other skills and agents.
Success Criteria
- The patterns described in this document are applied correctly in the implementation.
- Edge cases and anti-patterns listed in this document are avoided.