Chinaunix首页 | 论坛 | 博客
  • 博客访问: 167718
  • 博文数量: 60
  • 博客积分: 15
  • 博客等级: 民兵
  • 技术积分: 638
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-26 10:59
个人简介

喜欢coding,因为那是一件伟大的事情,是将无生命的IC赋予灵魂的过程,让我拥有了和上帝一样的成就感。(w1c2g3@163.com)

文章分类

全部博文(60)

文章存档

2017年(7)

2016年(41)

2015年(1)

2014年(4)

2013年(7)

我的朋友

分类: C/C++

2016-10-30 22:48:36

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




  1. #include <iostream>
  2. #include <string>


  3. using namespace std;


  4. struct Command {
  5.     virtual void execute() = 0;
  6.     virtual void undo() = 0;
  7. };

  8. struct NoCommand : public Command {
  9.     void execute() {};
  10.     void undo() {};
  11. };

  12. struct RemoteControl {
  13.     Command *onCommands[7];
  14.     Command *offCommands[7];
  15.     Command *undoCommand;

  16.     RemoteControl() {
  17.         //onCommands = new NoCommand[7];
  18.         //offCommands = new NoCommand[7];
  19.         Command *noCommand = new NoCommand();

  20.         for (int i = 0; i < 7; ++i)
  21.         {
  22.             onCommands[i] = noCommand;
  23.             offCommands[i] = noCommand;
  24.         }
  25.         undoCommand = noCommand;
  26.     }

  27.     void setCommand(int slot, Command *onCommand, Command *offCommand) {
  28.         onCommands[slot] = onCommand;
  29.         offCommands[slot] = offCommand;
  30.     }

  31.     void onButtonWasPushed(int slot) {
  32.         onCommands[slot]->execute();
  33.         undoCommand = onCommands[slot];
  34.     }

  35.     void offButtonWasPushed(int slot) {
  36.         offCommands[slot]->execute();
  37.         undoCommand = offCommands[slot];
  38.     }

  39.     void undoButtonWasPushed() {
  40.         undoCommand->undo();
  41.     }

  42.     string toString() {
  43.         //string stringBuffer = new string;
  44.     }
  45. };

  46. struct Light {
  47. public:
  48.     Light(string mname) {
  49.         name = mname;
  50.     }
  51.     on() {
  52.         cout << name << "Light on" << endl;
  53.     }
  54.     off() {
  55.         cout << name << "Light off" << endl;
  56.     }
  57. private:
  58.     string name;
  59. };

  60. struct LightOnCommand : public Command {
  61.     Light *light;

  62.     LightOnCommand(Light *mlight) {
  63.         light = mlight;
  64.     }

  65.     virtual void execute() {
  66.         light->on();
  67.     }

  68.     virtual void undo() {
  69.         light->off();
  70.     }
  71. };

  72. struct LightOffCommand : public Command {
  73.     Light *light;

  74.     LightOffCommand(Light *mlight) {
  75.         light = mlight;
  76.     }

  77.     virtual void execute() {
  78.         light->off();
  79.     }

  80.     virtual void undo() {
  81.         light->on();
  82.     }
  83. };

  84. struct CeilingFan {
  85. public:
  86.     CeilingFan(string mname) {
  87.         name = mname;
  88.     }
  89.     on() {
  90.         cout << name << "CeilingFan on" << endl;
  91.     }
  92.     off() {
  93.         cout << name << "CeilingFan off" << endl;
  94.     }
  95. private:
  96.     string name;
  97. };

  98. struct CeilingFanOnCommand : public Command {
  99.     CeilingFan *ceilingFan;

  100.     CeilingFanOnCommand(CeilingFan *mceilingFan) {
  101.         ceilingFan = mceilingFan;
  102.     }

  103.     virtual void execute() {
  104.         ceilingFan->on();
  105.     }

  106.     virtual void undo() {
  107.         ceilingFan->off();
  108.     }
  109. };

  110. struct CeilingFanOffCommand : public Command {
  111.     CeilingFan *ceilingFan;

  112.     CeilingFanOffCommand(CeilingFan *mceilingFan) {
  113.         ceilingFan = mceilingFan;
  114.     }

  115.     virtual void execute() {
  116.         ceilingFan->off();
  117.     }

  118.     virtual void undo() {
  119.         ceilingFan->on();
  120.     }
  121. };

  122. struct MacroCommand : public Command {
  123. public:
  124.     MacroCommand(Command **mCommand, int cnt) {
  125.         commands = mCommand;
  126.         commands_cnt = cnt;
  127.     }

  128.     virtual void execute() {
  129.         for (int i = 0; i < commands_cnt; ++i) {
  130.             commands[i]->execute();
  131.         }
  132.     }

  133.     virtual void undo() {
  134.         for (int i = 0; i < commands_cnt; ++i) {
  135.             commands[i]->undo();
  136.         }
  137.     }
  138. private:
  139.     Command **commands;
  140.     int commands_cnt;
  141. };

  142. // Remote loader
  143. int main(int argc, char **argv)
  144. {
  145.     RemoteControl *remoteControl = new RemoteControl();

  146.     Light *livingRoomLight = new Light("Living Room");
  147.     Light *kitchenLight = new Light("Kitchen");
  148.     CeilingFan *ceilingFan = new CeilingFan("Living Room");

  149.     LightOnCommand *livingRoomLightOn =
  150.         new LightOnCommand(livingRoomLight);
  151.     LightOffCommand *livingRoomLightOff =
  152.         new LightOffCommand(livingRoomLight);

  153.     LightOnCommand *kitchenLightOn =
  154.         new LightOnCommand(kitchenLight);
  155.     LightOffCommand *kitchenLightOff =
  156.         new LightOffCommand(kitchenLight);

  157.     CeilingFanOnCommand *ceilingFanOn =
  158.         new CeilingFanOnCommand(ceilingFan);
  159.     CeilingFanOffCommand *ceilingFanOff =
  160.         new CeilingFanOffCommand(ceilingFan);

  161.     remoteControl->setCommand(0, livingRoomLightOn, livingRoomLightOff);
  162.     remoteControl->setCommand(1, kitchenLightOn, kitchenLightOff);
  163.     remoteControl->setCommand(2, ceilingFanOn, ceilingFanOff);

  164.     //print log
  165.     remoteControl->onButtonWasPushed(0);
  166.     remoteControl->offButtonWasPushed(0);
  167.     remoteControl->undoButtonWasPushed();
  168.     remoteControl->onButtonWasPushed(1);
  169.     remoteControl->offButtonWasPushed(1);
  170.     remoteControl->onButtonWasPushed(2);
  171.     remoteControl->offButtonWasPushed(2);
  172.     remoteControl->undoButtonWasPushed();
  173.     
  174.     Command *partyOn[] = {livingRoomLightOn, kitchenLightOn, ceilingFanOn};
  175.     Command *partyOff[] = {livingRoomLightOff, kitchenLightOff, ceilingFanOff};
  176.     MacroCommand *partyOnCommand = new MacroCommand(partyOn, 3);
  177.     MacroCommand *partyOffCommand = new MacroCommand(partyOff, 3);
  178.     remoteControl->setCommand(3, partyOnCommand, partyOffCommand);
  179.     remoteControl->onButtonWasPushed(3);
  180.     remoteControl->offButtonWasPushed(3);
  181.     remoteControl->undoButtonWasPushed();

  182.     return 0;
  183. }

阅读(637) | 评论(0) | 转发(0) |
0

上一篇:单件模式

下一篇:适配器模式

给主人留下些什么吧!~~