Skip to main content

Command

Command encapsulates a request as an object so operations can be queued, logged, parameterized, and undone independently of their invoker. It underpins undo/redo, task queues, and transactional or recoverable behavior.

Type
Behavioral
When to Use
Undo Redo, Queue Requests, Decouple Invoker Receiver

Command is a behavioral pattern that turns a request into a stand-alone object containing everything needed to perform an action later. By packaging the receiver, the method to call, and its arguments into one object, you decouple the code that issues a request from the code that fulfills it. This makes operations first-class values you can store, pass around, queue, log, and reverse.

How It Works

The pattern defines a command interface with a single execute method, and often an undo method. A concrete command holds a reference to a receiver and implements execute by calling the receiver's operations. An invoker triggers commands without knowing their details; it might be a button, a scheduler, or a queue consumer. A client creates concrete commands and wires them to receivers.

Because commands share an interface, the invoker can hold a history list, replay it, or roll it back. Macro commands compose several commands into one. Persisting commands enables transactional or recoverable behavior, since the log of commands can be replayed after a crash.

When to Use It

Use Command when you need undo and redo, when you want to queue or schedule operations, or when you must log changes so they can be reapplied for recovery. It also suits decoupling UI actions from business logic, building task queues and job runners, and implementing transactional behavior where a set of operations is treated as a unit.

Trade-offs

The pattern adds a class per operation, which can multiply types in a large system. Reliable undo requires each command to capture enough state to reverse itself, which is non-trivial and often pairs with the Memento pattern. Serializing commands for persistence demands stable, versioned formats. For simple, one-off calls the indirection is unjustified, and a direct method call or a function reference is clearer.

Related Patterns

Memento stores the state a command needs to support undo without exposing the receiver's internals. Strategy also wraps behavior in objects, but it represents an algorithm choice rather than a deferred request. Chain of Responsibility can pass a command along handlers until one processes it. Composite combines several commands into a macro command treated as one. Command queues are a building block of event-driven systems, job and task runners, and the write side of CQRS, where each user intent is captured as a command, validated, and applied to produce events. The pattern also separates the moment a request is created from the moment it executes, which is exactly what background processing, retries, and scheduling require.

Example

A text editor represents each edit as a command: InsertText, DeleteText, FormatText. The editor pushes executed commands onto a history stack and a redo stack. Pressing undo pops the last command and calls its undo, then moves it to the redo stack so the action can be reapplied. A scripting feature replays a recorded list of commands as a macro, all without the editor core knowing the specifics of each action. The same structure scales to distributed systems: a deployment pipeline models each step as a command with a compensating undo, a smart-home hub turns button presses into queued device commands, and a remote API records incoming commands to a durable log so they can be replayed to rebuild state after a failure.