public void Load() { RemoteControlWithUndo remoteControl = new RemoteControlWithUndo(); Light livingRoomLight = new Light("Living Room"); Light kitchenLight = new Light("Kitchen"); Light light = new Light(); TV tv = new TV(); Hottub hottub = new Hottub(); CeilingFan ceilingFan = new CeilingFan("Living Room"); GarageDoor garageDoor = new GarageDoor(); Stereo stereo = new Stereo("Living Room"); LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight); LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight); LightOnCommand kitchenLightOn = new LightOnCommand(kitchenLight); LightOffCommand kitchenLightOff = new LightOffCommand(kitchenLight); CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan); CeilingFanHighCommand ceilingFanHigh = new CeilingFanHighCommand(ceilingFan); CeilingFanMediumCommand ceilingFanMedium = new CeilingFanMediumCommand(ceilingFan); GarageDoorUpCommand garageDoorUp = new GarageDoorUpCommand(garageDoor); GarageDoorDownCommand garageDoorDown = new GarageDoorDownCommand(garageDoor); StereoOnWithCDCommand stereoOnWithCd = new StereoOnWithCDCommand(stereo); StereoOffCommand stereoOff = new StereoOffCommand(stereo); 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); 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, livingRoomLightOn, livingRoomLightOff); remoteControl.SetCommand(1, kitchenLightOn, kitchenLightOff); remoteControl.SetCommand(2, ceilingFanHigh, ceilingFanOff); remoteControl.SetCommand(3, ceilingFanMedium, ceilingFanOff); remoteControl.SetCommand(4, stereoOnWithCd, stereoOff); remoteControl.SetCommand(5, garageDoorUp, garageDoorDown); remoteControl.SetCommand(6, partyOnMacro, partyOffMacro); Console.WriteLine(remoteControl); for (int i = 0; i <= 6; i++) { remoteControl.OnButtonWasPushed(i); remoteControl.OffButtonWasPushed(i); remoteControl.UndoButtonWasPushed(); } }
public static void CommandTest() { // 实例化遥控器 RemoteControl remoteControl = new RemoteControl(); // 实例化需要控制对象,并传入房子位置 Stereo stereo = new Stereo("客厅"); Light light = new Light("客厅"); TV tv = new TV("卧室"); // 调用设备开关方法 StereoOnWithCDCommand stereoOnWichCD = new StereoOnWithCDCommand(stereo); StereoOffCommand stereoOffWithCD = new StereoOffCommand(stereo); LightOnCommand lightOn = new LightOnCommand(light); LightOffCommand lightOff = new LightOffCommand(light); TVOnCommand tvOn = new TVOnCommand(tv); TVOffCommand tvOff = new TVOffCommand(tv); // 设置插槽位置(遥控器的哪个按钮对应哪个设备开关) remoteControl.SetCommand(0, lightOn, lightOff); remoteControl.SetCommand(3, stereoOnWichCD, stereoOffWithCD); remoteControl.SetCommand(5, tvOn, tvOff); // 输出插槽位置 Console.WriteLine(remoteControl); // 按下开关 remoteControl.OnButtonWasPushed(0); remoteControl.OffButtonWasPushed(0); remoteControl.OnButtonWasPushed(3); remoteControl.OffButtonWasPushed(3); remoteControl.OnButtonWasPushed(5); remoteControl.OffButtonWasPushed(5); }
private static void CommandPattern() { RemoteControlWithUndo remoteControl = new RemoteControlWithUndo(); 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); Console.WriteLine(remoteControl); Console.WriteLine("--- Pushing Macro On ---"); remoteControl.onButtonWasPushed(0); Console.WriteLine("--- Pushing Macro Off ---"); remoteControl.offButtonWasPushed(0); }
public static void MacroCommandExample() { 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); ICommand[] partyOn = { lightOn, stereoOn, tvOn, hottubOn }; ICommand[] partyOff = { lightOff, stereoOff, tvOff, hottubOff }; MacroCommand partyOnMacro = new MacroCommand(partyOn); MacroCommand partyOffMacro = new MacroCommand(partyOff); RemoteControl remoteControl = new RemoteControl(); remoteControl.SetCommand(0, partyOnMacro, partyOffMacro); Console.WriteLine(remoteControl.ToString()); Console.WriteLine("\n--- Pushing Macro On---"); remoteControl.OnButtonPushed(0); Console.WriteLine("\n--- Pushing Macro Off---"); remoteControl.OffButtonPushed(0); }
/// <summary> /// 命令模式 /// </summary> public static void CommandPatternMethods() { TV tv = new TV(); TVOffCommand offCommand = new TVOffCommand(tv); TVOnCommand onCommand = new TVOnCommand(tv); Invoke invoke = new Invoke(); invoke.command = onCommand; invoke.ExcuteCommand(); invoke.command = offCommand; invoke.ExcuteCommand(); }
static void Main(string[] args) { var remoteControl = new RemoteControl(); var light = new Light("Living Roon"); var tv = new TV("Living Room"); var stereo = new Stereo("Living Room"); var hottub = new Hottub(); var garageDoor = new GarageDoor(string.Empty); var lightOnCommand = new LightOnCommand(light); var lightOffCommand = new LightOffCommand(light); var stereoOnCommand = new StereoOnCommand(stereo); var stereoOffCommand = new StereoOffCommand(stereo); var tvOnCommand = new TVOnCommand(tv); var tvOffCommand = new TVOffCommand(tv); var hottubOnCommand = new HottubOnCommand(hottub); var hottubOffCommand = new HottubOffCommand(hottub); var partyOn = new ICommand[] { lightOnCommand, stereoOnCommand, tvOnCommand, hottubOnCommand }; var partyOff = new ICommand[] { lightOffCommand, stereoOffCommand, tvOffCommand, hottubOffCommand }; var partyOnMacro = new MacroCommand(partyOn); var partyOffMacro = new MacroCommand(partyOff); //remoteControl.SetCommand(0, lightOnCommand, lightOffCommand); //remoteControl.SetCommand(1, tvOnCommand, tvOffCommand); //remoteControl.SetCommand(2, stereoOnCommand, stereoOffCommand); //remoteControl.SetCommand(3, hottubOnCommand, hottubOffCommand); remoteControl.SetCommand(0, partyOnMacro, partyOffMacro); remoteControl.SetCommand(6, new GarageDoorUpCommand(garageDoor), new GarageDoorDownCommand(garageDoor)); Console.WriteLine(remoteControl); remoteControl.OnButtonPressed(2); remoteControl.OnButtonPressed(0); remoteControl.OnButtonPressed(3); remoteControl.OnButtonPressed(1); remoteControl.OffButtonPressed(2); remoteControl.OffButtonPressed(0); remoteControl.OffButtonPressed(3); remoteControl.OffButtonPressed(1); remoteControl.OnButtonPressed(6); remoteControl.UndoButtonPressed(); Console.Read(); }
public static void Test() { RemoteControl remoteControl = new RemoteControl(); Light livingRoomLight = new Light("Living Room"); Light kitchenLight = new Light("Kitchen"); CeilingFan ceilingFan = new CeilingFan("Living Room"); GarageDoor garageDoor = new GarageDoor("Garage"); Stereo stereo = new Stereo("Living Room"); TV tv = new TV("Living Room"); Hottub hottub = new Hottub(); LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight); LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight); LightOnCommand kitchenLightOn = new LightOnCommand(kitchenLight); LightOffCommand kitchenLightOff = new LightOffCommand(kitchenLight); CeilingFanOnCommand ceilingFanOn = new CeilingFanOnCommand(ceilingFan); CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan); GarageDoorUpCommand garageDoorUp = new GarageDoorUpCommand(garageDoor); GarageDoorDownCommand garageDoorDown = new GarageDoorDownCommand(garageDoor); StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo); StereoOffCommand stereoOff = new StereoOffCommand(stereo); TVOnCommand tvOn = new TVOnCommand(tv); TVOffCommand tvOff = new TVOffCommand(tv); HottubOnCommand hottubOn = new HottubOnCommand(hottub); HottubOffCommand hottubOff = new HottubOffCommand(hottub); Command[] partyOn = { livingRoomLightOn, stereoOnWithCD, tvOn, hottubOn }; Command[] partyOff = { livingRoomLightOff, stereoOff, tvOff, hottubOff }; MacroCommand partyOnCommand = new MacroCommand(partyOn); MacroCommand partyOffCommand = new MacroCommand(partyOff); remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff); remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff); remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff); remoteControl.setCommand(3, stereoOnWithCD, stereoOff); remoteControl.setCommand(4, partyOnCommand, partyOffCommand); Console.WriteLine(remoteControl); remoteControl.onButtonWasPushed(0); remoteControl.offButtonWasPushed(0); remoteControl.onButtonWasPushed(1); remoteControl.offButtonWasPushed(1); remoteControl.onButtonWasPushed(2); remoteControl.offButtonWasPushed(2); remoteControl.onButtonWasPushed(3); remoteControl.offButtonWasPushed(3); Console.WriteLine("-------------------------------------------------------------- Party Mode On -----------------------------------------------"); remoteControl.onButtonWasPushed(4); remoteControl.offButtonWasPushed(4); remoteControl.undoButtonPushed(); }
public static void Start() { // SimpleRemoteControl example //SimpleRemoteControl remote = new SimpleRemoteControl(); //Light light = new Light(); //LightOnCommand lightOn = new LightOnCommand(light); //GarageDoor garageDoor = new GarageDoor(); //GarageDoorOpenCommand garageOpen = new GarageDoorOpenCommand(garageDoor); //remote.SetCommand(lightOn); //remote.ButtonWasPressed(); //remote.SetCommand(garageOpen); //remote.ButtonWasPressed(); // RemoteControl example //RemoteControl remoteControl = new RemoteControl(); //Light livingRoomLight = new Light("Living Room"); //Light kitchenLight = new Light("Kitchen"); //CeilingFan ceilingFan = new CeilingFan("Living Room"); //GarageDoor garageDoor = new GarageDoor(); //Stereo stereo = new Stereo("Living Room"); //LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight); //LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight); //LightOnCommand kitchenLightOn = new LightOnCommand(kitchenLight); //LightOffCommand kitchenLightOff = new LightOffCommand(kitchenLight); //CeilingFanOnCommand ceilingFanOn = new CeilingFanOnCommand(ceilingFan); //CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan); //GarageDoorOpenCommand garageDoorUp = new GarageDoorOpenCommand(garageDoor); //GarageDoorCloseCommand garageDoorDown = new GarageDoorCloseCommand(garageDoor); //StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo); //StereoOffCommand stereoOff = new StereoOffCommand(stereo); //remoteControl.SetCommand(0, livingRoomLightOn, livingRoomLightOff); //remoteControl.SetCommand(1, kitchenLightOn, kitchenLightOff); //remoteControl.SetCommand(2, ceilingFanOn, ceilingFanOff); //remoteControl.SetCommand(3, stereoOnWithCD, stereoOff); //Console.WriteLine(remoteControl); //remoteControl.OnButtonWasPushed(0); //remoteControl.OffButtonWasPushed(0); //remoteControl.OnButtonWasPushed(1); //remoteControl.OffButtonWasPushed(1); //remoteControl.OnButtonWasPushed(2); //remoteControl.OffButtonWasPushed(2); //remoteControl.OnButtonWasPushed(3); //remoteControl.OffButtonWasPushed(3); // MacroCommand Example 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); ICommand[] partyOn = { lightOn, stereoOn, tvOn, hottubOn }; ICommand[] partyOff = { lightOff, stereoOff, tvOff, hottubOff }; MacroCommand partyOnMacro = new MacroCommand(partyOn); MacroCommand partyOffMacro = new MacroCommand(partyOff); remoteControl.SetCommand(0, partyOnMacro, partyOffMacro); Console.WriteLine(remoteControl); Console.WriteLine("---Pushing Macro On---"); remoteControl.OnButtonWasPushed(0); Console.WriteLine("---Pushing Macro Off---"); remoteControl.OffButtonWasPushed(0); }
public static void Run() { // Invoker RemoteControlWithUndo remoteControl = new RemoteControlWithUndo(); // Receivers Light livingRoomLight = new Light("Living Room"); TV tv = new TV("Living Room"); Stereo stereo = new Stereo("Living Room"); Hottub hottub = new Hottub("Outside"); // Commands LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight); LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight); TVOnCommand tvOn = new TVOnCommand(tv); TVOffCommand tvOff = new TVOffCommand(tv); StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo); StereoOffCommand stereoOff = new StereoOffCommand(stereo); HottubOnCommand hottubOn = new HottubOnCommand(hottub); HottubOffCommand hottubOff = new HottubOffCommand(hottub); // Set Macro ICommand[] partyOn = { livingRoomLightOn, stereoOnWithCD, tvOn, hottubOn }; ICommand[] partyOff = { livingRoomLightOff, stereoOff, tvOff, hottubOff }; MacroCommand partyOnMacro = new MacroCommand(partyOn); MacroCommand partyOffMacro = new MacroCommand(partyOff); // Set Commands remoteControl.SetCommand(0, partyOnMacro, partyOffMacro); // Invoke Commands Console.WriteLine("=================================================="); Console.WriteLine("============= Pressing Macro Buttons ============="); Console.WriteLine("=================================================="); Console.WriteLine(); Console.WriteLine(remoteControl); Console.WriteLine(); for (int i = 0; i < remoteControl.numberOfSlots; i++) { remoteControl.OnButtonWasPushed(i); if (!(remoteControl.onCommands[i] is NoCommand)) { Console.WriteLine(); } remoteControl.OffButtonWasPushed(i); if (!(remoteControl.onCommands[i] is NoCommand)) { Console.WriteLine(); } } Console.WriteLine(); Console.WriteLine("----- Undo -----"); int undoSize = remoteControl.undoCommands.Count; for (int i = 0; i < undoSize; i++) { remoteControl.UndoButtonWasPushed(); } }