命令模式(Command Pattern)

 

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

 

实例:可编程遥控器,每个按钮可自定义设置各种家电命令,但家电厂商种类繁多,不同家电的命令不同,要使遥控器与命令分离

 

JAVA代码

  1. class Light
  2. {
  3.     String location;
  4.     int level;
  5.     public Light(String location)
  6.     {
  7.         this.location = location;
  8.     }
  9.     public void on()
  10.     {
  11.         level = 100;
  12.         System.out.println("Light is on");
  13.     }
  14.     public void off()
  15.     {
  16.         level = 0;
  17.         System.out.println("Light is off");
  18.     }
  19.     public void dim(int level)
  20.     {
  21.         this.level = level;
  22.         if (level == 0)
  23.         {
  24.             off();
  25.         }
  26.         else
  27.         {
  28.             System.out.println("Light is dimmed to " + level + "%");
  29.         }
  30.     }
  31.     public int getLevel()
  32.     {
  33.         return level;
  34.     }
  35. }
  36. class CeilingFan
  37. {
  38.     public static final int HIGH = 3;
  39.     public static final int MEDIUM = 2;
  40.     public static final int LOW = 1;
  41.     public static final int OFF = 0;
  42.     String location;
  43.     int speed;
  44.     public CeilingFan(String location)
  45.     {
  46.         this.location = location;
  47.     }
  48.     public void high()
  49.     {
  50.         // turns the ceiling fan on to high
  51.         speed = HIGH;
  52.         System.out.println(location + " ceiling fan is on high");
  53.     }
  54.     public void medium()
  55.     {
  56.         // turns the ceiling fan on to medium
  57.         speed = MEDIUM;
  58.         System.out.println(location + " ceiling fan is on medium");
  59.     }
  60.     public void low()
  61.     {
  62.         // turns the ceiling fan on to low
  63.         speed = LOW;
  64.         System.out.println(location + " ceiling fan is on low");
  65.     }
  66.     public void off()
  67.     {
  68.         // turns the ceiling fan off
  69.         speed = OFF;
  70.         System.out.println(location + " ceiling fan is off");
  71.     }
  72.     public int getSpeed()
  73.     {
  74.         return speed;
  75.     }
  76. }
  77. class Hottub
  78. {
  79.     boolean on;
  80.     int temperature;
  81.     public Hottub()
  82.     {
  83.     }
  84.     public void on()
  85.     {
  86.         on = true;
  87.     }
  88.     public void off()
  89.     {
  90.         on = false;
  91.     }
  92.     public void circulate()
  93.     {
  94.         if (on)
  95.         {
  96.             System.out.println("Hottub is bubbling!");
  97.         }
  98.     }
  99.     public void jetsOn()
  100.     {
  101.         if (on)
  102.         {
  103.             System.out.println("Hottub jets are on");
  104.         }
  105.     }
  106.     public void jetsOff()
  107.     {
  108.         if (on)
  109.         {
  110.             System.out.println("Hottub jets are off");
  111.         }
  112.     }
  113.     public void setTemperature(int temperature)
  114.     {
  115.         if (temperature > this.temperature)
  116.         {
  117.             System.out.println("Hottub is heating to a steaming " + temperature + " degrees");
  118.         }
  119.         else
  120.         {
  121.             System.out.println("Hottub is cooling to " + temperature + " degrees");
  122.         }
  123.         this.temperature = temperature;
  124.     }
  125. }
  126. class Stereo
  127. {
  128.     String location;
  129.     public Stereo(String location)
  130.     {
  131.         this.location = location;
  132.     }
  133.     public void on()
  134.     {
  135.         System.out.println(location + " stereo is on");
  136.     }
  137.     public void off()
  138.     {
  139.         System.out.println(location + " stereo is off");
  140.     }
  141.     public void setCD()
  142.     {
  143.         System.out.println(location + " stereo is set for CD input");
  144.     }
  145.     public void setDVD()
  146.     {
  147.         System.out.println(location + " stereo is set for DVD input");
  148.     }
  149.     public void setRadio()
  150.     {
  151.         System.out.println(location + " stereo is set for Radio");
  152.     }
  153.     public void setVolume(int volume)
  154.     {
  155.         // code to set the volume
  156.         // valid range: 1-11 (after all 11 is better than 10, right?)
  157.         System.out.println(location + " Stereo volume set to " + volume);
  158.     }
  159. }
  160. class TV
  161. {
  162.     String location;
  163.     int channel;
  164.     public TV(String location)
  165.     {
  166.         this.location = location;
  167.     }
  168.     public void on()
  169.     {
  170.         System.out.println(location + " TV is on");
  171.     }
  172.     public void off()
  173.     {
  174.         System.out.println(location + " TV is off");
  175.     }
  176.     public void setInputChannel()
  177.     {
  178.         this.channel = 3;
  179.         System.out.println(location + " TV channel is set for DVD");
  180.     }
  181. }
  182. interface Command
  183. {
  184.     public void execute();
  185.     public void undo();
  186. }
  187. class NoCommand implements Command
  188. {
  189.     public void execute() { }
  190.     public void undo() { }
  191. }
  192. class LightOnCommand implements Command
  193. {
  194.     Light light;
  195.     public LightOnCommand(Light light)
  196.     {
  197.         this.light = light;
  198.     }
  199.     public void execute()
  200.     {
  201.         light.on();
  202.     }
  203.     public void undo()
  204.     {
  205.         light.off();
  206.     }
  207. }
  208. class LightOffCommand implements Command
  209. {
  210.     Light light;
  211.     public LightOffCommand(Light light)
  212.     {
  213.         this.light = light;
  214.     }
  215.     public void execute()
  216.     {
  217.         light.off();
  218.     }
  219.     public void undo()
  220.     {
  221.         light.on();
  222.     }
  223. }
  224. class CeilingFanHighCommand implements Command
  225. {
  226.     CeilingFan ceilingFan;
  227.     int prevSpeed;
  228.     public CeilingFanHighCommand(CeilingFan ceilingFan)
  229.     {
  230.         this.ceilingFan = ceilingFan;
  231.     }
  232.     public void execute()
  233.     {
  234.         prevSpeed = ceilingFan.getSpeed();
  235.         ceilingFan.high();
  236.     }
  237.     public void undo()
  238.     {
  239.         switch (prevSpeed)
  240.         {
  241.             case CeilingFan.HIGH: ceilingFan.high(); break;
  242.             case CeilingFan.MEDIUM: ceilingFan.medium(); break;
  243.             case CeilingFan.LOW: ceilingFan.low(); break;
  244.             default: ceilingFan.off(); break;
  245.         }
  246.     }
  247. }
  248. class CeilingFanMediumCommand implements Command
  249. {
  250.     CeilingFan ceilingFan;
  251.     int prevSpeed;
  252.     public CeilingFanMediumCommand(CeilingFan ceilingFan)
  253.     {
  254.         this.ceilingFan = ceilingFan;
  255.     }
  256.     public void execute()
  257.     {
  258.         prevSpeed = ceilingFan.getSpeed();
  259.         ceilingFan.medium();
  260.     }
  261.     public void undo()
  262.     {
  263.         switch (prevSpeed)
  264.         {
  265.             case CeilingFan.HIGH: ceilingFan.high(); break;
  266.             case CeilingFan.MEDIUM: ceilingFan.medium(); break;
  267.             case CeilingFan.LOW: ceilingFan.low(); break;
  268.             default: ceilingFan.off(); break;
  269.         }
  270.     }
  271. }
  272. class CeilingFanLowCommand implements Command
  273. {
  274.     CeilingFan ceilingFan;
  275.     int prevSpeed;
  276.     public CeilingFanLowCommand(CeilingFan ceilingFan)
  277.     {
  278.         this.ceilingFan = ceilingFan;
  279.     }
  280.     public void execute()
  281.     {
  282.         prevSpeed = ceilingFan.getSpeed();
  283.         ceilingFan.low();
  284.     }
  285.     public void undo()
  286.     {
  287.         switch (prevSpeed)
  288.         {
  289.             case CeilingFan.HIGH: ceilingFan.high(); break;
  290.             case CeilingFan.MEDIUM: ceilingFan.medium(); break;
  291.             case CeilingFan.LOW: ceilingFan.low(); break;
  292.             default: ceilingFan.off(); break;
  293.         }
  294.     }
  295. }
  296. class CeilingFanOffCommand implements Command
  297. {
  298.     CeilingFan ceilingFan;
  299.     int prevSpeed;
  300.     public CeilingFanOffCommand(CeilingFan ceilingFan)
  301.     {
  302.         this.ceilingFan = ceilingFan;
  303.     }
  304.     public void execute()
  305.     {
  306.         prevSpeed = ceilingFan.getSpeed();
  307.         ceilingFan.off();
  308.     }
  309.     public void undo()
  310.     {
  311.         switch (prevSpeed)
  312.         {
  313.             case CeilingFan.HIGH: ceilingFan.high(); break;
  314.             case CeilingFan.MEDIUM: ceilingFan.medium(); break;
  315.             case CeilingFan.LOW: ceilingFan.low(); break;
  316.             default: ceilingFan.off(); break;
  317.         }
  318.     }
  319. }
  320. class HottubOnCommand implements Command
  321. {
  322.     Hottub hottub;
  323.     public HottubOnCommand(Hottub hottub)
  324.     {
  325.         this.hottub = hottub;
  326.     }
  327.     public void execute()
  328.     {
  329.         hottub.on();
  330.         hottub.setTemperature(104);
  331.         hottub.circulate();
  332.     }
  333.     public void undo()
  334.     {
  335.         hottub.off();
  336.     }
  337. }
  338. class HottubOffCommand implements Command
  339. {
  340.     Hottub hottub;
  341.     public HottubOffCommand(Hottub hottub)
  342.     {
  343.         this.hottub = hottub;
  344.     }
  345.     public void execute()
  346.     {
  347.         hottub.setTemperature(98);
  348.         hottub.off();
  349.     }
  350.     public void undo()
  351.     {
  352.         hottub.on();
  353.     }
  354. }
  355. class MacroCommand implements Command
  356. {
  357.     Command[] commands;
  358.     public MacroCommand(Command[] commands)
  359.     {
  360.         this.commands = commands;
  361.     }
  362.     public void execute()
  363.     {
  364.         for (int i = 0; i < commands.length; i++)
  365.         {
  366.             commands[i].execute();
  367.         }
  368.     }
  369.     public void undo()
  370.     {
  371.         for (int i = commands.length - 1; i >= 0; i--)
  372.         {
  373.                         commands[i].undo();
  374.                 }
  375.     }
  376. }
  377. class StereoOnCommand implements Command
  378. {
  379.     Stereo stereo;
  380.     public StereoOnCommand(Stereo stereo)
  381.     {
  382.         this.stereo = stereo;
  383.     }
  384.     public void execute()
  385.     {
  386.         stereo.on();
  387.     }
  388.     public void undo()
  389.     {
  390.         stereo.off();
  391.     }
  392. }
  393. class StereoOffCommand implements Command
  394. {
  395.     Stereo stereo;
  396.     public StereoOffCommand(Stereo stereo)
  397.     {
  398.         this.stereo = stereo;
  399.     }
  400.     public void execute()
  401.     {
  402.         stereo.off();
  403.     }
  404.     public void undo()
  405.     {
  406.         stereo.on();
  407.     }
  408. }
  409. class TVOnCommand implements Command
  410. {
  411.     TV tv;
  412.     public TVOnCommand(TV tv)
  413.     {
  414.         this.tv = tv;
  415.     }
  416.     public void execute()
  417.     {
  418.         tv.on();
  419.         tv.setInputChannel();
  420.     }
  421.     public void undo()
  422.     {
  423.         tv.off();
  424.     }
  425. }
  426. class TVOffCommand implements Command
  427. {
  428.     TV tv;
  429.     public TVOffCommand(TV tv)
  430.     {
  431.         this.tv = tv;
  432.     }
  433.     public void execute()
  434.     {
  435.         tv.off();
  436.     }
  437.     public void undo()
  438.     {
  439.         tv.on();
  440.     }
  441. }
  442. class RemoteControl
  443. {
  444.     Command[] onCommands;
  445.     Command[] offCommands;
  446.     Command undoCommand;
  447.     public RemoteControl()
  448.     {
  449.         onCommands = new Command[7];
  450.         offCommands = new Command[7];
  451.         Command noCommand = new NoCommand();
  452.         for (int i = 0; i < 7; i++)
  453.         {
  454.             onCommands[i] = noCommand;
  455.             offCommands[i] = noCommand;
  456.         }
  457.         undoCommand = noCommand;
  458.     }
  459.     public void setCommand(int slot, Command onCommand, Command offCommand)
  460.     {
  461.         onCommands[slot] = onCommand;
  462.         offCommands[slot] = offCommand;
  463.     }
  464.     public void onButtonWasPushed(int slot)
  465.     {
  466.         onCommands[slot].execute();
  467.         undoCommand = onCommands[slot];
  468.     }
  469.     public void offButtonWasPushed(int slot)
  470.     {
  471.         offCommands[slot].execute();
  472.         undoCommand = offCommands[slot];
  473.     }
  474.     public void undoButtonWasPushed()
  475.     {
  476.         undoCommand.undo();
  477.     }
  478.     public String toString()
  479.     {
  480.         StringBuffer stringBuff = new StringBuffer();
  481.         stringBuff.append("/n------ Remote Control -------/n");
  482.         for (int i = 0; i < onCommands.length; i++)
  483.         {
  484.             stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName()
  485.                 + "    " + offCommands[i].getClass().getName() + "/n");
  486.         }
  487.         stringBuff.append("[undo] " + undoCommand.getClass().getName() + "/n");
  488.         return stringBuff.toString();
  489.     }
  490. }
  491. public class RemoteLoader
  492. {
  493.     public static void main(String[] args)
  494.     {
  495.         RemoteControl remoteControl = new RemoteControl();
  496.         Light light = new Light("Living Room");
  497.         TV tv = new TV("Living Room");
  498.         Stereo stereo = new Stereo("Living Room");
  499.         Hottub hottub = new Hottub();
  500.         LightOnCommand lightOn = new LightOnCommand(light);
  501.         StereoOnCommand stereoOn = new StereoOnCommand(stereo);
  502.         TVOnCommand tvOn = new TVOnCommand(tv);
  503.         HottubOnCommand hottubOn = new HottubOnCommand(hottub);
  504.         LightOffCommand lightOff = new LightOffCommand(light);
  505.         StereoOffCommand stereoOff = new StereoOffCommand(stereo);
  506.         TVOffCommand tvOff = new TVOffCommand(tv);
  507.         HottubOffCommand hottubOff = new HottubOffCommand(hottub);
  508.         Command[] partyOn = { lightOn, stereoOn, tvOn, hottubOn };
  509.         Command[] partyOff = { lightOff, stereoOff, tvOff, hottubOff };
  510.         MacroCommand partyOnMacro = new MacroCommand(partyOn);
  511.         MacroCommand partyOffMacro = new MacroCommand(partyOff);
  512.         remoteControl.setCommand(0, partyOnMacro, partyOffMacro);
  513.         CeilingFan ceilingFan = new CeilingFan("Living Room");
  514.         CeilingFanHighCommand ceilingFanHigh = new CeilingFanHighCommand(ceilingFan);
  515.         CeilingFanMediumCommand ceilingFanMedium = new CeilingFanMediumCommand(ceilingFan);
  516.         CeilingFanLowCommand ceilingFanLow = new CeilingFanLowCommand(ceilingFan);
  517.         CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan);
  518.         remoteControl.setCommand(1, ceilingFanHigh, ceilingFanOff);
  519.         remoteControl.setCommand(2, ceilingFanMedium, ceilingFanOff);
  520.         remoteControl.setCommand(3, ceilingFanLow, ceilingFanOff);
  521.         System.out.println(remoteControl);
  522.         System.out.println("--- Pushing Macro On---");
  523.         remoteControl.onButtonWasPushed(0);
  524.         System.out.println("--- Pushing Macro Off---");
  525.         remoteControl.offButtonWasPushed(0);
  526.         remoteControl.onButtonWasPushed(1);
  527.         remoteControl.offButtonWasPushed(1);
  528.         System.out.println(remoteControl);
  529.         remoteControl.undoButtonWasPushed();
  530.         remoteControl.onButtonWasPushed(2);
  531.         System.out.println(remoteControl);
  532.         remoteControl.undoButtonWasPushed();
  533.     }
  534. }

 

 

