static void Main(string[] args) { RemoteControl remote = new RemoteControl(); Light livingRoomLight = new Light(); Stereo stereo = new Stereo(); CeilingFan ceilingFan = new CeilingFan("Living Room"); LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight); LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight); StereoOnCommand stereoOn = new StereoOnCommand(stereo); StereoOffCommand stereoOff = new StereoOffCommand(stereo); CeilingFanHighCommand ceilingFanHigh = new CeilingFanHighCommand(ceilingFan); CeilingFanMediumCommand ceilingFanMedium = new CeilingFanMediumCommand(ceilingFan); CeilingFanLowCommand ceilingFanLow = new CeilingFanLowCommand(ceilingFan); CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan); Console.WriteLine(remote.ToString()); remote.SetCommand(0, livingRoomLightOn, livingRoomLightOff); remote.SetCommand(1, stereoOn, stereoOff); remote.SetCommand(2, ceilingFanHigh, ceilingFanOff); remote.SetCommand(3, ceilingFanMedium, ceilingFanOff); remote.SetCommand(4, ceilingFanLow, ceilingFanOff); Console.WriteLine(remote.ToString()); //remote.OnButtonWasPushed(0); //remote.OnButtonWasPushed(1); //remote.OffButtonWasPushed(0); //remote.OffButtonWasPushed(1); //remote.UndoButtonWasPushed(); //remote.OnButtonWasPushed(2); //remote.OnButtonWasPushed(3); //remote.UndoButtonWasPushed(); //remote.OffButtonWasPushed(2); //remote.UndoButtonWasPushed(); Command[] onCommands = { livingRoomLightOn, stereoOn, ceilingFanHigh }; MacroCommand macroCommandOn = new MacroCommand(onCommands); Command[] offCommands = { livingRoomLightOff, stereoOff, ceilingFanOff }; MacroCommand macroCommandOff = new MacroCommand(offCommands); remote.SetCommand(5, macroCommandOn, macroCommandOff); remote.OnButtonWasPushed(5); Console.WriteLine(); //remote.OffButtonWasPushed(5); remote.UndoButtonWasPushed(); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); var remote = new SimpleRemoteControl(); var kitchenLight = new Light("kitchen light"); var livingLight = new Light("living room light"); var garageDoor = new GarageDoor(); var stereo = new Stereo(); var kitLightOnCommand = new LightOnCommand(kitchenLight); var kitLightOffCommand = new LightOffCommand(kitchenLight); var lightOnCommand = new LightOnCommand(livingLight); var lightOffCommand = new LightOffCommand(livingLight); var garageDoorOpenCommand = new GarageDoorOpenCommand(garageDoor); var garageDoorCloseCommand = new GarageDoorCloseCommand(garageDoor); var stereoOnWithCDCommand = new StereoOnWithCDCommand(stereo); var stereoOffCommand = new StereoOffCommand(stereo); var ceilingFan = new CeilingFan("Ceiling fan"); var ceilingFanHighCommand = new CeilingFanHighCommand(ceilingFan); var ceilingFanLowCommand = new CeilingFanLowCommand(ceilingFan); var ceilingFanOffCommand = new CeilingFanOffCommand(ceilingFan); remote.SetCommand(0, lightOnCommand, lightOffCommand); remote.SetCommand(1, kitLightOnCommand, kitLightOffCommand); remote.SetCommand(2, garageDoorOpenCommand, garageDoorCloseCommand); remote.SetCommand(3, stereoOnWithCDCommand, stereoOffCommand); remote.SetCommand(4, ceilingFanHighCommand, ceilingFanOffCommand); remote.SetCommand(5, ceilingFanLowCommand, ceilingFanOffCommand); remote.OnButtonPressed(0); remote.OffButtonPressed(0); remote.OnButtonPressed(1); remote.OffButtonPressed(1); remote.OnButtonPressed(2); remote.OffButtonPressed(2); remote.OnButtonPressed(3); remote.OffButtonPressed(3); remote.OnButtonPressed(4); remote.OffButtonPressed(4); remote.UndoButtonPressed(); remote.OnButtonPressed(5); remote.OffButtonPressed(5); remote.UndoButtonPressed(); }
static void Main(string[] args) { SimpleRemoteControl remote = new SimpleRemoteControl(); Light livingRoomLight = new Light("Living Romm"); Light kitchenRoomLight = new Light("kitchen Romm"); GarageDoor garageDoor = new GarageDoor(""); CeilingFan ceilingFan = new CeilingFan("Living Room"); Stereo stereo = new Stereo("Living Room"); LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight); LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight); LightOnCommand kitchenRoomLightOn = new LightOnCommand(kitchenRoomLight); LightOffCommand kitchenRoomLightOff = new LightOffCommand(kitchenRoomLight); CeilingFanHighCommand ceilingFanOn = new CeilingFanHighCommand(ceilingFan); CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan); GarageDoorOpenCommand garageDoorOpenCommand = new GarageDoorOpenCommand(garageDoor); GarageDoorCloseCommand garageDoorCloseCommand = new GarageDoorCloseCommand(garageDoor); StereoOnWithCDCommand stereoOnWithCdCommand = new StereoOnWithCDCommand(stereo); StereoOffCommand stereoOffCommand = new StereoOffCommand(stereo); remote.SetCommand(0, livingRoomLightOn, livingRoomLightOff); remote.SetCommand(1, kitchenRoomLightOn, kitchenRoomLightOff); remote.SetCommand(2, ceilingFanOn, ceilingFanOff); remote.SetCommand(3, stereoOnWithCdCommand, stereoOffCommand); Console.WriteLine(remote.ToString()); remote.OnButtonWasPressed(0); remote.OffButtonWasPressed(0); remote.OnButtonWasPressed(1); remote.OffButtonWasPressed(1); remote.OnButtonWasPressed(2); remote.OffButtonWasPressed(2); remote.OnButtonWasPressed(3); remote.OffButtonWasPressed(3); Console.ReadLine(); }
static void Main(string[] args) { CeilingFan cf = new CeilingFan(); CeilingFanHighCommand cfh = new CeilingFanHighCommand(cf); CeilingFanLowCommand cfl = new CeilingFanLowCommand(cf); Control ctr = new Control(); // 假设 button0, button1 分别为高速低速 ctr.SetCommand(0, cfh); ctr.SetCommand(1, cfl); ctr.OnButtonWasPressed(0); ctr.OnButtonWasPressed(1); ctr.UndoButtonWasPressed(); ctr.RedoButtonWasPressed(); }
void Start() { var lightObj = new Command.Receiver.Light(); ICommand command = new LightOnCommand(lightObj); _simpleRemoteController.SetCommand(0, command); _simpleRemoteController.CommandExecute(0); var ceilingFan = new CeilingFan(); ICommand ceilingFanCommand = new CeilingFanHighCommand(ceilingFan); _simpleRemoteController.SetCommand(1, ceilingFanCommand); _simpleRemoteController.CommandExecute(1); _simpleRemoteController.Undo(); var macroCommand = new MacroCommand(new [] { command, ceilingFanCommand }); _simpleRemoteController.SetCommand(2, macroCommand); _simpleRemoteController.CommandExecute(2); _simpleRemoteController.Undo(); }
static void Main(string[] args) { Light light = new Light(); GarageDoor garageDoor = new GarageDoor(); LightOnCommand lightOn = new LightOnCommand(light); LightOffCommand lightOff = new LightOffCommand(light); GarageDoorOpenCommand garageDoorOpen = new GarageDoorOpenCommand(garageDoor); GarageDoorDownCommand garageDoorDown = new GarageDoorDownCommand(garageDoor); //01.简单控制器-------------------------- Console.WriteLine("\n-----01.简单控制器-----\n"); SimpleRemoteControl remote = new SimpleRemoteControl(); remote.SetCommand(lightOn); remote.ButtonWasPressed(); remote.SetCommand(garageDoorOpen); remote.ButtonWasPressed(); //02.复杂控制器-------------------------------------- Console.WriteLine("\n-----02.复杂控制器-----\n"); RemoteControl remoteControl = new RemoteControl(); remoteControl.SetCommand(0, lightOn, lightOff); remoteControl.SetCommand(1, garageDoorOpen, garageDoorDown); Console.WriteLine(remoteControl.ToString()); remoteControl.OnButtonWasPushed(0); remoteControl.OffButtonWasPushed(0); remoteControl.OnButtonWasPushed(1); remoteControl.OffButtonWasPushed(1); //03.复杂控制器,撤销功能-------------------------------------- Console.WriteLine("\n-----03.复杂控制器,撤销功能-----\n"); RemoteControlWithUndo remoteControlWithUndo = new RemoteControlWithUndo(); remoteControlWithUndo.SetCommand(0, lightOn, lightOff); remoteControlWithUndo.OnButtonWasPushed(0); remoteControlWithUndo.OffButtonWasPushed(0); Console.WriteLine(remoteControlWithUndo.ToString()); remoteControlWithUndo.UndoButtonWasPushed(); remoteControlWithUndo.OffButtonWasPushed(0); remoteControlWithUndo.OnButtonWasPushed(0); Console.WriteLine(remoteControlWithUndo.ToString()); remoteControlWithUndo.UndoButtonWasPushed(); //04.复杂撤销功能-天花板吊扇 Console.WriteLine("\n-----04.复杂撤销功能-天花板吊扇-----\n"); remoteControlWithUndo = new RemoteControlWithUndo(); CeilingFan ceilingFan = new CeilingFan("Living Room"); CeilingFanHighCommand ceilingFanHighCommand = new CeilingFanHighCommand(ceilingFan); CeilingFanMediumCommand ceilingFanMediumCommand = new CeilingFanMediumCommand(ceilingFan); CeilingFanOffCommand ceilingFanOffCommand = new CeilingFanOffCommand(ceilingFan); remoteControlWithUndo.SetCommand(0, ceilingFanHighCommand, ceilingFanOffCommand); //0号插槽的On键设置为高速,Off键设置为关闭 remoteControlWithUndo.SetCommand(1, ceilingFanMediumCommand, ceilingFanOffCommand); //1号插槽的On键设置为中速,Off键设置为关闭 remoteControlWithUndo.OnButtonWasPushed(0); //首先以高速开启吊扇 remoteControlWithUndo.OffButtonWasPushed(0); //然后关闭 Console.WriteLine(remoteControlWithUndo.ToString()); remoteControlWithUndo.UndoButtonWasPushed(); //撤销,回到中速 remoteControlWithUndo.OnButtonWasPushed(1); //开启中速 remoteControlWithUndo.OffButtonWasPushed(1); //关闭 Console.WriteLine(remoteControlWithUndo.ToString()); //撤销,回到中速 remoteControlWithUndo.UndoButtonWasPushed(); //05.Party模式 Console.WriteLine("\n-----05.Party模式-----\n"); ICommand[] partyOn = { lightOn, garageDoorOpen, ceilingFanHighCommand }; //一个数组用来记录开启命令 ICommand[] partyOff = { lightOff, garageDoorDown, ceilingFanOffCommand }; //另一个数组用来记录关闭命令 MacroCommand partyOnMacroCommand = new MacroCommand(partyOn); //创建对应的宏持有开启命令数组 MacroCommand partyOffMacroCommand = new MacroCommand(partyOff); //创建对应的宏持有关闭命令数组 remoteControlWithUndo = new RemoteControlWithUndo(); remoteControlWithUndo.SetCommand(0, partyOnMacroCommand, partyOffMacroCommand);//将宏命令指定一个按钮 Console.WriteLine(remoteControlWithUndo); Console.WriteLine("----Pushing Macro On----"); remoteControlWithUndo.OnButtonWasPushed(0); Console.WriteLine("----Pushing Macro Off----"); remoteControlWithUndo.OffButtonWasPushed(0); Console.WriteLine("----Pushing Macro Undo----"); remoteControlWithUndo.UndoButtonWasPushed(); Console.ReadKey(); }