Sitemap
CodeDesignHub

CodeDesignHub

Command Design Pattern

4 min readMay 11, 2026

The Command design pattern is a behavioural design pattern that focuses on encapsulating a request or an operation as an object. It allows you to separate the requester of an action from the object that actually performs the action.

In the Command pattern, there are four main components:

1. Command: This is the interface or abstract class that declares the common methods for executing the command, typically including an execute() method. Concrete command classes implement this interface and provide the necessary logic for executing a specific command.

2. Concrete Command: These are the concrete implementations of the Command interface. Each concrete command represents a specific operation or request. It encapsulates all the information required to execute that command.

3. Invoker: The invoker is responsible for invoking the commands. It receives requests from clients and delegates the execution to the appropriate command object without having knowledge of the specific command details. The invoker only knows how to execute the execute() method defined in the Command interface.

4. Receiver: The receiver is the object that performs the actual work associated with a command. It knows how to carry out the requested operations. The command object interacts with the receiver to execute the requested actions.

Steps to implement Command Design Pattern

To implement the Command design pattern, you can follow these steps:

1. Identify the commands: Determine the set of actions or operations that need to be encapsulated as commands. These can be any actions that you want to perform in a decoupled and flexible manner.

Get CodeDesignHub’s stories in your inbox

Join Medium for free to get updates from this writer.

2. Define the Command interface: Create an interface or an abstract class that declares the common methods for executing a command. Typically, this includes a method like execute().

3. Implement concrete command classes: Create concrete classes that implement the Command interface. Each concrete command represents a specific operation or action. Implement the execute() method in each concrete command with the logic to perform the associated action.

4. Define the receiver: Identify the object that will actually perform the requested actions. This object should have the necessary methods to carry out the actions associated with each command.

5. Instantiate the receiver and commands: Create an instance of the receiver object and instances of the concrete command classes. Associate each command instance with the appropriate receiver object.

6. Implement the invoker: Create an invoker class that will receive requests and invoke the corresponding commands. The invoker should have a method to set or associate a command with the invoker object.

7. Invoke the commands: Use the invoker to execute the commands by calling the execute() method on the associated command object. The invoker doesn’t need to know the specific details of the command or the receiver; it just triggers the command execution.

By following these steps, you will have implemented the Command design pattern in your code. Remember that the pattern allows for loose coupling between the sender and the receiver, as the invoker only interacts with the command interface and does not directly invoke the receiver’s methods. This promotes flexibility and extensibility, as new commands can be easily added without modifying existing code, and you can support additional features like logging, queuing, or undoing operations.

Class Diagram

Press enter or click to view image in full size

Source Code

#include <iostream>
#include <string>


// A simple C++ program to demonstrate
// implementation of Command Pattern using
// a remote control example.

// An interface for command
class Command
{
public:
virtual void execute() = 0;
};

// Light class and its corresponding command

// Receiver Object
class Light
{
public:

void on()
{
std::cout << "Light is on";
}
void off()
{
std::cout << "Light is off";
}
};

//Concrete Command Object
class LightOnCommand: public Command
{
Light* light;
public:

// The constructor is passed the light it
// is going to control.
LightOnCommand(Light* light)
{
this->light = light;
}

void execute()
{
light->on();
}
};

//Concrete Command Object
class LightOffCommand : public Command
{
Light* light;
public:

LightOffCommand(Light* light)
{
this->light = light;
}
void execute()
{
light->off();
}
};

//Receiver
// MusicSystem and its command classes
class MusicSystem
{
public:

void on()
{
std::cout << "MusicSystem is on";
}
void off()
{
std::cout << "MusicSystem is off";
}
void setCD()
{
std::cout << "MusicSystem is set " <<
"for CD input";
}
void setDVD()
{
std::cout << "MusicSystem is set" <<
" for DVD input";
}
void setRadio()
{
std::cout << "MusicSystem is set" <<
" for Radio";
}
void setVolume(int volume)
{
// code to set the volume
std::cout << "MusicSystem volume set to " << volume;
}
};

//Concrete Command Object
class MusicSystemOffCommand :public Command
{
MusicSystem* musicSystem;
public:

MusicSystemOffCommand(MusicSystem* musicSystem)
{
this->musicSystem = musicSystem;
}
void execute()
{
musicSystem->off();
}
};

//Concrete Command Object
class MusicSystemOnWithCDCommand :public Command
{
MusicSystem* musicSystem;
public:
MusicSystemOnWithCDCommand(MusicSystem* musicSystem)
{
this->musicSystem = musicSystem;
}
void execute()
{
musicSystem->on();
musicSystem->setCD();
musicSystem->setVolume(11);
}
};


//Invoker
class RemoteControl
{
Command* slot; // only one button

public:

RemoteControl ()
{
}

void setCommand(Command* command)
{
// set the command the remote will
// execute
slot = command;
}

void buttonWasPressed()
{
slot->execute();
}
};


int main()
{
RemoteControl * remote =
new RemoteControl ();
Light* light = new Light();
MusicSystem* musicSystem = new MusicSystem();

// we can change command dynamically
remote->setCommand(new
LightOnCommand(light));
remote->buttonWasPressed();
remote->setCommand(new
MusicSystemOnWithCDCommand(musicSystem));
remote->buttonWasPressed();
remote->setCommand(new
MusicSystemOffCommand(musicSystem));
remote->buttonWasPressed();
}

--

--