Command Design Pattern
Definition:
The Command pattern is a behavioral design pattern in which a request is encapsulated as an object. This object contains all the information about the request, such as what action to perform and which receiver should perform it.
By encapsulating the behavior of the request into an object this pattern decouples the sender from receiver and enables runtime composition, undo/redo support, and request queuing.
Conceptually, a command can be viewed as an executable callback, but with richer capabilities such as logging, macro commands, and undo operations
Applicability:
— — — — — — —
1. When we want to decouple the request sender from request receiver
2. Parameterize objects with operations
3. Undo/redo
4. Queue or log operations
5. Implement user actions (menu items, buttons, API calls) where the invoker should not know which receiver performs the action.
Components:
— — — — — -
It has four components
1. Invoker — which invokes the command
2. Receiver — which executes the actual request
3. Command interface — contains the base method like execute()
4. Concrete implementations of the command interfaces — these contains the respective receiver objects which performs the actual action invoked by the sender
Implementation:
— — — — — — —
Will consider the Remote Control example here
Here we want to control the devices without coupling the remote to device logic
1. Command Interface
public interface Command{
void execute();
}
2. Receiver (considering we are operating the light using the remote)
Get Prassu’s stories in your inbox
Join Medium for free to get updates from this writer.
public class Light {
public void turnOn(){
//on operation of light
}
}
3. Concrete Implementations — LightCommand( contains has-a relationship with the receiver)
public class LightCommand implements Command{
private final Light light;
public LightCommand(Light light){
this.light = light;
}
@Override
public execute(){
light.turnOn();
}
}
4. Invoker (which is the remote control here)
public class RemoteControl{
private final Command command;
public void setCommand(Command command){
this.command = command;
}
public void turnOnLight(){
command.execute();
}
}
5. Client
public class Client{
public static void main(String [] args){
Light light = new Light();
Command command = new LightCommand(light);
RemoteControl remoteControl = new RemoteControl();
remoteControl.setCommand(command);
remoteControl.turnOnLight();
}
}
Similar Examples:
— — — — — — — — —
1. Java Runnable/Callable interfaces uses this command pattern
2. CQRS pattern leverages this design patterns where it represents the intent of state-changing operations, which aligns well with the Command pattern.
Advantages :
— — — — — -
1. Loose Coupling between the Sender and receiver objects
2. Open/Closed principle where we can able to modify it easily
3. Undo/redo support
4. We can create a Macro command where it follows a set of commands to fulfill the macro operation like ordering an item in ecommerce site which involves multiple operations to perform
Disadvantages:
— — — — — — —
1. Class Explosion — As we need to create classes for each operation
2. Overkill for simple scenarios
3. More objects to manage and it would be difficult for traceability in larger systems as we need to make sure which receiver should perform the actual request or command
Thanks for reading and let me know your suggestions in improving my articles



