C ++ design mode: Command pattern)

Source: Internet
Author: User

Definition:

In command mode, the "request" is encapsulated into an object so that different requests, queues, or logs can be used to parameterize other objects. In command mode, unrecoverable operations are also supported. The command mode decouples the "Action requestor" from the "action performer" object.

Scenario:

We want to design a remote control that can control the lights in the bedroom and kitchen by pressing the control button above, and control the sound switch in the bedroom. Remote control is our "Action requestor" in time, while lights and audios are our "action performer ". When we press a switch on the remote control, the remote control can send related commands to our designated home appliance. In this process, the remote control is decoupled from the home appliance. We can add, modify, or delete other home appliance control functions through settings without modifying the remote control code.

Class diagram:

 

The C ++ code is as follows:

#include <iostream>
#include <vector>
using namespace std;

class Command
{
public:
virtual void execute() = 0;
};

class NoCommand : public Command
{
public:
void execute() {};
};

class Light
{
public:
Light(string location);
void on();
void off();
private:
string m_sLocation;
};

class LightOffCommand : public Command
{
public:
LightOffCommand(string location):m_Light(location) {}
void execute();
private:
Light m_Light;
};

class LightOnCommand : public Command
{
public:
LightOnCommand(string location):m_Light(location) {}
void execute();
private:
Light m_Light;
};

class Stereo
{
public:
Stereo(string location);
void on();
void off();
void setCD();
void setDVD();
void setRadio();
void setVolume(int volume);
private:
string m_sLocation;
};

class StereoOnWithCDCommand : public Command
{
public:
StereoOnWithCDCommand(string location):m_Stereo(location) {}

void execute();
private:
Stereo m_Stereo;
};

class StereoOffCommand : public Command
{
public:
StereoOffCommand(string location):m_Stereo(location) {}

void execute();
private:
Stereo m_Stereo;
};

class RemoteControl
{
public:
RemoteControl();
~RemoteControl();
void setCommand(int slot, Command* pOnCommand, Command* pOffCommand);
void onButtonWasPushed(int slot);
void offButtonWasPushed(int slot);
private:
vector<Command*> m_OnCommands;
vector<Command*> m_OffCommands;
};

Light::Light(string location)
{
m_sLocation = location;
}

void Light::on()
{
printf("%s light is on\n",m_sLocation.c_str());
}

void Light::off()
{
printf("%s light is off\n",m_sLocation.c_str());
}

void LightOffCommand::execute()
{
m_Light.off();
}

void LightOnCommand::execute()
{
m_Light.on();
}

Stereo::Stereo(string location)
{
m_sLocation = location;
}

void Stereo::on()
{
printf("%s stereo is on\n",m_sLocation.c_str());
}

void Stereo::off()
{
printf("%s stereo is off\n",m_sLocation.c_str());
}

void Stereo::setCD()
{
printf("%s stereo is set for CD input\n",m_sLocation.c_str());
}

void Stereo::setDVD()
{
printf("%s stereo is set for DVD input\n",m_sLocation.c_str());
}

void Stereo::setRadio()
{
printf("%s stereo is set for Radio\n",m_sLocation.c_str());
}

void Stereo::setVolume(int volume)
{
printf("%s Stereo volume set to %d\n",m_sLocation.c_str(),volume);
}

void StereoOnWithCDCommand::execute()
{
m_Stereo.on();
m_Stereo.setCD();
m_Stereo.setVolume(11);
}

void StereoOffCommand::execute()
{
m_Stereo.off();
}

RemoteControl::RemoteControl()
{
for (int i = 0; i < 7; i++)
{
Command* noCommandOn = new NoCommand();
m_OnCommands.push_back(noCommandOn);
Command* noCommandOff = new NoCommand();
m_OffCommands.push_back(noCommandOff);
}
}

RemoteControl::~RemoteControl()
{
for (int i = 0; i < 7; i++)
{
delete m_OnCommands.at(i);
delete m_OffCommands.at(i);
}
m_OnCommands.clear();
m_OffCommands.clear();
}

void RemoteControl::setCommand(int slot, Command* pOnCommand, Command* pOffCommand)
{
delete m_OnCommands.at(slot);
m_OnCommands.at(slot) = pOnCommand;
delete m_OffCommands.at(slot);
m_OffCommands.at(slot) = pOffCommand;
}

void RemoteControl::onButtonWasPushed(int slot)
{
m_OnCommands.at(slot)->execute();
}

void RemoteControl::offButtonWasPushed(int slot)
{
m_OffCommands.at(slot)->execute();
}

int main()
{
RemoteControl remoteControl;
LightOffCommand* pLivingRoomLightOff = new LightOffCommand("Living Room");
LightOffCommand* pKitchenLightOff = new LightOffCommand("Kitchen");
LightOnCommand* pLivingRoomLightOn = new LightOnCommand("Living Room");
LightOnCommand* pKitchenLightOn = new LightOnCommand("Kitchen");
StereoOnWithCDCommand* pStereoOnWithCD = new StereoOnWithCDCommand("Living Room");
StereoOffCommand* pStereoOff = new StereoOffCommand("Living Room");

remoteControl.setCommand(0,pLivingRoomLightOn,pLivingRoomLightOff);
remoteControl.setCommand(1,pKitchenLightOn,pKitchenLightOff);
remoteControl.setCommand(2,pStereoOnWithCD,pStereoOff);

remoteControl.onButtonWasPushed(0);
remoteControl.offButtonWasPushed(0);
remoteControl.onButtonWasPushed(1);
remoteControl.offButtonWasPushed(1);
remoteControl.onButtonWasPushed(2);
remoteControl.offButtonWasPushed(2);
return 0;
}

 

The running result is as follows:

Living room light is on

Living room light is off

Kitchen light is on

Kitchen light is off

Living room stereo is on

Living room stereo is set for CD Input

Living room stereo volume set to 11

Living room stereo is off

 

Reference books: head first design model

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.