🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 模式定义:       命令模式将“请求”封装成对象,以便使用不同的请求、队列或者日志来参数化其他对象。命令模式也支持可撤销的操作。       命令对象将动作和接受者包进对象中,这个对象只暴露一个execute()方法。       当需要将发出请求的对象和执行请求的对象解耦的时候,使用命令模式。 ## 模式结构: ![](https://box.kancloud.cn/2016-08-30_57c5458f5e3ea.jpg) ## 举例:       遥控器上有一个插槽,可以放上不同的装置,然后用按钮控制。我们这里放置电灯,并有开和关按钮。可以命令模式实现。 ## UML设计: ![](https://box.kancloud.cn/2016-08-30_57c5458fc8b65.jpg)       其中,RemoteControl为遥控器,LightOnCommand为开灯请求对象,LightOffCommand为关灯请求对象,他们继承自基类Command,这样设计可以使插槽在以后防止其他的装置。 ## 编程实现及执行结果: ~~~ #include <iostream> using namespace std; //电灯类 class Light { public: void on() { cout << "Light on !" << endl; } void off() { cout << "Light off !" << endl; } }; //命令类 class Command { public: virtual void execute(){} }; //具体命令类 class LigthOnCommand : public Command { public: LigthOnCommand(Light* lig):light(lig){} //execute方法 void execute() { light->on(); } private: Light* light; }; class LigthOffCommand : public Command { public: LigthOffCommand(Light* lig):light(lig){} void execute() { light->off(); } private: Light* light; }; //遥控器类 class RemoteControl { public: void setCommand(Command* command) { slot = command; } void buttonOn() { slot->execute(); } private: Command* slot; }; //客户代码 int main() { RemoteControl lightOnControl; RemoteControl lightOffControl; Command* onCommand = new LigthOnCommand(new Light()); Command* offCommand = new LigthOffCommand(new Light()); lightOnControl.setCommand(onCommand); lightOffControl.setCommand(offCommand); lightOnControl.buttonOn(); lightOffControl.buttonOn(); return 0; } ~~~ 执行结果: **Lighton !** **Lightoff !** **请按任意键继续. . .**