static void Main(string[] args) { RemoteControlInfo remoteControl = new RemoteControlInfo(); LightInfo livingRoomLight = new LightInfo("Living Room"); LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight); LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight); remoteControl.SetCommand(0, livingRoomLightOn, livingRoomLightOff); Console.WriteLine("--== Living Room Light ==--"); remoteControl.OnButtonWasPushed(0); remoteControl.UndoButtonWasPushed(); remoteControl.OffButtonWasPushed(0); Console.WriteLine(); CeilingFanInfo drawingRoomCeilingFan = new CeilingFanInfo("Drawing Room"); CeilingFanHighCommand drawingRoomCeilingFanHigh = new CeilingFanHighCommand(drawingRoomCeilingFan); CeilingFanLowCommand drawingRoomCeilingFanLow = new CeilingFanLowCommand(drawingRoomCeilingFan); remoteControl.SetCommand(1, drawingRoomCeilingFanHigh, drawingRoomCeilingFanLow); Console.WriteLine("--== Drawing Room Ceiling Fan ==--"); remoteControl.OnButtonWasPushed(1); remoteControl.UndoButtonWasPushed(); remoteControl.OffButtonWasPushed(1); Console.ReadLine(); }
static void Main(string[] args) { RemoteControlWithUndo remoteControl = new RemoteControlWithUndo(); Light livingRoomLight = new Light(false, "Living Room"); LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight); LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight); remoteControl.SetCommand(0, livingRoomLightOn, livingRoomLightOff); remoteControl.OnButtonWasPushed(0); remoteControl.OffButtonWasPushed(0); remoteControl.UndoButtonWasPushed(); CeilingFan livingRoomCeilingFan = new CeilingFan("Living Room", CeilingFan.Off); CeilingFanHighCommand ceilingFanHighCommand = new CeilingFanHighCommand(livingRoomCeilingFan); CeilingFanLowCommand ceilingFanLowCommand = new CeilingFanLowCommand(livingRoomCeilingFan); CeilingFanOffCommand ceilingFanOffCommand = new CeilingFanOffCommand(livingRoomCeilingFan); remoteControl.SetCommand(1, ceilingFanHighCommand, ceilingFanOffCommand); remoteControl.SetCommand(2, ceilingFanLowCommand, ceilingFanOffCommand); remoteControl.OnButtonWasPushed(1); remoteControl.OffButtonWasPushed(1); remoteControl.UndoButtonWasPushed(); remoteControl.OnButtonWasPushed(2); remoteControl.UndoButtonWasPushed(); }
static void Main(string[] args) { var remoteControl = new RemoteControl(); var light = new Light(); var stereo = new Stereo(); var garageDoor = new GarageDoor(); var ceilingFan = new CeilingFan(); var lightOnCommand = new LightOnCommand(light); var lightOffCommand = new LightOffCommand(light); var stereoOnWithCdCommand = new StereoOnWithCDCommand(stereo); var stereoOnWithDvdCommand = new StereoOnWithDVDCommand(stereo); var stereoOffCommand = new StereoOffCommand(stereo); var garageDoorOpenCommand = new GarageDoorOpenCommand(garageDoor); var garageDoorCloseCommand = new GarageDoorCloseCommand(garageDoor); var ceilingFanHighCommand = new CeilingFanHighCommand(ceilingFan); var ceilingFanLowCommand = new CeilingFanLowCommand(ceilingFan); var ceilingFanOffCommand = new CeilingFanOffCommand(ceilingFan); var partyOnCommand = new MacroCommand(lightOnCommand, stereoOnWithDvdCommand, ceilingFanHighCommand, garageDoorOpenCommand); var partyOffCommand = new MacroCommand(lightOffCommand, stereoOffCommand, ceilingFanOffCommand, garageDoorCloseCommand); remoteControl.SetCommand(0, lightOnCommand, lightOffCommand); remoteControl.SetCommand(1, garageDoorOpenCommand, garageDoorCloseCommand); remoteControl.SetCommand(2, stereoOnWithCdCommand, stereoOffCommand); remoteControl.SetCommand(3, stereoOnWithDvdCommand, stereoOffCommand); remoteControl.OnButtonPressed(0); remoteControl.OffButtonPressed(0); remoteControl.OnUndoButtonPressed(); remoteControl.OnButtonPressed(1); remoteControl.OffButtonPressed(1); remoteControl.OnButtonPressed(2); remoteControl.OffButtonPressed(2); remoteControl.OnUndoButtonPressed(); remoteControl.OnButtonPressed(3); remoteControl.OnUndoButtonPressed(); remoteControl.OffButtonPressed(3); remoteControl.OnUndoButtonPressed(); remoteControl.OnUndoButtonPressed(); remoteControl.OnUndoButtonPressed(); remoteControl.OnUndoButtonPressed(); Console.WriteLine(); remoteControl.SetCommand(2, ceilingFanLowCommand, ceilingFanOffCommand); remoteControl.SetCommand(3, ceilingFanHighCommand, ceilingFanOffCommand); remoteControl.SetCommand(4, partyOnCommand, partyOffCommand); remoteControl.OnButtonPressed(2); remoteControl.OnButtonPressed(3); remoteControl.OnUndoButtonPressed(); remoteControl.OffButtonPressed(3); remoteControl.OnButtonPressed(4); remoteControl.OffButtonPressed(4); remoteControl.OnUndoButtonPressed(); Console.ReadLine(); }
static void Main(string[] args) { RemoteControl Remote = new RemoteControl(); Light Light = new Light(); LightOnCommand LightOn = new LightOnCommand(Light); LightOffCommand LightOff = new LightOffCommand(Light); CeilingFan Fan = new CeilingFan("Living room"); CeilingFanHighCommand FanHigh = new CeilingFanHighCommand(Fan); CeilingFanLowCommand FanLow = new CeilingFanLowCommand(Fan); CeilingFanMediumCommand FanMedium = new CeilingFanMediumCommand(Fan); CeilingFanOffCommand FanOff = new CeilingFanOffCommand(Fan); GarageDoor GarageDoor = new GarageDoor(); GarageCloseDoorCommand GarageCloseDoor = new GarageCloseDoorCommand(GarageDoor); GarageOpenDoorCommand GarageOpenDoor = new GarageOpenDoorCommand(GarageDoor); ICommand[] OnCommands = { LightOn, FanHigh, GarageOpenDoor }; ICommand[] OffCommands = { LightOff, FanOff, GarageCloseDoor }; MacroCommand MacroOnCommand = new MacroCommand(OnCommands); MacroCommand MacroOffCommand = new MacroCommand(OffCommands); Remote.SetCommand(0, LightOn, LightOff); Remote.SetCommand(1, FanMedium, FanOff); Remote.SetCommand(2, FanHigh, FanOff); Remote.SetCommand(3, MacroOnCommand, MacroOnCommand); Console.WriteLine(Remote.ToString()); Remote.OnButtonPressed(3); Remote.UndoButtonWasPressed(); //Remote.OnButtonPressed(0); //Remote.UndoButtonWasPressed(); //Remote.OnButtonPressed(1); //Remote.OffButtonPressed(1); //Console.WriteLine(Remote.ToString()); //Remote.UndoButtonWasPressed(); //Remote.OnButtonPressed(2); //Console.WriteLine(Remote.ToString()); //Remote.UndoButtonWasPressed(); }
public static void Run() { // Invoker RemoteControlWithUndo remoteControl = new RemoteControlWithUndo(); // Receivers 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"); // Commands LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight); LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight); LightOnCommand kitchenLightOn = new LightOnCommand(kitchenLight); LightOffCommand kitchenLightOff = new LightOffCommand(kitchenLight); StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo); StereoOffCommand stereoOff = new StereoOffCommand(stereo); CeilingFanLowCommand ceilingFanLow = new CeilingFanLowCommand(ceilingFan); CeilingFanMediumCommand ceilingFanMedium = new CeilingFanMediumCommand(ceilingFan); CeilingFanHighCommand ceilingFanHigh = new CeilingFanHighCommand(ceilingFan); CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan); GarageDoorOpenCommand garageDoorUp = new GarageDoorOpenCommand(garageDoor); GarageDoorCloseCommand garageDoorDown = new GarageDoorCloseCommand(garageDoor); // Set Commands remoteControl.SetCommand(0, livingRoomLightOn, livingRoomLightOff); remoteControl.SetCommand(1, kitchenLightOn, kitchenLightOff); remoteControl.SetCommand(2, ceilingFanLow, ceilingFanOff); remoteControl.SetCommand(3, ceilingFanMedium, ceilingFanOff); remoteControl.SetCommand(4, ceilingFanHigh, ceilingFanOff); remoteControl.SetCommand(5, garageDoorUp, garageDoorDown); remoteControl.SetCommand(6, stereoOnWithCD, stereoOff); // Invoke Commands Console.WriteLine("=================================================="); Console.WriteLine("============= Testing Remote Loader ============="); Console.WriteLine("=================================================="); Console.WriteLine(); Console.WriteLine(remoteControl); Console.WriteLine(); for (int i = 0; i < remoteControl.numberOfSlots; i++) { remoteControl.OnButtonWasPushed(i); remoteControl.OffButtonWasPushed(i); if (!(remoteControl.onCommands[i] is NoCommand)) { Console.WriteLine(); } } Console.WriteLine(); Console.WriteLine("----- Undo -----"); remoteControl.UndoButtonWasPushed(); }
/* Esta clase contiene toda la lógica de nuestro patrón comando, * analicémosla detenidamente */ public static RemoteControl CreateControl() { /* Nuestro control es nuestro invocador, es el que da comandos y * los demás los ejecutan, veámoslo un poco más de cerca * entra a la clase RemoteControl Para analizarla */ RemoteControl control = new RemoteControl(); /* Como pudiste ver el control no tiene ningún conocimiento de como * se realizan los comandos, lo único que hace es llamarlos y ejecutarlos, * * el siguiente proceso crea a los recibidores o accionadores que son los * que se encargan de ejecutar y hacer realidad nuestros comandos * * entra en las clases para analizarlas */ ILight livingRoomLight = new LivingRoomLight(); CeilingFan ceilingFan = new CeilingFan(); ILight kitchenLight = new KitchenLight(); GarageDoor garageDoor = new GarageDoor(); Stereo stereo = new Stereo(); ILight allLights = new AllLights(); HotTub hotTub = new HotTub(); TV tv = new TV(); /* Como pudiste observar cada uno de estos receptores define como * ejecutar sus acciones; acontinuacionel en siguiente proceso podrás * observar cómo se crean los comandos para ser almacenados dentro de nuestro * control que funcionara como nuestro invocador. */ //Este proceso crea los comandos, recibiendo como parametro //la clase que ejecutara el proceso(receptor) //Entra a LightsOnCommands para analizarlo LightsOnCommands livingRoomLightsOn = new LightsOnCommands(livingRoomLight); LightsOffCommands livingRoomLightsOff = new LightsOffCommands(livingRoomLight); LightsOnCommands kitchenLightsOn = new LightsOnCommands(kitchenLight); LightsOffCommands kitchenLightsOff = new LightsOffCommands(kitchenLight); LightsOnCommands allLightsOn = new LightsOnCommands(allLights); LightsOffCommands allLightsOff = new LightsOffCommands(allLights); CeilingFanLowCommand ceilingFanOn = new CeilingFanLowCommand(ceilingFan); CeilingFanMediumCommand ceilingFanMedium = new CeilingFanMediumCommand(ceilingFan); CeilingFanHightCommand ceilingFanHight = new CeilingFanHightCommand(ceilingFan); CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan); GarageDoorOpenCommand garageDoorOpen = new GarageDoorOpenCommand(garageDoor); GarageDoorCloseCommand garageDoorClose = new GarageDoorCloseCommand(garageDoor); StereoWithCDOnCommand stereoWithCDOn = new StereoWithCDOnCommand(stereo); StereoWithCDOffCommand stereoWithCDOff = new StereoWithCDOffCommand(stereo); HotTubOnCommand hotTubOn = new HotTubOnCommand(hotTub); HotTubOffCommand hotTubOff = new HotTubOffCommand(hotTub); TvOffCommand tvOff = new TvOffCommand(tv); TvOnCommand tvOn = new TvOnCommand(tv); /* para el modo fiesta necesitaremos un comando especial, que pueda recibir una * serie de comandos y los ejecute, por eso fue creada la clase MacroCommand que * ejecuta una serie de comandos. */ ICommand[] PartyModeCommandsOn = { livingRoomLightsOn, stereoWithCDOn, tvOn, hotTubOn }; ICommand[] PartyModeCommandsOff = { livingRoomLightsOff, stereoWithCDOff, tvOff, hotTubOff }; MacroCommand PartyModeOn = new MacroCommand(PartyModeCommandsOn); MacroCommand PartyModeOff = new MacroCommand(PartyModeCommandsOff); //Al almacenar los comandos en el control habilitamos la funcionalidad //para que sea ejecutada al presionar algún boton, ve a la clase principal //del proyecto y ve cómo se ejecuta cada una de las acciones control.OnCommands[0] = livingRoomLightsOn; control.OffCommands[0] = livingRoomLightsOff; control.OnCommands[1] = kitchenLightsOn; control.OffCommands[1] = kitchenLightsOff; control.OnCommands[2] = allLightsOn; control.OffCommands[2] = allLightsOff; control.OnCommands[3] = ceilingFanOn; control.OffCommands[3] = ceilingFanOff; control.OnCommands[4] = ceilingFanMedium; control.OffCommands[4] = ceilingFanOff; control.OnCommands[5] = ceilingFanHight; control.OffCommands[5] = ceilingFanOff; control.OnCommands[6] = garageDoorOpen; control.OffCommands[6] = garageDoorClose; control.OnCommands[7] = stereoWithCDOn; control.OffCommands[7] = stereoWithCDOff; control.OnCommands[8] = PartyModeOn; control.OffCommands[8] = PartyModeOff; control.OnCommands[9] = new NoCommand(); control.OffCommands[9] = new NoCommand(); return(control); }
static void Main(string[] args) { int numSlots = 5; BetterRemoteControl remote = new BetterRemoteControl(numSlots); Light livingRoomLight = new Light("Living Room"); Light kitchenLght = new Light("Kitchen"); CeilingFan ceilingFan = new CeilingFan("Living Room"); GarageDoor garageDoor = new GarageDoor("Downstairs"); Stereo stereo = new Stereo("Living Room"); LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight); LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight); LightOnCommand kitchenLightOn = new LightOnCommand(livingRoomLight); LightOffCommand kitchenLightOff = new LightOffCommand(livingRoomLight); CeilingFanHighCommand ceilingFanOnHigh = new CeilingFanHighCommand(ceilingFan); CeilingFanMediumCommand ceilingFanOnMedium = new CeilingFanMediumCommand(ceilingFan); CeilingFanLowCommand ceilingFanOnLow = new CeilingFanLowCommand(ceilingFan); CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan); GarageDoorOpenCommand garageOpen = new GarageDoorOpenCommand(garageDoor); GarageDoorCloseCommand garageClose = new GarageDoorCloseCommand(garageDoor); StereoOnWithCdCommand stereoOnWithCd = new StereoOnWithCdCommand(stereo); StereoOffCommand stereoOff = new StereoOffCommand(stereo); Command[] partyOn = { livingRoomLightOn, ceilingFanOnHigh, stereoOnWithCd }; Command[] partyOff = { livingRoomLightOff, ceilingFanOff, stereoOff }; MacroCommand partyOnMacro = new MacroCommand(partyOn); MacroCommand partOffMacro = new MacroCommand(partyOff); Command[] testPartyFanOn = { ceilingFanOnHigh, ceilingFanOnMedium, ceilingFanOnLow }; Command[] testPartyFanOff = { ceilingFanOff }; MacroCommand testPartyFanOnMacro = new MacroCommand(testPartyFanOn); MacroCommand testPartyFanOffMacro = new MacroCommand(testPartyFanOff); //remote.SetCommand(0, livingRoomLightOn, livingRoomLightOff); //remote.SetCommand(1, kitchenLightOn, kitchenLightOff); //remote.SetCommand(2, ceilingFanOnHigh, ceilingFanOff); //remote.SetCommand(3, garageOpen, garageClose); //remote.SetCommand(4, stereoOnWithCd, stereoOff); remote.SetCommand(0, ceilingFanOnHigh, ceilingFanOff); remote.SetCommand(1, ceilingFanOnMedium, ceilingFanOff); remote.SetCommand(2, ceilingFanOnLow, ceilingFanOff); remote.SetCommand(3, partyOnMacro, partOffMacro); remote.SetCommand(4, testPartyFanOnMacro, testPartyFanOffMacro); Console.WriteLine(remote); Console.WriteLine("\n Changing fan settings several times"); remote.OnButtonWasPushed(0); remote.OnButtonWasPushed(1); remote.OnButtonWasPushed(2); Console.WriteLine("\n Undoing fan settings several times"); remote.UndoButtonWasPushed(); remote.UndoButtonWasPushed(); remote.UndoButtonWasPushed(); remote.UndoButtonWasPushed(); remote.UndoButtonWasPushed(); Console.WriteLine("\nTurning on macro"); remote.OnButtonWasPushed(3); //Console.WriteLine("\nTurning off macro"); //remote.OffButtonWasPushed(3); Console.WriteLine("\nUndoing previous macro button push"); //Though this eventually calls 3 Undo()'s (one for each of the 3 classes in the MacroCommand's Command[]), the stack in //remote control class actually only has 1 object in it, the MacroCommand object remote.UndoButtonWasPushed(); //This won't get called because stack size is not > 0 remote.UndoButtonWasPushed(); Console.WriteLine("\nTesting out how fan's handle the macro class"); remote.OnButtonWasPushed(4); Console.WriteLine("\nUndoing the fan macro class"); remote.UndoButtonWasPushed(); //remote.OnButtonWasPushed(1); //remote.UndoButtonWasPushed(); //remote.OnButtonWasPushed(1); //remote.OffButtonWasPushed(1); //remote.OnButtonWasPushed(2); //remote.OffButtonWasPushed(2); //remote.OnButtonWasPushed(3); //remote.OffButtonWasPushed(3); //remote.OnButtonWasPushed(4); //remote.OffButtonWasPushed(4); Console.ReadLine(); }