设计模式19-Command(命令)模式-智能家居
在软件构建过程中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”。但在某些场合——比如需要对行为进行“记录、撤销/重做(undo/redo)、事务”等处理,这种无法抵御变化的紧耦合是不合适的。在这种情况下,如何将“行为请求者”与“行为实现者”解耦?
这种情况下就可以使用命令模式,命令模式即将一个请求(行为)封装为一个对象,从而使你可用不同的请求对客户端进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。
UML图:
举个例子,当前智能家居很火热,假设我们要设计一个手机app,可以通过按动上边的控制按钮控制卧室和厨房的灯,还能控制卧室中的音响的开关。app即是“动作的请求者”,而灯和音响就是“动作的执行者”。当我们按动app上的某个开关后,app就可以把相关的指令发送到我们的指定的家电上。这之中app和家电之间是解耦的,我们完全可以通过设置,添加、修改或删除其它的家电控制功能,而不需要修改app的代码。
代码如下:
#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) {
m_sLocation = location;
}
void on() {
printf("%s light is on\n", m_sLocation.c_str());
}
void off() {
printf("%s light is off\n", m_sLocation.c_str());
}
private:
string m_sLocation;
};
class LightOffCommand : public Command
{
public:
LightOffCommand(string location) :m_Light(location) {}
void execute() {
m_Light.off();
}
private:
Light m_Light;
};
class LightOnCommand : public Command
{
public:
LightOnCommand(string location) :m_Light(location) {}
void execute() {
m_Light.on();
}
private:
Light m_Light;
};
class Stereo
{
public:
Stereo(string location) {
m_sLocation = location;
}
void on() {
printf("%s stereo is on\n", m_sLocation.c_str());
}
void off() {
printf("%s stereo is off\n", m_sLocation.c_str());
}
void setCD() {
printf("%s stereo is set for CD input\n", m_sLocation.c_str());
}
void setDVD() {
printf("%s stereo is set for DVD input\n", m_sLocation.c_str());
}
void setRadio() {
printf("%s stereo is set for Radio\n", m_sLocation.c_str());
}
void setVolume(int volume) {
printf("%s Stereo volume set to %d\n", m_sLocation.c_str(), volume);
}
private:
string m_sLocation;
};
class StereoOnWithCDCommand : public Command
{
public:
StereoOnWithCDCommand(string location) :m_Stereo(location) {}
void execute() {
m_Stereo.on();
m_Stereo.setCD();
m_Stereo.setVolume(11);
}
private:
Stereo m_Stereo;
};
class StereoOffCommand : public Command
{
public:
StereoOffCommand(string location) :m_Stereo(location) {}
void execute() {
m_Stereo.off();
}
private:
Stereo m_Stereo;
};
class RemoteControl
{
public:
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() {
for (int i = 0; i < 7; i++)
{
delete m_OnCommands.at(i);
delete m_OffCommands.at(i);
}
m_OnCommands.clear();
m_OffCommands.clear();
}
void 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 onButtonWasPushed(int slot) {
m_OnCommands.at(slot)->execute();
}
void offButtonWasPushed(int slot) {
m_OffCommands.at(slot)->execute();
}
private:
vector<Command*> m_OnCommands;
vector<Command*> m_OffCommands;
};
int main()
{
RemoteControl remoteControl;
LightOffCommand* pLivingRoomLightOff = new LightOffCommand("卧室");
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;
}
运行结果:
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
推荐阅读:江苏生活网