设计模式学习_命令模式
Posted Leslie X徐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式学习_命令模式相关的知识,希望对你有一定的参考价值。
命令模式
概述
将"请求"封装成对象,以便使用不同的请求,队列或日志来参数化其他对象.命令模式也支持可撤销的操作.
项目
实现智能化遥控器控制不同的设备
采用命令模式, 从逻辑上将遥控器的类和各设备的类解偶.
命令接口
//----------------命令接口------------------
class Command{
public:
virtual ~Command(){} //虚析构
virtual void execute()=0;
};
无效指令
class NoCommand:
public Command
{
public:
NoCommand() :Command() {}
void execute(){}
};
实现遥控器
//---------------实现遥控器----------------
class RemoteControl{
Command* onCommands[7];
Command* offCommands[7];
public:
RemoteControl(){
Command *noCommand = new NoCommand();
for(int i=0; i<7; ++i){
onCommands[i] = noCommand;
offCommands[i] = noCommand;
}
//不加上noCommand指针指向未知会段错误
}
void setCommand(int slot, Command *onCommand, Command *offCommand){
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
void onButtonPushed(int slot){
onCommands[slot]->execute();
}
void offButtonPushed(int slot){
offCommands[slot]->execute();
}
};
实现命令
灯设备和命令
//灯设备
class Light{
string light;
public:
Light(){}
Light(string light){this->light = light;}
void on(){cout<<light<<" is on\\n";}
void off(){cout<<light<<" is off\\n";}
};
//开关灯
class LightOnCommand:
public Command
{
Light light;
public:
LightOnCommand(Light &light){
this->light = light;
}
void execute(){
this->light.on();
}
};
class LightOffCommand:
public Command
{
Light light;
public:
LightOffCommand(Light &light){
this->light = light;
}
void execute(){
this->light.off();
}
};
音响设备和命令
//音响设备
class Stereo{
string stereo;
public:
Stereo(){}
Stereo(string stereo){this->stereo = stereo;}
void on(){cout<<stereo<<" is on\\n";}
void setCd(){cout<<"insert CD\\n";}
void setVolume(int vol){cout<<"set vol: "<<vol<<endl;}
void off(){cout<<"Stereo is off\\n";}
};
//开关音响
class StereoOnWithCDCommand:
public Command
{
Stereo stereo;
public:
StereoOnWithCDCommand(Stereo &stereo){
this->stereo = stereo;
}
void execute(){
this->stereo.on();
this->stereo.setCd();
this->stereo.setVolume(11);
}
};
class StereoOffCommand:
public Command
{
Stereo stereo;
public:
StereoOffCommand(Stereo &stereo){
this->stereo = stereo;
}
void execute(){
this->stereo.off();
}
};
主函数测试
int main(int argc, char **argv)
{
//实例化遥控器
RemoteControl remotectl;
//实例化各个设备及其开关命令
Light livingRoomLight("livingRoomLight");
LightOnCommand livingRoomLightOn(livingRoomLight);
LightOffCommand livingRoomLightOff(livingRoomLight);
Light dinningRoomLight("dinningRoomLight");
LightOnCommand dinningRoomLightOn(dinningRoomLight);
LightOffCommand dinningRoomLightOff(dinningRoomLight);
Stereo livingRoomStereo("livingRoomStereo");
StereoOnWithCDCommand stereoOn(livingRoomStereo);
StereoOffCommand stereoOff(livingRoomStereo);
//设置遥控器各槽对应指令
remotectl.setCommand(0,&livingRoomLightOn,&livingRoomLightOff);
remotectl.setCommand(1,&dinningRoomLightOn,&dinningRoomLightOff);
remotectl.setCommand(2,&stereoOn,&stereoOff);
char OnOrOff;
int slot;
while(1){
cout<<"请输入命令: ";
cin>>OnOrOff>>slot;
if(OnOrOff=='n') remotectl.onButtonPushed(slot);
if(OnOrOff=='f') remotectl.offButtonPushed(slot);
if(OnOrOff=='q') break;
}
return 0;
}
输出
请输入命令: n0
livingRoomLight is on
请输入命令: n1
dinningRoomLight is on
请输入命令: n2
livingRoomStereo is on
insert CD
set vol: 11
请输入命令: n3
请输入命令: f0
livingRoomLight is off
请输入命令: f1
dinningRoomLight is off
请输入命令: f2
Stereo is off
请输入命令: q
以上是关于设计模式学习_命令模式的主要内容,如果未能解决你的问题,请参考以下文章