Head First设计模式C++实现-Command模式

xiaoxiao2021-03-01  13

定义:

命令模式将“请求”封装成对象,以便试用不同的请求,队列或者日志来参数化其他对象。命令模式也可支持可撤销的操作。

UML图:

程序实现:

/************************************************************************/ /* Command.h */ /************************************************************************/ /** * @file Command.h * @brief Command模式的实现代码 * @author wlq_729@163.com * http://blog.csdn.net/rabbit729 * @version 1.0 * @date 2009-02-22 */ #ifndef COMMAND_H_ #define COMMAND_H_ #include <iostream> #include <vector> #include <string> using namespace std; class Command { public: virtual string Execute() = 0; virtual string GetType() = 0; }; class CeillingFan { public: CeillingFan(string location); string High(); string Medium(); string Low(); string Off(); int GetSpeed(); protected: private: string location; int level; public: static const int HIGH = 2; static const int MEDIUM = 1; static const int LOW = 0; }; class CeillingFanOffCommand : public Command { public: CeillingFanOffCommand(CeillingFan* ceilingFan); string Execute(); string GetType(); private: CeillingFan* ceilingFan; }; class CeillingFanOnCommand : public Command { public: CeillingFanOnCommand(CeillingFan* ceilingFan); string Execute(); string GetType(); private: CeillingFan* ceilingFan; }; class GarageDoor { public: GarageDoor(string location); string Up(); string Down(); string Stop(); string LightOn(); string LightOff(); private: string location; }; class GarageDoorDownCommand : public Command { public: GarageDoorDownCommand(GarageDoor* garageDoor); string Execute(); string GetType(); private: GarageDoor* garageDoor; }; class GarageDoorUpCommand : public Command { public: GarageDoorUpCommand(GarageDoor* garageDoor); string Execute(); string GetType(); private: GarageDoor* garageDoor; }; class Hottub { public: Hottub(); bool On(); bool Off(); string BubblesOn(); string BubblesOff(); string JetsOn(); string JetsOff(); int SetTemperature(int temperature); string Heat(); string Cool(); private: bool on; int temperature; }; class HottubOffCommand : public Command { public: HottubOffCommand(Hottub* hottub); string Execute(); string GetType(); private: Hottub* hottub; }; class HottubOnCommand : public Command { private: Hottub* hottub; public: HottubOnCommand(Hottub* hottub); string Execute(); string GetType(); }; class Light { public: Light(string location); string On(); string Off(); private: string location; }; class LightOffCommand : public Command { public: LightOffCommand(Light* light); string Execute(); string GetType(); private: Light* light; }; class LightOnCommand : public Command { public: LightOnCommand(Light* light); string Execute(); string GetType(); private: Light* light; }; class LivingroomLightOffCommand : public Command { public: LivingroomLightOffCommand(Light* light); string Execute(); string GetType(); private: Light* light; }; class LivingroomLightOnCommand : public Command { public: LivingroomLightOnCommand(Light* light); string Execute(); string GetType(); private: Light* light; }; class NoCommand : public Command { public: NoCommand() { } string Execute() { return NULL; } string GetType() { return "NoCommand"; } }; class Stereo { public: Stereo(string location); string On(); string Off(); string SetCD(); string SetDVD(); string SetRadio(); string SetVolume(int volume); private: string location; }; class StereoOffCommand : public Command { public: StereoOffCommand(Stereo* stereo); string Execute(); string GetType(); private: Stereo* stereo; }; class StereoOnWithCDCommand : public Command { public: StereoOnWithCDCommand(Stereo* stereo); string Execute(); string GetType(); private: Stereo* stereo; }; class SimpleRemoteControl { public: SimpleRemoteControl() { } void SetCommand(Command* command); void ButtonWasPressed(); private: Command* slot; }; class Remote { public: Remote(); void SetCommand(int solt, Command* onCommand, Command* offCommand); void OnButtonWasPushed(int slot); void OffButtonWasPushed(int slot); string toString(); private: vector<Command*> onCommands; vector<Command*> offCommands; }; #endif /************************************************************************/ /* Command.cpp */ /************************************************************************/ /** * @file Command.cpp * @brief Command模式的实现代码 * @author wlq_729@163.com * http://blog.csdn.net/rabbit729 * @version 1.0 * @date 2009-02-22 */ #include "Command.h" CeillingFan::CeillingFan(string location) { this->location = location; } string CeillingFan::High() { level = HIGH; string temp = location + " ceiling fan is on high"; cout<<temp<<endl; return temp; } string CeillingFan::Medium() { level = MEDIUM; string temp = location + " ceiling fan is on medium"; cout<<temp<<endl; return temp; } string CeillingFan::Low() { level = LOW; string temp = location + " ceiling fan is on low"; cout<<temp<<endl; return temp; } string CeillingFan::Off() { level = 0; string temp = location + " ceiling fan is off"; cout<<temp<<endl; return temp; } int CeillingFan::GetSpeed() { return level; } CeillingFanOffCommand::CeillingFanOffCommand(CeillingFan* ceilingFan) { this->ceilingFan = ceilingFan; } string CeillingFanOffCommand::Execute() { //cout<<ceilingFan->Off()<<endl; return ceilingFan->Off(); } string CeillingFanOffCommand::GetType() { return "CeillingFanOffCommand"; } CeillingFanOnCommand::CeillingFanOnCommand(CeillingFan* ceilingFan) { this->ceilingFan = ceilingFan; } string CeillingFanOnCommand::Execute() { //cout<<ceilingFan->High()<<endl; return ceilingFan->High(); } string CeillingFanOnCommand::GetType() { return "CeillingFanOnCommand"; } GarageDoor::GarageDoor(string location) { this->location = location; } string GarageDoor::Up() { cout<<"Garage door is up"<<endl; return "Garage door is up"; } string GarageDoor::Down() { cout<<"Garage door is down"<<endl; return "Garage door is down"; } string GarageDoor::Stop() { cout<<"Garage door movement is stopped"<<endl; return "Garage door movement is stopped"; } string GarageDoor::LightOn() { cout<<"Garage door light is on"<<endl; return "Garage door light is on"; } string GarageDoor::LightOff() { cout<<"Garage door light is off"<<endl; return "Garage door light is off"; } GarageDoorDownCommand::GarageDoorDownCommand(GarageDoor* garageDoor) { this->garageDoor = garageDoor; } string GarageDoorDownCommand::Execute() { //cout<<garageDoor->Down()<<endl; return garageDoor->Down(); } string GarageDoorDownCommand::GetType() { return "GarageDoorDownCommand"; } GarageDoorUpCommand::GarageDoorUpCommand(GarageDoor* garageDoor) { this->garageDoor = garageDoor; } string GarageDoorUpCommand::Execute() { return garageDoor->Up(); } string GarageDoorUpCommand::GetType() { return "GarageDoorUpCommand"; } Hottub::Hottub() { } bool Hottub::On() { on = true; return on; } bool Hottub::Off() { on = false; return on; } string Hottub::BubblesOn() { if (on) { cout<<"Hottub is bubbling!"<<endl; return "Hottub is bubbling!"; } return NULL; } string Hottub::BubblesOff() { if (on) { cout<<"Hottub is not bubbling!"<<endl; return "Hottub is not bubbling!"; } return NULL; } string Hottub::JetsOn() { if (on) { cout<<"Hottub jets are on!"<<endl; return "Hottub jets are on!"; } return NULL; } string Hottub::JetsOff() { if (on) { cout<<"Hottub jets are off!"<<endl; return "Hottub jets are off!"; } return NULL; } int Hottub::SetTemperature(int temperature) { this->temperature = temperature; return temperature; } string Hottub::Heat() { temperature = 105; cout<<"Hottub is heating to a steaming 105 degrees"<<endl; return "Hottub is heating to a steaming 105 degrees"; } string Hottub::Cool() { temperature = 98; cout<<"Hottub is cooling to 98 degrees"<<endl; return "Hottub is cooling to 98 degrees"; } HottubOffCommand::HottubOffCommand(Hottub *hottub) { this->hottub = hottub; } string HottubOffCommand::Execute() { bool temp = hottub->Off(); char buf[3]; memset(buf, 0, 3); sprintf(buf, "%d", temp); cout<<buf<<endl; return buf; } string HottubOffCommand::GetType() { return "HottubOffCommand"; } HottubOnCommand::HottubOnCommand(Hottub *hottub) { this->hottub = hottub; } string HottubOnCommand::Execute() { string temp = hottub->On() + "/n" + hottub->Heat() + "/n" + hottub->BubblesOn(); cout<<temp<<endl; return temp; } string HottubOnCommand::GetType() { return "HottubOnCommand"; } Light::Light(string location) { this->location = location; } string Light::On() { string temp = location + "light is on"; cout<<temp<<endl; return temp; } string Light::Off() { string temp = location + "light is off"; cout<<temp<<endl; return temp; } LightOffCommand::LightOffCommand(Light* light) { this->light = light; } string LightOffCommand::Execute() { //cout<<light->Off()<<endl; return light->Off(); } string LightOffCommand::GetType() { return "LightOffCommand"; } LightOnCommand::LightOnCommand(Light* light) { this->light = light; } string LightOnCommand::Execute() { return light->On(); } string LightOnCommand::GetType() { return "LightOnCommand"; } LivingroomLightOffCommand::LivingroomLightOffCommand(Light* light) { this->light = light; } string LivingroomLightOffCommand::Execute() { //cout<<light->Off()<<endl; return light->Off(); } string LivingroomLightOffCommand::GetType() { return "LivingroomLightOffCommand"; } LivingroomLightOnCommand::LivingroomLightOnCommand(Light* light) { this->light = light; } string LivingroomLightOnCommand::Execute() { //cout<<light->On()<<endl; return light->On(); } string LivingroomLightOnCommand::GetType() { return "LivingroomLightOnCommand"; } Stereo::Stereo(string location) { this->location = location; } string Stereo::On() { string temp = location + " stereo is on"; cout<<temp<<endl; return temp; } string Stereo::Off() { string temp = location + " stereo is off"; cout<<temp<<endl; return temp; } string Stereo::SetCD() { string temp = location + " stereo is set for CD input"; cout<<temp<<endl; return temp; } string Stereo::SetDVD() { string temp = location + " stereo is set for DVD input"; cout<<temp<<endl; return temp; } string Stereo::SetRadio() { string temp = location + " stereo is set for Radio"; cout<<temp<<endl; return temp; } string Stereo::SetVolume(int volume) { char buf[5]; memset(buf, 0, 5); string temp = location + " Stereo volume set to " + itoa(volume, buf, 10); cout<<temp<<endl; return temp; } StereoOffCommand::StereoOffCommand(Stereo* stereo) { this->stereo = stereo; } string StereoOffCommand::Execute() { //cout<<stereo->Off()<<endl; return stereo->Off(); } string StereoOffCommand::GetType() { return "StereoOffCommand"; } StereoOnWithCDCommand::StereoOnWithCDCommand(Stereo* stereo) { this->stereo = stereo; } string StereoOnWithCDCommand::Execute() { string temp = stereo->On() + "/n" + stereo->SetCD() + "/n" + stereo->SetVolume(11); cout<<temp<<endl; return temp; } string StereoOnWithCDCommand::GetType() { return "StereoOnWithCDCommand"; } void SimpleRemoteControl::SetCommand(Command* command) { this->slot = command; } void SimpleRemoteControl::ButtonWasPressed() { slot->Execute(); } Remote::Remote() { Command* noCommand = new NoCommand(); for (int i = 0; i < 7; i++) { onCommands.push_back(noCommand); offCommands.push_back(noCommand); } } void Remote::SetCommand(int solt, Command* onCommand, Command* offCommand) { onCommands[solt] = onCommand; offCommands[solt] = offCommand; } void Remote::OnButtonWasPushed(int slot) { onCommands[slot]->Execute(); } void Remote::OffButtonWasPushed(int slot) { offCommands[slot]->Execute(); } string Remote::toString() { char buf[3]; memset(buf, 0, 3); string sb; sb.append("/n----------- Remote Control ----------/n"); for (int i = 0; i < onCommands.size(); i++) { itoa(i, buf, 10); string num(buf); sb.append("[slot " + num + "]" + onCommands[i]->GetType() + " " + offCommands[i]->GetType() + "/n"); } return sb; } /************************************************************************/ /* test.cpp */ /************************************************************************/ /** * @file test.cpp * @brief Command模式的实现代码 * @author wlq_729@163.com * http://blog.csdn.net/rabbit729 * @version 1.0 * @date 2009-02-22 */ #include "Command.h" void main(void) { /* SimpleRemoteControl* remote = new SimpleRemoteControl(); Light* light = new Light("light"); GarageDoor* garageDoor = new GarageDoor("test"); LightOnCommand* lightOn = new LightOnCommand(light); GarageDoorUpCommand* garageUp = new GarageDoorUpCommand(garageDoor); remote->SetCommand(lightOn); remote->ButtonWasPressed(); remote->SetCommand(garageUp); remote->ButtonWasPressed();*/ Remote* remote = new Remote(); Light* livingRoomLight = new Light("Living Room"); Light* kitchenLight = new Light("Kitchen"); CeillingFan* ceilingFan = new CeillingFan("Living Room"); GarageDoor* garageDoor = new GarageDoor(""); Stereo* stereo = new Stereo("Living Room"); LightOnCommand* livingRoomLightOn = new LightOnCommand(livingRoomLight); LightOffCommand* livingRoomLightOff = new LightOffCommand(livingRoomLight); LightOnCommand* kitchenLightOn = new LightOnCommand(kitchenLight); LightOffCommand* kitchenLightOff = new LightOffCommand(kitchenLight); CeillingFanOnCommand* ceilingFanOn = new CeillingFanOnCommand(ceilingFan); CeillingFanOffCommand* ceilingFanOff = new CeillingFanOffCommand(ceilingFan); GarageDoorUpCommand* garageDoorUp = new GarageDoorUpCommand(garageDoor); GarageDoorDownCommand* garageDoorDown = new GarageDoorDownCommand(garageDoor); StereoOnWithCDCommand* stereoOnWithCD = new StereoOnWithCDCommand(stereo); StereoOffCommand* stereoOff = new StereoOffCommand(stereo); remote->SetCommand(0, livingRoomLightOn, livingRoomLightOff); remote->SetCommand(1, kitchenLightOn, kitchenLightOff); remote->SetCommand(2, ceilingFanOn, ceilingFanOff); remote->SetCommand(3, stereoOnWithCD, stereoOff); cout<<remote->toString()<<endl; remote->OnButtonWasPushed(0); remote->OffButtonWasPushed(0); remote->OnButtonWasPushed(1); remote->OffButtonWasPushed(1); remote->OnButtonWasPushed(2); remote->OffButtonWasPushed(2); remote->OnButtonWasPushed(3); remote->OffButtonWasPushed(3); }

程序输出:

----------- Remote Control ----------[slot 0]LightOnCommand LightOffCommand[slot 1]LightOnCommand LightOffCommand[slot 2]CeillingFanOnCommand CeillingFanOffCommand[slot 3]StereoOnWithCDCommand StereoOffCommand[slot 4]NoCommand NoCommand[slot 5]NoCommand NoCommand[slot 6]NoCommand NoCommand

Living Roomlight is onLiving Roomlight is offKitchenlight is onKitchenlight is offLiving Room ceiling fan is on highLiving Room ceiling fan is offLiving Room Stereo volume set to 11Living Room stereo is set for CD inputLiving Room stereo is onLiving Room stereo is onLiving Room stereo is set for CD inputLiving Room Stereo volume set to 11Living Room stereo is off请按任意键继续. . .

相关资源:Head First设计模式官方源码
转载请注明原文地址: https://www.6miu.com/read-4050269.html

最新回复(0)