Command Pattern Examples in C++
Problem statement: Design a system that demonstrates the use of the Command Pattern to decouple the sender and receiver of a request. The system should consist of several key components: Command, Concrete Command, Receiver, and Invoker.
Below is the implementation of the Command Pattern in C++:
C++
#include <iostream> // Receiver class Receiver { public : // Receiver class defines the action to be performed. void performAction() { std::cout << "Receiver is performing an action." << std::endl; } }; // Command interface class Command { public : // The execute method is declared in the Command // interface. virtual void execute() = 0; }; // Concrete Command class ConcreteCommand : public Command { private : Receiver& receiver; public : // ConcreteCommand takes a reference to a Receiver // object in its constructor. ConcreteCommand(Receiver& rec) : receiver(rec) { } // The execute method calls the action on the Receiver. void execute() { receiver.performAction(); } }; // Invoker class Invoker { private : Command* command; public : // The setCommand method allows setting the command to // be executed. void setCommand(Command* cmd) { command = cmd; } // The executeCommand method triggers the execution of // the command. void executeCommand() { command->execute(); } }; int main() { // Create a Receiver instance. Receiver receiver; // Create a ConcreteCommand, passing the Receiver to it. ConcreteCommand command(receiver); // Create an Invoker. Invoker invoker; // Set the command to be executed by the invoker. invoker.setCommand(&command); // Execute the command. invoker.executeCommand(); return 0; } |
Receiver is performing an action.
Problem statement: Design a system to simulate a remote control for various electronic devices. The system should allow users to turn these devices on and off using a remote control with multiple buttons. Implement the Command Pattern to achieve this functionality.
Below is the step by step process to implement above problem statement:
- Create an interface or base class Command that defines a execute method, which should be implemented by concrete commands.
- Implement concrete command classes, such as TurnOnCommand and TurnOffCommand, to encapsulate specific actions for turning electronic devices on and off.
- Create a receiver class ElectronicDevice for the electronic devices that need to be controlled. It should have methods to turn the device on and off.
- Design an Invoker class RemoteControl to manage and execute the commands. It should provide the following features:
- The ability to add commands to specific buttons on the remote control.
- A method to press a button, which triggers the associated command’s execution.
- Demonstrate the functionality by creating instances of electronic devices (e.g., TV, lights), concrete commands (turn on TV, turn off lights), and a remote control.
- Users should be able to press the buttons on the remote control to turn electronic devices on and off.
- Ensure that the system is flexible and extensible, allowing for easy addition of new electronic devices and commands without modifying existing code.
Below is the implementation of the above example:
C++
#include <iostream> #include <vector> // Receiver: Electronic Device class ElectronicDevice { private : std::string name; public : ElectronicDevice( const std::string& n) : name(n) { } void turnOn() { std::cout << name << " is now ON." << std::endl; } void turnOff() { std::cout << name << " is now OFF." << std::endl; } }; // Command interface class Command { public : virtual void execute() = 0; }; // Concrete Command: Turn on class TurnOnCommand : public Command { private : ElectronicDevice& device; public : TurnOnCommand(ElectronicDevice& dev) : device(dev) { } void execute() { device.turnOn(); } }; // Concrete Command: Turn off class TurnOffCommand : public Command { private : ElectronicDevice& device; public : TurnOffCommand(ElectronicDevice& dev) : device(dev) { } void execute() { device.turnOff(); } }; // Invoker: Remote Control class RemoteControl { private : std::vector<Command*> commands; public : void addCommand(Command* cmd) { commands.push_back(cmd); } void pressButton( int slot) { if (slot >= 0 && slot < commands.size()) { commands[slot]->execute(); } } }; int main() { // Create electronic devices ElectronicDevice tv("TV"); ElectronicDevice lights("Lights"); // Create commands for turning devices on and off TurnOnCommand turnOnTV(tv); TurnOffCommand turnOffTV(tv); TurnOnCommand turnOnLights(lights); TurnOffCommand turnOffLights(lights); // Create a remote control RemoteControl remote; // Set commands for remote buttons remote.addCommand(&turnOnTV); // Button 0: Turn on TV remote.addCommand(&turnOffTV); // Button 1: Turn off TV remote.addCommand( &turnOnLights); // Button 2: Turn on Lights remote.addCommand( &turnOffLights); // Button 3: Turn off Lights // Press buttons on the remote remote.pressButton(0); // Turn on TV remote.pressButton(3); // Turn off Lights remote.pressButton(1); // Turn off TV remote.pressButton(2); // Turn on Lights return 0; } |
TV is now ON. Lights is now OFF. TV is now OFF. Lights is now ON.
Command Pattern | C++ Design Patterns
The Command Pattern is a behavioral design pattern that focuses on encapsulating a request as an object, thereby decoupling the sender of the request from the receiver. This pattern allows you to parameterize objects with commands, delay or queue a request’s execution, and support undoable operations. It’s a fundamental pattern for implementing a wide range of functionality in software systems.
Important Topics for the Command Pattern in C++ Design Patterns
- key components of the Command Pattern
- Command Pattern Examples in C++
- Advantages of the Command Pattern in C++ Design Patterns
- Disadvantages of the Command Pattern in C++ Design Patterns
Contact Us