C++代码:

 

  1. // RemoteLoader.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <string>
  6. using namespace std;
  7. class Light
  8. {
  9.     string  location;
  10.     int level;
  11. public :
  12.     Light(string location)
  13.     {
  14.         this->location = location;
  15.     }
  16.     void on()
  17.     {
  18.         level = 100;
  19.         cout<<"Light is on/n";
  20.     }
  21.     void off()
  22.     {
  23.         level = 0;
  24.         cout<<"Light is off/n";
  25.     }
  26.     void dim(int level)
  27.     {
  28.         this->level = level;
  29.         if (level == 0)
  30.         {
  31.             off();
  32.         }
  33.         else
  34.         {
  35.             cout<<"Light is dimmed to "<< level <<"%/n";
  36.         }
  37.     }
  38.     int getLevel()
  39.     {
  40.         return level;
  41.     }
  42. };
  43. class CeilingFan
  44. {
  45. public:
  46.     static const int HIGH = 3;
  47.     static const int MEDIUM = 2;
  48.     static const int LOW = 1;
  49.     static const int OFF = 0;
  50.     string location;
  51.     int speed;
  52.     CeilingFan(string location)
  53.     {
  54.         this->location = location;
  55.     }
  56.     void high()
  57.     {
  58.         // turns the ceiling fan on to high
  59.         speed = HIGH;
  60.         cout<<location << " ceiling fan is on high/n";
  61.     }
  62.     void medium()
  63.     {
  64.         // turns the ceiling fan on to medium
  65.         speed = MEDIUM;
  66.         cout<<location << " ceiling fan is on medium/n";
  67.     }
  68.     void low()
  69.     {
  70.         // turns the ceiling fan on to low
  71.         speed = LOW;
  72.         cout<<location << " ceiling fan is on low/n";
  73.     }
  74.     void off()
  75.     {
  76.         // turns the ceiling fan off
  77.         speed = OFF;
  78.         cout<<location <<" ceiling fan is off/n";
  79.     }
  80.     int getSpeed()
  81.     {
  82.         return speed;
  83.     }
  84. };
  85. class Hottub
  86. {
  87.     bool isOn;
  88.     int temperature;
  89. public:
  90.     Hottub()
  91.     {
  92.     }
  93.     void on()
  94.     {
  95.         isOn = true;
  96.     }
  97.     void off()
  98.     {
  99.         isOn = false;
  100.     }
  101.     void circulate()
  102.     {
  103.         if (isOn)
  104.         {
  105.             cout<<"Hottub is bubbling!/n";
  106.         }
  107.     }
  108.     void jetsOn()
  109.     {
  110.         if (isOn)
  111.         {
  112.             cout<<"Hottub jets are on/n";
  113.         }
  114.     }
  115.     void jetsOff()
  116.     {
  117.         if (isOn)
  118.         {
  119.             cout<<"Hottub jets are off/n";
  120.         }
  121.     }
  122.     void setTemperature(int temperature)
  123.     {
  124.         if (temperature > this->temperature)
  125.         {
  126.             cout<<"Hottub is heating to a steaming " << temperature << " degrees/n";
  127.         }
  128.         else
  129.         {
  130.             cout<<"Hottub is cooling to " << temperature << " degrees/n";
  131.         }
  132.         this->temperature = temperature;
  133.     }
  134. };
  135. class Stereo
  136. {
  137.     string location;
  138. public :
  139.     Stereo(string location)
  140.     {
  141.         this->location = location;
  142.     }
  143.     void on()
  144.     {
  145.         cout<<location << " stereo is on/n";
  146.     }
  147.     void off()
  148.     {
  149.         cout<<location << " stereo is off/n";
  150.     }
  151.     void setCD()
  152.     {
  153.         cout<<location << " stereo is set for CD input/n";
  154.     }
  155.     void setDVD()
  156.     {
  157.         cout<<location << " stereo is set for DVD input/n";
  158.     }
  159.     void setRadio()
  160.     {
  161.         cout<<location << " stereo is set for Radio/n";
  162.     }
  163.     void setVolume(int volume)
  164.     {
  165.         // code to set the volume
  166.         // valid range: 1-11 (after all 11 is better than 10, right?)
  167.         cout<<location << " Stereo volume set to " << volume<<"/n";
  168.     }
  169. };
  170. class TV
  171. {
  172.     string location;
  173.     int channel;
  174. public:
  175.     TV(string location)
  176.     {
  177.         this->location = location;
  178.     }
  179.     void on()
  180.     {
  181.         cout<<location << " TV is on/n";
  182.     }
  183.     void off()
  184.     {
  185.         cout<<location << " TV is off/n";
  186.     }
  187.     void setInputChannel()
  188.     {
  189.         this->channel = 3;
  190.         cout<<location << " TV channel is set for DVD/n";
  191.     }
  192. };
  193. class Command
  194. {
  195. public:
  196.     virtual void execute()=0;
  197.     virtual void undo()=0;
  198. };
  199. class NoCommand :public Command
  200. {
  201. public:
  202.     void execute() { }
  203.     void undo() { }
  204. };
  205. class LightOnCommand :public Command
  206. {
  207.     Light* light;
  208. public:
  209.     LightOnCommand(Light* light)
  210.     {
  211.         this->light = light;
  212.     }
  213.     void execute()
  214.     {
  215.         light->on();
  216.     }
  217.     void undo()
  218.     {
  219.         light->off();
  220.     }
  221. };
  222. class LightOffCommand :public Command
  223. {
  224.     Light* light;
  225. public:
  226.     LightOffCommand(Light* light)
  227.     {
  228.         this->light = light;
  229.     }
  230.     void execute()
  231.     {
  232.         light->off();
  233.     }
  234.     void undo()
  235.     {
  236.         light->on();
  237.     }
  238. };
  239. class CeilingFanHighCommand :public Command
  240. {
  241.     CeilingFan* ceilingFan;
  242.     int prevSpeed;
  243. public:
  244.     CeilingFanHighCommand(CeilingFan* ceilingFan)
  245.     {
  246.         this->ceilingFan = ceilingFan;
  247.     }
  248.     void execute()
  249.     {
  250.         prevSpeed = ceilingFan->getSpeed();
  251.         ceilingFan->high();
  252.     }
  253.     void undo()
  254.     {
  255.         switch (prevSpeed)
  256.         {
  257.             case CeilingFan::HIGH: 
  258.                 ceilingFan->high(); 
  259.                 break;
  260.             case CeilingFan::MEDIUM: 
  261.                 ceilingFan->medium(); 
  262.                 break;
  263.             case CeilingFan::LOW: 
  264.                 ceilingFan->low();
  265.                 break;
  266.             default
  267.                 ceilingFan->off(); 
  268.                 break;
  269.         }
  270.     }
  271. };
  272. class CeilingFanMediumCommand :public Command
  273. {
  274.     CeilingFan* ceilingFan;
  275.     int prevSpeed;
  276. public:
  277.     CeilingFanMediumCommand(CeilingFan* ceilingFan)
  278.     {
  279.         this->ceilingFan = ceilingFan;
  280.     }
  281.     void execute()
  282.     {
  283.         prevSpeed = ceilingFan->getSpeed();
  284.         ceilingFan->medium();
  285.     }
  286.     void undo()
  287.     {
  288.         switch (prevSpeed)
  289.         {
  290.             case CeilingFan::HIGH: 
  291.                     ceilingFan->high(); 
  292.                     break;
  293.             case CeilingFan::MEDIUM: 
  294.                     ceilingFan->medium(); 
  295.                     break;
  296.             case CeilingFan::LOW: 
  297.                     ceilingFan->low(); 
  298.                     break;
  299.             default
  300.                 ceilingFan->off(); 
  301.                 break;
  302.         }
  303.     }
  304. };
  305. class CeilingFanLowCommand :public Command
  306. {
  307.     CeilingFan* ceilingFan;
  308.     int prevSpeed;
  309. public:
  310.     CeilingFanLowCommand(CeilingFan* ceilingFan)
  311.     {
  312.         this->ceilingFan = ceilingFan;
  313.     }
  314.     void execute()
  315.     {
  316.         prevSpeed = ceilingFan->getSpeed();
  317.         ceilingFan->low();
  318.     }
  319.     void undo()
  320.     {
  321.         switch (prevSpeed)
  322.         {
  323.             case CeilingFan::HIGH: 
  324.                 ceilingFan->high(); 
  325.                 break;
  326.             case CeilingFan::MEDIUM:
  327.                 ceilingFan->medium(); 
  328.                 break;
  329.             case CeilingFan::LOW: 
  330.                 ceilingFan->low(); 
  331.                 break;
  332.             default
  333.                 ceilingFan->off(); 
  334.                 break;
  335.         }
  336.     }
  337. };
  338. class CeilingFanOffCommand :public Command
  339. {
  340.     CeilingFan* ceilingFan;
  341.     int prevSpeed;
  342. public:
  343.     CeilingFanOffCommand(CeilingFan* ceilingFan)
  344.     {
  345.         this->ceilingFan = ceilingFan;
  346.     }
  347.     void execute()
  348.     {
  349.         prevSpeed = ceilingFan->getSpeed();
  350.         ceilingFan->off();
  351.     }
  352.     void undo()
  353.     {
  354.         switch (prevSpeed)
  355.         {
  356.             case CeilingFan::HIGH: 
  357.                 ceilingFan->high(); 
  358.                 break;
  359.             case CeilingFan::MEDIUM: 
  360.                 ceilingFan->medium(); 
  361.                 break;
  362.             case CeilingFan::LOW: 
  363.                 ceilingFan->low(); 
  364.                 break;
  365.             default
  366.                 ceilingFan->off(); 
  367.                 break;
  368.         }
  369.     }
  370. };
  371. class HottubOnCommand :public Command
  372. {
  373.     Hottub* hottub;
  374. public:
  375.     HottubOnCommand(Hottub* hottub)
  376.     {
  377.         this->hottub = hottub;
  378.     }
  379.     void execute()
  380.     {
  381.         hottub->on();
  382.         hottub->setTemperature(104);
  383.         hottub->circulate();
  384.     }
  385.     void undo()
  386.     {
  387.         hottub->off();
  388.     }
  389. };
  390. class HottubOffCommand :public Command
  391. {
  392.     Hottub* hottub;
  393. public:
  394.     HottubOffCommand(Hottub* hottub)
  395.     {
  396.         this->hottub = hottub;
  397.     }
  398.     void execute()
  399.     {
  400.         hottub->setTemperature(98);
  401.         hottub->off();
  402.     }
  403.     void undo()
  404.     {
  405.         hottub->on();
  406.     }
  407. };
  408. class MacroCommand :public Command
  409. {
  410.     Command** commands;
  411.     int commandSize;
  412. public:
  413.     MacroCommand(Command** commands,int commandSize)
  414.     {
  415.         this->commands = commands;
  416.         this->commandSize=commandSize;
  417.     }
  418.     void execute()
  419.     {
  420.         for (int i = 0; i < commandSize; i++)
  421.         {
  422.             commands[i]->execute();
  423.         }
  424.     }
  425.     void undo()
  426.     {
  427.         for (int i = commandSize - 1; i >= 0; i--)
  428.         {
  429.             commands[i]->undo();
  430.         }
  431.     }
  432. };
  433. class StereoOnCommand :public Command
  434. {
  435.     Stereo* stereo;
  436. public:
  437.     StereoOnCommand(Stereo* stereo)
  438.     {
  439.         this->stereo = stereo;
  440.     }
  441.     void execute()
  442.     {
  443.         stereo->on();
  444.     }
  445.     void undo()
  446.     {
  447.         stereo->off();
  448.     }
  449. };
  450. class StereoOffCommand :public Command
  451. {
  452.     Stereo* stereo;
  453. public:
  454.     StereoOffCommand(Stereo* stereo)
  455.     {
  456.         this->stereo = stereo;
  457.     }
  458.     void execute()
  459.     {
  460.         stereo->off();
  461.     }
  462.     void undo()
  463.     {
  464.         stereo->on();
  465.     }
  466. };
  467. class TVOnCommand :public Command
  468. {
  469.     TV* tv;
  470. public:
  471.     TVOnCommand(TV* tv)
  472.     {
  473.         this->tv = tv;
  474.     }
  475.     void execute()
  476.     {
  477.         tv->on();
  478.         tv->setInputChannel();
  479.     }
  480.     void undo()
  481.     {
  482.         tv->off();
  483.     }
  484. };
  485. class TVOffCommand :public Command
  486. {
  487.     TV* tv;
  488. public:
  489.     TVOffCommand(TV* tv)
  490.     {
  491.         this->tv = tv;
  492.     }
  493.     void execute()
  494.     {
  495.         tv->off();
  496.     }
  497.     void undo()
  498.     {
  499.         tv->on();
  500.     }
  501. };
  502. class RemoteControl
  503. {
  504.     Command** onCommands;
  505.     Command** offCommands;
  506.     Command* undoCommand;
  507. public:
  508.     RemoteControl()
  509.     {
  510.         onCommands = new Command*[7];
  511.         offCommands = new Command*[7];
  512.         Command* noCommand = new NoCommand();
  513.         for (int i = 0; i < 7; i++)
  514.         {
  515.             onCommands[i] = noCommand;
  516.             offCommands[i] = noCommand;
  517.         }
  518.         undoCommand = noCommand;
  519.     }
  520.     void setCommand(int slot, Command* onCommand, Command* offCommand)
  521.     {
  522.         onCommands[slot] = onCommand;
  523.         offCommands[slot] = offCommand;
  524.     }
  525.     void onButtonWasPushed(int slot)
  526.     {
  527.         onCommands[slot]->execute();
  528.         undoCommand = onCommands[slot];
  529.     }
  530.     void offButtonWasPushed(int slot)
  531.     {
  532.         offCommands[slot]->execute();
  533.         undoCommand = offCommands[slot];
  534.     }
  535.     void undoButtonWasPushed()
  536.     {
  537.         undoCommand->undo();
  538.     }
  539.     
  540. };
  541. int _tmain(int argc, _TCHAR* argv[])
  542. {
  543.     RemoteControl* remoteControl = new RemoteControl();
  544.     Light* light = new Light("Living Room");
  545.     TV* tv = new TV("Living Room");
  546.     Stereo* stereo = new Stereo("Living Room");
  547.     Hottub* hottub = new Hottub();
  548.     LightOnCommand* lightOn = new LightOnCommand(light);
  549.     StereoOnCommand* stereoOn = new StereoOnCommand(stereo);
  550.     TVOnCommand* tvOn = new TVOnCommand(tv);
  551.     HottubOnCommand* hottubOn = new HottubOnCommand(hottub);
  552.     LightOffCommand* lightOff = new LightOffCommand(light);
  553.     StereoOffCommand* stereoOff = new StereoOffCommand(stereo);
  554.     TVOffCommand* tvOff = new TVOffCommand(tv);
  555.     HottubOffCommand* hottubOff = new HottubOffCommand(hottub);
  556.     Command** partyOn =new Command*[4];
  557.     partyOn[0]= lightOn ;
  558.     partyOn[1]=stereoOn;
  559.     partyOn[2]=tvOn;
  560.     partyOn[3]=hottubOn;
  561.     Command* partyOff[] = { lightOff, stereoOff, tvOff, hottubOff };
  562.     MacroCommand* partyOnMacro = new MacroCommand(partyOn,4);
  563.     MacroCommand* partyOffMacro = new MacroCommand(partyOff,4);
  564.     remoteControl->setCommand(0, partyOnMacro, partyOffMacro);
  565.     CeilingFan* ceilingFan = new CeilingFan("Living Room");
  566.     CeilingFanHighCommand* ceilingFanHigh = new CeilingFanHighCommand(ceilingFan);
  567.     CeilingFanMediumCommand* ceilingFanMedium = new CeilingFanMediumCommand(ceilingFan);
  568.     CeilingFanLowCommand* ceilingFanLow = new CeilingFanLowCommand(ceilingFan);
  569.     CeilingFanOffCommand* ceilingFanOff = new CeilingFanOffCommand(ceilingFan);
  570.     remoteControl->setCommand(1, ceilingFanHigh, ceilingFanOff);
  571.     remoteControl->setCommand(2, ceilingFanMedium, ceilingFanOff);
  572.     remoteControl->setCommand(3, ceilingFanLow, ceilingFanOff);
  573.     //System.out.println(remoteControl);
  574.     cout<<"--- Pushing Macro On---/n";
  575.     remoteControl->onButtonWasPushed(0);
  576.     cout<<"--- Pushing Macro Off---/n";
  577.     remoteControl->offButtonWasPushed(0);
  578.     remoteControl->onButtonWasPushed(1);
  579.     remoteControl->offButtonWasPushed(1);
  580.     //System.out.println(remoteControl);
  581.     remoteControl->undoButtonWasPushed();
  582.     remoteControl->onButtonWasPushed(2);
  583.     //System.out.println(remoteControl);
  584.     remoteControl->undoButtonWasPushed();
  585.     return 0;
  586. }

 

结果;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值