it decouples the object that sends a request from the one that executes it.
This lets you:
- Parameterize objects with operations (commands).
- Queue or log requests.
- Support undoable operations.
After a long walk through the city, you get to a nice restaurant and sit at the table by the window. A friendly waiter approaches you and quickly takes your order, writing it down on a piece of paper. The waiter goes to the kitchen and sticks the order on the wall. After a while, the order gets to the chef, who reads it and cooks the meal accordingly. The cook places the meal on a tray along with the order. The waiter discovers the tray, checks the order to make sure everything is as you wanted it, and brings everything to your table.
The paper order serves as a command. It remains in a queue until the chef is ready to serve it. The order contains all the relevant information required to cook the meal. It allows the chef to start cooking right away instead of running around clarifying the order details from you directly.

When to Use
- You want to parameterize objects with operations.
- You need to queue, schedule, or log operations.
- You want to implement undo/redo functionality.
- You want to decouple senders and receivers.
Real Life Examples
- GUI buttons or menu items triggering actions.
- Macro recording (a sequence of commands).
- Task queues and job scheduling.
- Undo/redo systems in editors.
- Transactional operations in databases.
Code Example
// Command interface
interface Command {
void execute();
}
// Receiver classes
class Light {
void turnOn() { System.out.println("Light is ON"); }
void turnOff() { System.out.println("Light is OFF"); }
}
class Stereo {
void turnOn() { System.out.println("Stereo is ON"); }
void turnOff() { System.out.println("Stereo is OFF"); }
}
// Concrete Commands
class LightOnCommand implements Command {
private Light light;
public LightOnCommand(Light light) {
this.light = light;
}
public void execute() {
light.turnOn();
}
}
class LightOffCommand implements Command {
private Light light;
public LightOffCommand(Light light) {
this.light = light;
}
public void execute() {
light.turnOff();
}
}
class StereoOnCommand implements Command {
private Stereo stereo;
public StereoOnCommand(Stereo stereo) {
this.stereo = stereo;
}
public void execute() {
stereo.turnOn();
}
}
class StereoOffCommand implements Command {
private Stereo stereo;
public StereoOffCommand(Stereo stereo) {
this.stereo = stereo;
}
public void execute() {
stereo.turnOff();
}
}
// Invoker
class RemoteControl {
private Command command;
public void setCommand(Command command) {
this.command = command;
}
public void pressButton() {
if (command != null) {
command.execute();
} else {
System.out.println("No command set!");
}
}
}
// Client
public class SmartHome {
public static void main(String[] args) {
RemoteControl remote = new RemoteControl();
Light livingRoomLight = new Light();
Stereo homeStereo = new Stereo();
Command lightOn = new LightOnCommand(livingRoomLight);
Command lightOff = new LightOffCommand(livingRoomLight);
Command stereoOn = new StereoOnCommand(homeStereo);
Command stereoOff = new StereoOffCommand(homeStereo);
// Turn on light
remote.setCommand(lightOn);
remote.pressButton(); // Output: Light is ON
// Turn off light
remote.setCommand(lightOff);
remote.pressButton(); // Output: Light is OFF
// Turn on stereo
remote.setCommand(stereoOn);
remote.pressButton(); // Output: Stereo is ON
// Turn off stereo
remote.setCommand(stereoOff);
remote.pressButton(); // Output: Stereo is OFF
}
}