Command
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
The Problem
You need to support undo/redo, operation queuing, macro recording, or deferred execution. Calling methods directly gives you no way to reverse, store, or replay operations.
Structure
Execution Walkthrough
Create Command
Client creates a ConcreteCommand object, binding it to a Receiver (the object that actually does the work).
Pass to Invoker
execute()
Push to history
undo()
Code Comparison
Full C++ Implementation
Participants
Where it is used
Text editors
Every edit is a Command stored on a stack. Ctrl+Z pops and calls undo(); Ctrl+Y re-executes.
EDA tools
Schematic edits are Command objects. The tool replays or rolls back an entire design session.
Job schedulers
Tasks are Command objects enqueued for worker threads. Failed tasks call execute() again for retry.
Macro recording
IDE macros record each action as a Command sequence; playback calls execute() deterministically.