Command is a behavioral design pattern that turns a request into a stand-alone object that contains all information about the request. This transformation lets you pass requests as a method arguments, delay or queue a request’s execution, and support undoable operations.
Applicability
- When you want to parameterise objects with operations to perform
- When you need to queue, log, or undo operations (with the help of Memento pattern)
- When you want to decouple the sender and receiver of a request
Approach
- Define a command interface that declares an
execute()
method to perform an action - Create concrete command classes that implement the
execute()
method and call the appropriate actions on the receiver - Define a receiver class that knows how to perform the operations associated with the command
- Create an invoker class that asks the command to carry out the request
- The client creates the command object, assigns the receiver, and passes the command to the invoker
Components
- Command
- An interface that declares an
execute()
operation.
- An interface that declares an
- ConcreteCommand
- Implements the Command interface and defines a link between a receiver object and an action.
- Receiver
- Knows how to perform the operations associated with carrying out a request.
- Invoker
- Asks the command to execute the request.
- Client
- Creates a concrete command and sets its receiver.
Example (Java)
Identify command pattern components
Command: Command
interface
Concrete Command: LightOnCommand
, LightOffCommand
Receiver: Light
Invoker: RemoteControl
Client: CommandPatternExample
Command
Interface to execute a command
public interface Command {
void execute();
}
Receiver
The object that knows how to perform the actual operation.
public class Light {
public void turnOn() {
System.out.println("The light is on");
}
public void turnOff() {
System.out.println("The light is off");
}
}
Concrete command
Concrete command classes that call methods on receiver.
public class LightOnCommand implements Command {
private Light light;
public LightOnCommand(Light light) {
this.light = light;
}
public void execute() {
light.turnOn();
}
}
public class LightOffCommand implements Command {
private Light light;
public LightOffCommand(Light light) {
this.light = light;
}
public void execute() {
light.turnOff();
}
}
Invoker
The invoker knows how to execute a command, but doesn’t know the specifics.
public class RemoteController {
private Command command;
public void setCommand(Command command) {
this.command = command;
}
public void pressButton() {
command.execute();
}
}
Client
The client assembles the command object, receiver, and invoker.
public class Demo {
public static void main(String[] args) {
Light light = new Light();
Command lightOn = new LightOnCommand(light);
Command lightOff = new LightOffCommand(light);
RemoteController remote = new RemoteController();
remote.setCommand(lightOn);
remote.pressButton(); //Output: The light is On
remote.setCommand(lightOff);
remote.pressButton(); //Output: The light is Off
}
}
Back to parent page: Behavioural Patterns
Design_Pattern Behavioural_Design_Patterns SOFT3202 Command_Pattern
Reference