命令模式(Command Pattern):将“请求”封装成对象,以便使用不同的请求、队列或者日志来参数化其他对象,命令模式也支持可撤销的操作。
实例:可编程遥控器,每个按钮可自定义设置各种家电命令,但家电厂商种类繁多,不同家电的命令不同,要使遥控器与命令分离
JAVA代码
- class Light
- {
- String location;
- int level;
- public Light(String location)
- {
- this.location = location;
- }
- public void on()
- {
- level = 100;
- System.out.println("Light is on");
- }
- public void off()
- {
- level = 0;
- System.out.println("Light is off");
- }
- public void dim(int level)
- {
- this.level = level;
- if (level == 0)
- {
- off();
- }
- else
- {
- System.out.println("Light is dimmed to " + level + "%");
- }
- }
- public int getLevel()
- {
- return level;
- }
- }
- class CeilingFan
- {
- public static final int HIGH = 3;
- public static final int MEDIUM = 2;
- public static final int LOW = 1;
- public static final int OFF = 0;
- String location;
- int speed;
- public CeilingFan(String location)
- {
- this.location = location;
- }
- public void high()
- {
- // turns the ceiling fan on to high
- speed = HIGH;
- System.out.println(location + " ceiling fan is on high");
- }
- public void medium()
- {
- // turns the ceiling fan on to medium
- speed = MEDIUM;
- System.out.println(location + " ceiling fan is on medium");
- }
- public void low()
- {
- // turns the ceiling fan on to low
- speed = LOW;
- System.out.println(location + " ceiling fan is on low");
- }
- public void off()
- {
- // turns the ceiling fan off
- speed = OFF;
- System.out.println(location + " ceiling fan is off");
- }
- public int getSpeed()
- {
- return speed;
- }
- }
- class Hottub
- {
- boolean on;
- int temperature;
- public Hottub()
- {
- }
- public void on()
- {
- on = true;
- }
- public void off()
- {
- on = false;
- }
- public void circulate()
- {
- if (on)
- {
- System.out.println("Hottub is bubbling!");
- }
- }
- public void jetsOn()
- {
- if (on)
- {
- System.out.println("Hottub jets are on");
- }
- }
- public void jetsOff()
- {
- if (on)
- {
- System.out.println("Hottub jets are off");
- }
- }
- public void setTemperature(int temperature)
- {
- if (temperature > this.temperature)
- {
- System.out.println("Hottub is heating to a steaming " + temperature + " degrees");
- }
- else
- {
- System.out.println("Hottub is cooling to " + temperature + " degrees");
- }
- this.temperature = temperature;
- }
- }
- class Stereo
- {
- String location;
- public Stereo(String location)
- {
- this.location = location;
- }
- public void on()
- {
- System.out.println(location + " stereo is on");
- }
- public void off()
- {
- System.out.println(location + " stereo is off");
- }
- public void setCD()
- {
- System.out.println(location + " stereo is set for CD input");
- }
- public void setDVD()
- {
- System.out.println(location + " stereo is set for DVD input");
- }
- public void setRadio()
- {
- System.out.println(location + " stereo is set for Radio");
- }
- public void setVolume(int volume)
- {
- // code to set the volume
- // valid range: 1-11 (after all 11 is better than 10, right?)
- System.out.println(location + " Stereo volume set to " + volume);
- }
- }
- class TV
- {
- String location;
- int channel;
- public TV(String location)
- {
- this.location = location;
- }
- public void on()
- {
- System.out.println(location + " TV is on");
- }
- public void off()
- {
- System.out.println(location + " TV is off");
- }
- public void setInputChannel()
- {
- this.channel = 3;
- System.out.println(location + " TV channel is set for DVD");
- }
- }
- interface Command
- {
- public void execute();
- public void undo();
- }
- class NoCommand implements Command
- {
- public void execute() { }
- public void undo() { }
- }
- class LightOnCommand implements Command
- {
- Light light;
- public LightOnCommand(Light light)
- {
- this.light = light;
- }
- public void execute()
- {
- light.on();
- }
- public void undo()
- {
- light.off();
- }
- }
- class LightOffCommand implements Command
- {
- Light light;
- public LightOffCommand(Light light)
- {
- this.light = light;
- }
- public void execute()
- {
- light.off();
- }
- public void undo()
- {
- light.on();
- }
- }
- class CeilingFanHighCommand implements Command
- {
- CeilingFan ceilingFan;
- int prevSpeed;
- public CeilingFanHighCommand(CeilingFan ceilingFan)
- {
- this.ceilingFan = ceilingFan;
- }
- public void execute()
- {
- prevSpeed = ceilingFan.getSpeed();
- ceilingFan.high();
- }
- public void undo()
- {
- switch (prevSpeed)
- {
- case CeilingFan.HIGH: ceilingFan.high(); break;
- case CeilingFan.MEDIUM: ceilingFan.medium(); break;
- case CeilingFan.LOW: ceilingFan.low(); break;
- default: ceilingFan.off(); break;
- }
- }
- }
- class CeilingFanMediumCommand implements Command
- {
- CeilingFan ceilingFan;
- int prevSpeed;
- public CeilingFanMediumCommand(CeilingFan ceilingFan)
- {
- this.ceilingFan = ceilingFan;
- }
- public void execute()
- {
- prevSpeed = ceilingFan.getSpeed();
- ceilingFan.medium();
- }
- public void undo()
- {
- switch (prevSpeed)
- {
- case CeilingFan.HIGH: ceilingFan.high(); break;
- case CeilingFan.MEDIUM: ceilingFan.medium(); break;
- case CeilingFan.LOW: ceilingFan.low(); break;
- default: ceilingFan.off(); break;
- }
- }
- }
- class CeilingFanLowCommand implements Command
- {
- CeilingFan ceilingFan;
- int prevSpeed;
- public CeilingFanLowCommand(CeilingFan ceilingFan)
- {
- this.ceilingFan = ceilingFan;
- }
- public void execute()
- {
- prevSpeed = ceilingFan.getSpeed();
- ceilingFan.low();
- }
- public void undo()
- {
- switch (prevSpeed)
- {
- case CeilingFan.HIGH: ceilingFan.high(); break;
- case CeilingFan.MEDIUM: ceilingFan.medium(); break;
- case CeilingFan.LOW: ceilingFan.low(); break;
- default: ceilingFan.off(); break;
- }
- }
- }
- class CeilingFanOffCommand implements Command
- {
- CeilingFan ceilingFan;
- int prevSpeed;
- public CeilingFanOffCommand(CeilingFan ceilingFan)
- {
- this.ceilingFan = ceilingFan;
- }
- public void execute()
- {
- prevSpeed = ceilingFan.getSpeed();
- ceilingFan.off();
- }
- public void undo()
- {
- switch (prevSpeed)
- {
- case CeilingFan.HIGH: ceilingFan.high(); break;
- case CeilingFan.MEDIUM: ceilingFan.medium(); break;
- case CeilingFan.LOW: ceilingFan.low(); break;
- default: ceilingFan.off(); break;
- }
- }
- }
- class HottubOnCommand implements Command
- {
- Hottub hottub;
- public HottubOnCommand(Hottub hottub)
- {
- this.hottub = hottub;
- }
- public void execute()
- {
- hottub.on();
- hottub.setTemperature(104);
- hottub.circulate();
- }
- public void undo()
- {
- hottub.off();
- }
- }
- class HottubOffCommand implements Command
- {
- Hottub hottub;
- public HottubOffCommand(Hottub hottub)
- {
- this.hottub = hottub;
- }
- public void execute()
- {
- hottub.setTemperature(98);
- hottub.off();
- }
- public void undo()
- {
- hottub.on();
- }
- }
- class MacroCommand implements Command
- {
- Command[] commands;
- public MacroCommand(Command[] commands)
- {
- this.commands = commands;
- }
- public void execute()
- {
- for (int i = 0; i < commands.length; i++)
- {
- commands[i].execute();
- }
- }
- public void undo()
- {
- for (int i = commands.length - 1; i >= 0; i--)
- {
- commands[i].undo();
- }
- }
- }
- class StereoOnCommand implements Command
- {
- Stereo stereo;
- public StereoOnCommand(Stereo stereo)
- {
- this.stereo = stereo;
- }
- public void execute()
- {
- stereo.on();
- }
- public void undo()
- {
- stereo.off();
- }
- }
- class StereoOffCommand implements Command
- {
- Stereo stereo;
- public StereoOffCommand(Stereo stereo)
- {
- this.stereo = stereo;
- }
- public void execute()
- {
- stereo.off();
- }
- public void undo()
- {
- stereo.on();
- }
- }
- class TVOnCommand implements Command
- {
- TV tv;
- public TVOnCommand(TV tv)
- {
- this.tv = tv;
- }
- public void execute()
- {
- tv.on();
- tv.setInputChannel();
- }
- public void undo()
- {
- tv.off();
- }
- }
- class TVOffCommand implements Command
- {
- TV tv;
- public TVOffCommand(TV tv)
- {
- this.tv = tv;
- }
- public void execute()
- {
- tv.off();
- }
- public void undo()
- {
- tv.on();
- }
- }
- class RemoteControl
- {
- Command[] onCommands;
- Command[] offCommands;
- Command undoCommand;
- public RemoteControl()
- {
- onCommands = new Command[7];
- offCommands = new Command[7];
- Command noCommand = new NoCommand();
- for (int i = 0; i < 7; i++)
- {
- onCommands[i] = noCommand;
- offCommands[i] = noCommand;
- }
- undoCommand = noCommand;
- }
- public void setCommand(int slot, Command onCommand, Command offCommand)
- {
- onCommands[slot] = onCommand;
- offCommands[slot] = offCommand;
- }
- public void onButtonWasPushed(int slot)
- {
- onCommands[slot].execute();
- undoCommand = onCommands[slot];
- }
- public void offButtonWasPushed(int slot)
- {
- offCommands[slot].execute();
- undoCommand = offCommands[slot];
- }
- public void undoButtonWasPushed()
- {
- undoCommand.undo();
- }
- public String toString()
- {
- StringBuffer stringBuff = new StringBuffer();
- stringBuff.append("/n------ Remote Control -------/n");
- for (int i = 0; i < onCommands.length; i++)
- {
- stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName()
- + " " + offCommands[i].getClass().getName() + "/n");
- }
- stringBuff.append("[undo] " + undoCommand.getClass().getName() + "/n");
- return stringBuff.toString();
- }
- }
- public class RemoteLoader
- {
- public static void main(String[] args)
- {
- RemoteControl remoteControl = new RemoteControl();
- Light light = new Light("Living Room");
- TV tv = new TV("Living Room");
- Stereo stereo = new Stereo("Living Room");
- Hottub hottub = new Hottub();
- LightOnCommand lightOn = new LightOnCommand(light);
- StereoOnCommand stereoOn = new StereoOnCommand(stereo);
- TVOnCommand tvOn = new TVOnCommand(tv);
- HottubOnCommand hottubOn = new HottubOnCommand(hottub);
- LightOffCommand lightOff = new LightOffCommand(light);
- StereoOffCommand stereoOff = new StereoOffCommand(stereo);
- TVOffCommand tvOff = new TVOffCommand(tv);
- HottubOffCommand hottubOff = new HottubOffCommand(hottub);
- Command[] partyOn = { lightOn, stereoOn, tvOn, hottubOn };
- Command[] partyOff = { lightOff, stereoOff, tvOff, hottubOff };
- MacroCommand partyOnMacro = new MacroCommand(partyOn);
- MacroCommand partyOffMacro = new MacroCommand(partyOff);
- remoteControl.setCommand(0, partyOnMacro, partyOffMacro);
- CeilingFan ceilingFan = new CeilingFan("Living Room");
- CeilingFanHighCommand ceilingFanHigh = new CeilingFanHighCommand(ceilingFan);
- CeilingFanMediumCommand ceilingFanMedium = new CeilingFanMediumCommand(ceilingFan);
- CeilingFanLowCommand ceilingFanLow = new CeilingFanLowCommand(ceilingFan);
- CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan);
- remoteControl.setCommand(1, ceilingFanHigh, ceilingFanOff);
- remoteControl.setCommand(2, ceilingFanMedium, ceilingFanOff);
- remoteControl.setCommand(3, ceilingFanLow, ceilingFanOff);
- System.out.println(remoteControl);
- System.out.println("--- Pushing Macro On---");
- remoteControl.onButtonWasPushed(0);
- System.out.println("--- Pushing Macro Off---");
- remoteControl.offButtonWasPushed(0);
- remoteControl.onButtonWasPushed(1);
- remoteControl.offButtonWasPushed(1);
- System.out.println(remoteControl);
- remoteControl.undoButtonWasPushed();
- remoteControl.onButtonWasPushed(2);
- System.out.println(remoteControl);
- remoteControl.undoButtonWasPushed();
- }
- }
C++代码:
- // RemoteLoader.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- using namespace std;
- class Light
- {
- string location;
- int level;
- public :
- Light(string location)
- {
- this->location = location;
- }
- void on()
- {
- level = 100;
- cout<<"Light is on/n";
- }
- void off()
- {
- level = 0;
- cout<<"Light is off/n";
- }
- void dim(int level)
- {
- this->level = level;
- if (level == 0)
- {
- off();
- }
- else
- {
- cout<<"Light is dimmed to "<< level <<"%/n";
- }
- }
- int getLevel()
- {
- return level;
- }
- };
- class CeilingFan
- {
- public:
- static const int HIGH = 3;
- static const int MEDIUM = 2;
- static const int LOW = 1;
- static const int OFF = 0;
- string location;
- int speed;
- CeilingFan(string location)
- {
- this->location = location;
- }
- void high()
- {
- // turns the ceiling fan on to high
- speed = HIGH;
- cout<<location << " ceiling fan is on high/n";
- }
- void medium()
- {
- // turns the ceiling fan on to medium
- speed = MEDIUM;
- cout<<location << " ceiling fan is on medium/n";
- }
- void low()
- {
- // turns the ceiling fan on to low
- speed = LOW;
- cout<<location << " ceiling fan is on low/n";
- }
- void off()
- {
- // turns the ceiling fan off
- speed = OFF;
- cout<<location <<" ceiling fan is off/n";
- }
- int getSpeed()
- {
- return speed;
- }
- };
- class Hottub
- {
- bool isOn;
- int temperature;
- public:
- Hottub()
- {
- }
- void on()
- {
- isOn = true;
- }
- void off()
- {
- isOn = false;
- }
- void circulate()
- {
- if (isOn)
- {
- cout<<"Hottub is bubbling!/n";
- }
- }
- void jetsOn()
- {
- if (isOn)
- {
- cout<<"Hottub jets are on/n";
- }
- }
- void jetsOff()
- {
- if (isOn)
- {
- cout<<"Hottub jets are off/n";
- }
- }
- void setTemperature(int temperature)
- {
- if (temperature > this->temperature)
- {
- cout<<"Hottub is heating to a steaming " << temperature << " degrees/n";
- }
- else
- {
- cout<<"Hottub is cooling to " << temperature << " degrees/n";
- }
- this->temperature = temperature;
- }
- };
- class Stereo
- {
- string location;
- public :
- Stereo(string location)
- {
- this->location = location;
- }
- void on()
- {
- cout<<location << " stereo is on/n";
- }
- void off()
- {
- cout<<location << " stereo is off/n";
- }
- void setCD()
- {
- cout<<location << " stereo is set for CD input/n";
- }
- void setDVD()
- {
- cout<<location << " stereo is set for DVD input/n";
- }
- void setRadio()
- {
- cout<<location << " stereo is set for Radio/n";
- }
- void setVolume(int volume)
- {
- // code to set the volume
- // valid range: 1-11 (after all 11 is better than 10, right?)
- cout<<location << " Stereo volume set to " << volume<<"/n";
- }
- };
- class TV
- {
- string location;
- int channel;
- public:
- TV(string location)
- {
- this->location = location;
- }
- void on()
- {
- cout<<location << " TV is on/n";
- }
- void off()
- {
- cout<<location << " TV is off/n";
- }
- void setInputChannel()
- {
- this->channel = 3;
- cout<<location << " TV channel is set for DVD/n";
- }
- };
- class Command
- {
- public:
- virtual void execute()=0;
- virtual void undo()=0;
- };
- class NoCommand :public Command
- {
- public:
- void execute() { }
- void undo() { }
- };
- class LightOnCommand :public Command
- {
- Light* light;
- public:
- LightOnCommand(Light* light)
- {
- this->light = light;
- }
- void execute()
- {
- light->on();
- }
- void undo()
- {
- light->off();
- }
- };
- class LightOffCommand :public Command
- {
- Light* light;
- public:
- LightOffCommand(Light* light)
- {
- this->light = light;
- }
- void execute()
- {
- light->off();
- }
- void undo()
- {
- light->on();
- }
- };
- class CeilingFanHighCommand :public Command
- {
- CeilingFan* ceilingFan;
- int prevSpeed;
- public:
- CeilingFanHighCommand(CeilingFan* ceilingFan)
- {
- this->ceilingFan = ceilingFan;
- }
- void execute()
- {
- prevSpeed = ceilingFan->getSpeed();
- ceilingFan->high();
- }
- void undo()
- {
- switch (prevSpeed)
- {
- case CeilingFan::HIGH:
- ceilingFan->high();
- break;
- case CeilingFan::MEDIUM:
- ceilingFan->medium();
- break;
- case CeilingFan::LOW:
- ceilingFan->low();
- break;
- default:
- ceilingFan->off();
- break;
- }
- }
- };
- class CeilingFanMediumCommand :public Command
- {
- CeilingFan* ceilingFan;
- int prevSpeed;
- public:
- CeilingFanMediumCommand(CeilingFan* ceilingFan)
- {
- this->ceilingFan = ceilingFan;
- }
- void execute()
- {
- prevSpeed = ceilingFan->getSpeed();
- ceilingFan->medium();
- }
- void undo()
- {
- switch (prevSpeed)
- {
- case CeilingFan::HIGH:
- ceilingFan->high();
- break;
- case CeilingFan::MEDIUM:
- ceilingFan->medium();
- break;
- case CeilingFan::LOW:
- ceilingFan->low();
- break;
- default:
- ceilingFan->off();
- break;
- }
- }
- };
- class CeilingFanLowCommand :public Command
- {
- CeilingFan* ceilingFan;
- int prevSpeed;
- public:
- CeilingFanLowCommand(CeilingFan* ceilingFan)
- {
- this->ceilingFan = ceilingFan;
- }
- void execute()
- {
- prevSpeed = ceilingFan->getSpeed();
- ceilingFan->low();
- }
- void undo()
- {
- switch (prevSpeed)
- {
- case CeilingFan::HIGH:
- ceilingFan->high();
- break;
- case CeilingFan::MEDIUM:
- ceilingFan->medium();
- break;
- case CeilingFan::LOW:
- ceilingFan->low();
- break;
- default:
- ceilingFan->off();
- break;
- }
- }
- };
- class CeilingFanOffCommand :public Command
- {
- CeilingFan* ceilingFan;
- int prevSpeed;
- public:
- CeilingFanOffCommand(CeilingFan* ceilingFan)
- {
- this->ceilingFan = ceilingFan;
- }
- void execute()
- {
- prevSpeed = ceilingFan->getSpeed();
- ceilingFan->off();
- }
- void undo()
- {
- switch (prevSpeed)
- {
- case CeilingFan::HIGH:
- ceilingFan->high();
- break;
- case CeilingFan::MEDIUM:
- ceilingFan->medium();
- break;
- case CeilingFan::LOW:
- ceilingFan->low();
- break;
- default:
- ceilingFan->off();
- break;
- }
- }
- };
- class HottubOnCommand :public Command
- {
- Hottub* hottub;
- public:
- HottubOnCommand(Hottub* hottub)
- {
- this->hottub = hottub;
- }
- void execute()
- {
- hottub->on();
- hottub->setTemperature(104);
- hottub->circulate();
- }
- void undo()
- {
- hottub->off();
- }
- };
- class HottubOffCommand :public Command
- {
- Hottub* hottub;
- public:
- HottubOffCommand(Hottub* hottub)
- {
- this->hottub = hottub;
- }
- void execute()
- {
- hottub->setTemperature(98);
- hottub->off();
- }
- void undo()
- {
- hottub->on();
- }
- };
- class MacroCommand :public Command
- {
- Command** commands;
- int commandSize;
- public:
- MacroCommand(Command** commands,int commandSize)
- {
- this->commands = commands;
- this->commandSize=commandSize;
- }
- void execute()
- {
- for (int i = 0; i < commandSize; i++)
- {
- commands[i]->execute();
- }
- }
- void undo()
- {
- for (int i = commandSize - 1; i >= 0; i--)
- {
- commands[i]->undo();
- }
- }
- };
- class StereoOnCommand :public Command
- {
- Stereo* stereo;
- public:
- StereoOnCommand(Stereo* stereo)
- {
- this->stereo = stereo;
- }
- void execute()
- {
- stereo->on();
- }
- void undo()
- {
- stereo->off();
- }
- };
- class StereoOffCommand :public Command
- {
- Stereo* stereo;
- public:
- StereoOffCommand(Stereo* stereo)
- {
- this->stereo = stereo;
- }
- void execute()
- {
- stereo->off();
- }
- void undo()
- {
- stereo->on();
- }
- };
- class TVOnCommand :public Command
- {
- TV* tv;
- public:
- TVOnCommand(TV* tv)
- {
- this->tv = tv;
- }
- void execute()
- {
- tv->on();
- tv->setInputChannel();
- }
- void undo()
- {
- tv->off();
- }
- };
- class TVOffCommand :public Command
- {
- TV* tv;
- public:
- TVOffCommand(TV* tv)
- {
- this->tv = tv;
- }
- void execute()
- {
- tv->off();
- }
- void undo()
- {
- tv->on();
- }
- };
- class RemoteControl
- {
- Command** onCommands;
- Command** offCommands;
- Command* undoCommand;
- public:
- RemoteControl()
- {
- onCommands = new Command*[7];
- offCommands = new Command*[7];
- Command* noCommand = new NoCommand();
- for (int i = 0; i < 7; i++)
- {
- onCommands[i] = noCommand;
- offCommands[i] = noCommand;
- }
- undoCommand = noCommand;
- }
- void setCommand(int slot, Command* onCommand, Command* offCommand)
- {
- onCommands[slot] = onCommand;
- offCommands[slot] = offCommand;
- }
- void onButtonWasPushed(int slot)
- {
- onCommands[slot]->execute();
- undoCommand = onCommands[slot];
- }
- void offButtonWasPushed(int slot)
- {
- offCommands[slot]->execute();
- undoCommand = offCommands[slot];
- }
- void undoButtonWasPushed()
- {
- undoCommand->undo();
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- RemoteControl* remoteControl = new RemoteControl();
- Light* light = new Light("Living Room");
- TV* tv = new TV("Living Room");
- Stereo* stereo = new Stereo("Living Room");
- Hottub* hottub = new Hottub();
- LightOnCommand* lightOn = new LightOnCommand(light);
- StereoOnCommand* stereoOn = new StereoOnCommand(stereo);
- TVOnCommand* tvOn = new TVOnCommand(tv);
- HottubOnCommand* hottubOn = new HottubOnCommand(hottub);
- LightOffCommand* lightOff = new LightOffCommand(light);
- StereoOffCommand* stereoOff = new StereoOffCommand(stereo);
- TVOffCommand* tvOff = new TVOffCommand(tv);
- HottubOffCommand* hottubOff = new HottubOffCommand(hottub);
- Command** partyOn =new Command*[4];
- partyOn[0]= lightOn ;
- partyOn[1]=stereoOn;
- partyOn[2]=tvOn;
- partyOn[3]=hottubOn;
- Command* partyOff[] = { lightOff, stereoOff, tvOff, hottubOff };
- MacroCommand* partyOnMacro = new MacroCommand(partyOn,4);
- MacroCommand* partyOffMacro = new MacroCommand(partyOff,4);
- remoteControl->setCommand(0, partyOnMacro, partyOffMacro);
- CeilingFan* ceilingFan = new CeilingFan("Living Room");
- CeilingFanHighCommand* ceilingFanHigh = new CeilingFanHighCommand(ceilingFan);
- CeilingFanMediumCommand* ceilingFanMedium = new CeilingFanMediumCommand(ceilingFan);
- CeilingFanLowCommand* ceilingFanLow = new CeilingFanLowCommand(ceilingFan);
- CeilingFanOffCommand* ceilingFanOff = new CeilingFanOffCommand(ceilingFan);
- remoteControl->setCommand(1, ceilingFanHigh, ceilingFanOff);
- remoteControl->setCommand(2, ceilingFanMedium, ceilingFanOff);
- remoteControl->setCommand(3, ceilingFanLow, ceilingFanOff);
- //System.out.println(remoteControl);
- cout<<"--- Pushing Macro On---/n";
- remoteControl->onButtonWasPushed(0);
- cout<<"--- Pushing Macro Off---/n";
- remoteControl->offButtonWasPushed(0);
- remoteControl->onButtonWasPushed(1);
- remoteControl->offButtonWasPushed(1);
- //System.out.println(remoteControl);
- remoteControl->undoButtonWasPushed();
- remoteControl->onButtonWasPushed(2);
- //System.out.println(remoteControl);
- remoteControl->undoButtonWasPushed();
- return 0;
- }
结果;