#include using namespace std; // Command Interface class Command{ public: virtual void execute() = 0; }; // Receiver Class class Light{ public: void on() { cout << "The light is on\n"; } void off() { cout << "The light is off\n"; } }; // Command for turning on the light class LightOnCommand:public Command{ Light *pLight; public: LightOnCommand(Light& light) : pLight(&light) {} void execute(){ pLight->on(); } }; // Command for turning off the light class LightOffCommand : public Command { Light *pLight; public: LightOffCommand(Light& light) : pLight(&light) {} void execute(){ pLight->off(); } }; // Invoker - Stores the ConcreteCommand object class RemoteControl { Command *pCmd; public: void setCommand(Command& cmd){ pCmd = &cmd; } void buttonPressed() { pCmd->execute(); } }; // The client int main() { // Receiver Light light; // concrete Command objects LightOnCommand lightOn(light); LightOffCommand lightOff(light); // invoker objects RemoteControl controller; // execute (Invocation) controller.setCommand(lightOn); controller.buttonPressed(); controller.setCommand(lightOff); controller.buttonPressed(); return 0; }