private static string ProcessRemoteControl() { RemoteControl remoteControl = new RemoteControl(); Light livingRoomLight = new Light("Living Room"); Light kitchenLight = new Light("Kitchen"); CeilingFan ceilingFan = new CeilingFan("Living Room"); GarageDoor garage = new GarageDoor(); Stereo stereo = new Stereo(); LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight); LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight); LightOnCommand kitchenLightOn = new LightOnCommand(kitchenLight); LightOffCommand kitchenLightOff = new LightOffCommand(kitchenLight); CeilingFanOnCommand ceilingFanOnCommand = new CeilingFanOnCommand(ceilingFan); CeilingFanOffCommand ceilingFanOffCommand = new CeilingFanOffCommand(ceilingFan); GarageDoorOpenCommand garageDoorOpen = new GarageDoorOpenCommand(garage); GarageDoorCloseCommand garageDoorClose = new GarageDoorCloseCommand(garage); StereoOnWithCDCommand stereoOnWithCDCommand = new StereoOnWithCDCommand(stereo); StereoOffCommand stereoOffCommand = new StereoOffCommand(stereo); remoteControl.OnCommands[0] = livingRoomLightOn; remoteControl.OnCommands[1] = kitchenLightOn; remoteControl.OnCommands[2] = ceilingFanOnCommand; remoteControl.OnCommands[3] = stereoOnWithCDCommand; remoteControl.OffCommands[0] = livingRoomLightOff; remoteControl.OffCommands[1] = kitchenLightOff; remoteControl.OffCommands[2] = ceilingFanOffCommand; remoteControl.OffCommands[3] = stereoOffCommand; StringBuilder sb = new StringBuilder(); sb.AppendLine(remoteControl.ToString()); sb.AppendLine(remoteControl.OnButtonWasPushed(0)); sb.AppendLine(remoteControl.OffButtonWasPushed(0)); sb.AppendLine(remoteControl.OnButtonWasPushed(1)); sb.AppendLine(remoteControl.OffButtonWasPushed(1)); sb.AppendLine(remoteControl.OnButtonWasPushed(2)); sb.AppendLine(remoteControl.OffButtonWasPushed(2)); sb.AppendLine(remoteControl.OnButtonWasPushed(3)); sb.AppendLine(remoteControl.OffButtonWasPushed(3)); return(sb.ToString()); }
/// <summary> /// The client or "customer" - responsible for creating the concrete command and setting its receiver /// Waitress/remote = invoker - holds a command and at some point asks the command to carry out a request /// Cook/vendor class = receiver - knows how to perform the work needed to carry out the request /// orderUp()/press button = setCommand() /// Order/Device action = command - invoked through its execute() method which asks the receiver to perform an action. Binds action to a receiver /// Customer/user = client /// takeOrder()/buttonPressed() = execute() - invokes actions on the receiver needed to fulfill the request /// </summary> /// <param name="args"></param> static void Main(string[] args) { SimpleRemoteControl remote = new SimpleRemoteControl(); // the INVOKER or "waitress" can be parameterized with different requests Light light = new Light(); // the RECEIVER of the request or "the cook" LightOnCommand lightOn = new LightOnCommand(light); // create a COMMAND and pass the receiver to it or "the order" GarageDoor door = new GarageDoor(); // the RECEIVER GarageDoorOpenCommand garageOpen = new GarageDoorOpenCommand(door); // the COMMAND remote.setCommand(lightOn); // pass the COMMAND to the INVOKER or "takeOrder() remote.buttonWasPressed(); // calls execute() or "orderUp()" remote.setCommand(garageOpen); remote.buttonWasPressed(); }
public static void Run() { var remote = new SimpleRemoteControl(); var light = new Light(); var lightOn = new LightOnCommand(light); remote.Command = lightOn; remote.ButtonWesPressed(); var garageDoor = new GarageDoor(); var garageDoorOpen = new GarageDoorOpenCommand(garageDoor); remote.Command = garageDoorOpen; remote.ButtonWesPressed(); }
private static string ProcessSimpleRemote() { StringBuilder sb = new StringBuilder(); SimpleRemoteControl remote = new SimpleRemoteControl(); Light light = new Light(); LightOnCommand lightOn = new LightOnCommand(light); GarageDoor garageDoor = new GarageDoor(); GarageDoorOpenCommand garageDoorOpenCommand = new GarageDoorOpenCommand(garageDoor); remote.Slot = lightOn; sb.AppendLine(remote.ButtonWasPressed()); remote.Slot = garageDoorOpenCommand; sb.AppendLine(remote.ButtonWasPressed()); return(sb.ToString()); }
public static void Run() { // Invoker RemoteControl remoteControl = new RemoteControl(); // 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); CeilingFanOnLowCommand ceilingFanOnLow = new CeilingFanOnLowCommand(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, stereoOnWithCD, stereoOff); remoteControl.SetCommand(3, ceilingFanOnLow, ceilingFanOff); remoteControl.SetCommand(4, garageDoorUp, garageDoorDown); Console.WriteLine(remoteControl); // Invoke Commands for (int i = 0; i < remoteControl.numberOfSlots; i++) { remoteControl.OnButtonWasPushed(i); remoteControl.OffButtonWasPushed(i); } }
static void TrySimpleRemoteControl() { Console.WriteLine("\n----- Simple remote control -----\n"); var simpleRemoteControl = new SimpleRemoteControl(); var light = new Light("Living room"); var lightsOnCommand = new LightsOnCommand(light); simpleRemoteControl.SetCommand(lightsOnCommand); simpleRemoteControl.PressTheButton(); var garageDoor = new GarageDoor(); var garageDoorOpenCommand = new GarageDoorOpenCommand(garageDoor); simpleRemoteControl.SetCommand(garageDoorOpenCommand); simpleRemoteControl.PressTheButton(); }
public void RemoteControl_ToString() { RemoteControl control = new RemoteControl(); GarageDoor door = new GarageDoor(); Light light = new Light(); CeilingFan fan = new CeilingFan(); GarageDoorOpenCommand openGarageCommand = new GarageDoorOpenCommand(door); GarageDoorCloseCommand closeGarageCommand = new GarageDoorCloseCommand(door); LightOffCommand lightOffCommand = new LightOffCommand(light); LightOnCommand lightOnCommand = new LightOnCommand(light); CeilingFanHighCommand fanHighCommand = new CeilingFanHighCommand(fan); CeilingFanOffCommand fanOffCommand = new CeilingFanOffCommand(fan); control.SetCommand(0, lightOnCommand, lightOffCommand); control.SetCommand(1, openGarageCommand, closeGarageCommand); control.SetCommand(2, fanHighCommand, fanOffCommand); Console.WriteLine(control.ToString()); }
public static void Test() { var remoteControl = new RemoteControl(); var livingRoomLight = new Light("Living Room"); var kitchenLight = new Light("Kitchen"); var ceilingFan = new CeilingFan("Living Room"); var garageDoor = new GarageDoor(); var stereo = new Stereo("Living Room"); var livingRoomLightOn = new LightOnCommand(livingRoomLight); var livingRoomLightOff = new LightOffCommand(livingRoomLight); var kitchenLightOn = new LightOnCommand(kitchenLight); var kitchenLightOff = new LightOffCommand(kitchenLight); var ceilingFanOn = new CeilingFanOnCommand(ceilingFan); var ceilingFanOff = new CeilingFanOffCommand(ceilingFan); var garageDoorUp = new GarageDoorOpenCommand(garageDoor); var garageDoorDown = new GarageDoorCloseCommand(garageDoor); var stereoOnWithCd = new StereoOnWithCdCommand(stereo); var stereoOff = new StereoOffCommand(stereo); remoteControl.SetCommand(0, livingRoomLightOn, livingRoomLightOff); remoteControl.SetCommand(1, kitchenLightOn, kitchenLightOff); remoteControl.SetCommand(2, ceilingFanOn, ceilingFanOff); remoteControl.SetCommand(3, garageDoorUp, garageDoorDown); remoteControl.SetCommand(4, stereoOnWithCd, stereoOff); Console.WriteLine(remoteControl); remoteControl.OnButtonWasPushed(0); remoteControl.OffButtonWasPushed(0); remoteControl.UndoButtonWasPushed(); remoteControl.OnButtonWasPushed(1); remoteControl.OffButtonWasPushed(1); remoteControl.OnButtonWasPushed(2); remoteControl.OffButtonWasPushed(2); remoteControl.OnButtonWasPushed(3); remoteControl.OffButtonWasPushed(3); remoteControl.OnButtonWasPushed(4); remoteControl.OffButtonWasPushed(4); }
public static void Run() { // Invoker SimpleRemoteControl remote = new SimpleRemoteControl(); // Receiver Light light = new Light(); // Command LightOnCommand lightOn = new LightOnCommand(light); remote.SetCommand(lightOn); remote.ButtonWasPressed(); GarageDoor garageDoor = new GarageDoor(); GarageDoorOpenCommand garageOpen = new GarageDoorOpenCommand(garageDoor); remote.SetCommand(garageOpen); remote.ButtonWasPressed(); }
public IControlebel GetCommand(CommandsType commandType, out ICommand onCommand, out ICommand offCommand) { IControlebel controlebel; switch (commandType) { case CommandsType.Light: controlebel = new Light(win); onCommand = new LightOnCommand(controlebel); offCommand = new LightOffCommand(controlebel); break; case CommandsType.Garage: controlebel = new Garage(win); onCommand = new GarageDoorOpenCommand(controlebel); offCommand = new GarageDoorCloseCommand(controlebel); break; case CommandsType.Stereo: controlebel = new Stereo(win); onCommand = new StereoOnWithCDCommand(controlebel); offCommand = new StereoOffCommand(controlebel); break; case CommandsType.MacroCommand: controlebel = default(IControlebel); onCommand = new MacroCommand(); offCommand = new MacroCommand(); break; default: controlebel = default(IControlebel); onCommand = default(ICommand); offCommand = default(ICommand); break; } return(controlebel); }
public void RemoteControl_Undo() { RemoteControl control = new RemoteControl(); GarageDoor door = new GarageDoor(); Light light = new Light(); CeilingFan fan = new CeilingFan(); GarageDoorOpenCommand openGarageCommand = new GarageDoorOpenCommand(door); GarageDoorCloseCommand closeGarageCommand = new GarageDoorCloseCommand(door); LightOffCommand lightOffCommand = new LightOffCommand(light); LightOnCommand lightOnCommand = new LightOnCommand(light); CeilingFanHighCommand fanHighCommand = new CeilingFanHighCommand(fan); CeilingFanOffCommand fanOffCommand = new CeilingFanOffCommand(fan); control.SetCommand(0, lightOnCommand, lightOffCommand); control.SetCommand(1, openGarageCommand, closeGarageCommand); control.SetCommand(2, fanHighCommand, fanOffCommand); control.ClickButtonOn(0); control.ClickButtonOn(1); control.ClickButtonOff(2); control.UndoButtonClick(); }
public void InitializeCommandTests() { // receivers Light kitchenLight = new Light("kitchen light"); Light bedroomLight = new Light("bedroom light"); GarageDoor garageDoor = new GarageDoor(); // commands LightOnCommand kitchenLightOn = new LightOnCommand(kitchenLight); LightOffCommand kitchenLightOff = new LightOffCommand(kitchenLight); LightOnCommand bedroomLightOn = new LightOnCommand(bedroomLight); LightOffCommand bedroomLightOff = new LightOffCommand(bedroomLight); GarageDoorOpenCommand garageOpenCmd = new GarageDoorOpenCommand(garageDoor); GarageDoorCloseCommand garageCloseCmd = new GarageDoorCloseCommand(garageDoor); // assign commands to remote this.remote = new Remote(); remote.SetCommand(0, kitchenLightOn, kitchenLightOff); remote.SetCommand(1, bedroomLightOn, bedroomLightOff); remote.SetCommand(2, garageOpenCmd, garageCloseCmd); Console.WriteLine(remote.ToString()); }
static void Main(string[] args) { RemoteControl remoteControl = new RemoteControl(); //GarageDoor GarageDoor garageDoor = new GarageDoor(); GarageDoorCloseCommand garageDoorCloseCommand = new GarageDoorCloseCommand(garageDoor); GarageDoorOpenCommand garageDoorOpenCommand = new GarageDoorOpenCommand(garageDoor); Light light = new Light(); LightOnCommand lightOnCommand = new LightOnCommand(light); LightOffCommand lightOffCommand = new LightOffCommand(light); TV tv = new TV(); TvOnCommand tvOnCommand = new TvOnCommand(tv); TvOffCommand tvOffCommand = new TvOffCommand(tv); Stereo stereo = new Stereo(); StereoOnCommand stereoOnCommand = new StereoOnCommand(stereo); StereoOffCommand stereoOffCommand = new StereoOffCommand(stereo); remoteControl.SetCommand(0, garageDoorOpenCommand, garageDoorCloseCommand); remoteControl.SetCommand(1, lightOnCommand, lightOffCommand); remoteControl.SetCommand(2, tvOnCommand, tvOffCommand); remoteControl.SetCommand(3, stereoOnCommand, stereoOffCommand); remoteControl.OnButtonWasPushed(0); remoteControl.OnButtonWasPushed(1); remoteControl.OnButtonWasPushed(2); remoteControl.OnButtonWasPushed(3); Console.WriteLine(remoteControl); remoteControl.OffButtonWasPushed(0); Console.ReadKey(); }
public void RemoteControl_Macro() { RemoteControl control = new RemoteControl(); GarageDoor door = new GarageDoor(); Light light = new Light(); CeilingFan fan = new CeilingFan(); GarageDoorOpenCommand openGarageCommand = new GarageDoorOpenCommand(door); GarageDoorCloseCommand closeGarageCommand = new GarageDoorCloseCommand(door); LightOffCommand lightOffCommand = new LightOffCommand(light); LightOnCommand lightOnCommand = new LightOnCommand(light); CeilingFanHighCommand fanHighCommand = new CeilingFanHighCommand(fan); CeilingFanOffCommand fanOffCommand = new CeilingFanOffCommand(fan); List <ICommand> onCommands = new List <ICommand>() { new GarageDoorOpenCommand(door), new LightOnCommand(light), new CeilingFanHighCommand(fan) }; List <ICommand> offCommands = new List <ICommand>() { new GarageDoorCloseCommand(door), new LightOffCommand(light), new CeilingFanOffCommand(fan) }; MacroCommand onMacro = new MacroCommand(onCommands); MacroCommand offMacro = new MacroCommand(offCommands); control.SetCommand(0, onMacro, offMacro); control.ClickButtonOn(0); control.ClickButtonOff(0); control.UndoButtonClick(); }
static void Main() { var remote = new SimpleRemoteControl(); var light = new Light(); var garageDoor = new GarageDoor(); var ceilingFan = new CeilingFan(); var lightOn = new LightOnCommand(light); var lightOff = new LightOffCommand(light); var garageDoorOpen = new GarageDoorOpenCommand(garageDoor); var garageDoorClose = new GarageDoorCloseCommand(garageDoor); var ceilingFanTurnOn = new CeilingFanTurnOnHighCommand(ceilingFan); var ceilingFanTurnOff = new CeilingFanTurnOffCommand(ceilingFan); remote.SetCommand(0, lightOn, lightOff); remote.SetCommand(1, garageDoorOpen, garageDoorClose); remote.SetCommand(2, ceilingFanTurnOn, ceilingFanTurnOff); Console.WriteLine(remote); remote.OnButtonWasPressed(0); remote.OffButtonWasPressed(0); remote.OnButtonWasPressed(1); remote.OffButtonWasPressed(1); remote.OnButtonWasPressed(2); //remote.OffButtonWasPressed(2); remote.UndoButtonWasPressed(); //remote.UndoButtonWasPressed(); //remote.UndoButtonWasPressed(); Console.ReadKey(); }
private void Command_Click(object sender, RoutedEventArgs e) { var remoteControl = new RemoteControl(); var light = new Light(); var garageDoor = new GarageDoor(); var stereo = new Stereo(); var lightOn = new LightOnCommand(light); var lightOff = new LightOffCommand(light); var garageOpen = new GarageDoorOpenCommand(garageDoor); var garageClose = new GarageDoorCloseCommand(garageDoor); var stereoOn = new StereoOnWithCdCommand(stereo); var stereoOff = new StereoOffWithCdCommand(stereo); remoteControl.SetCommand(0, lightOn, lightOff); remoteControl.SetCommand(1, garageOpen, garageClose); remoteControl.SetCommand(2, stereoOn, stereoOff); remoteControl.SetCommand(6, new MacroCommand(new ICommand[] { lightOn, garageOpen, stereoOn }), new MacroCommand(new ICommand[] { lightOff, garageClose, stereoOff })); Console.WriteLine(remoteControl.ToString()); }
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) { Console.WriteLine("SIMPLE REMOTE CONTROL"); var simpleRemoteControl = new SimpleRemoteControl(); var light = new Light("Living room"); var lightOn = new LightOnCommand(light); simpleRemoteControl.SetCommand(lightOn); simpleRemoteControl.ButtonWasPressed(); var garageDoorSimpleControl = new GarageDoor(); var garageOpen = new GarageDoorOpenCommand(garageDoorSimpleControl); simpleRemoteControl.SetCommand(garageOpen); simpleRemoteControl.ButtonWasPressed(); Console.WriteLine("REMOTE CONTROL"); var remoteControl = new RemoteControl(); var livingRoomLight = new Light("Living room"); var kitchenLight = new Light("Kitchen"); var ceilingFan = new CeilingFan("Living room"); var garageDoor = new GarageDoor(); var stereo = new Stereo("Living room"); var livingRoomLightOn = new LightOnCommand(livingRoomLight); var livingRoomLightOff = new LightOffCommand(livingRoomLight); var kitchenLightOn = new LightOnCommand(kitchenLight); var kitchenLightOff = new LightOffCommand(kitchenLight); var ceilingFanOn = new CeilingFanOnCommand(ceilingFan); var ceilingFanOff = new CeilingFanOffCommand(ceilingFan); var garageDoorOpen = new GarageDoorOpenCommand(garageDoor); var garageDoorClose = new GarageDoorCloseCommand(garageDoor); var stereoOnWithCd = new StereoOnWithCdCommand(stereo); var stereoOffWithCd = new StereoOffWithCdCommand(stereo); remoteControl.SetCommand(0, livingRoomLightOn, livingRoomLightOff); remoteControl.SetCommand(1, kitchenLightOn, kitchenLightOff); remoteControl.SetCommand(2, ceilingFanOn, ceilingFanOff); remoteControl.SetCommand(3, stereoOnWithCd, stereoOffWithCd); Console.WriteLine(remoteControl.ToString()); Console.WriteLine("-----------------FUNÇÕES EXECUTANDO"); 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(""); Console.WriteLine("REMOTE CONTROL WITH UNDO"); remoteControl.OnButtonWasPushed(0); remoteControl.OffButtonWasPushed(0); remoteControl.UndoButtonWasPushed(); remoteControl.OnButtonWasPushed(1); remoteControl.OffButtonWasPushed(1); remoteControl.UndoButtonWasPushed(); remoteControl.OnButtonWasPushed(2); remoteControl.OffButtonWasPushed(2); remoteControl.UndoButtonWasPushed(); remoteControl.OnButtonWasPushed(3); remoteControl.OffButtonWasPushed(3); remoteControl.UndoButtonWasPushed(); Console.WriteLine(""); Console.WriteLine("REMOTE CONTROL WITH UNDO OF THE CEILING FAN"); var remoteControlCeilingFan = new RemoteControl(); var ceilingFanHigh = new CeilingFanHightCommand(ceilingFan); var ceilingFanMedium = new CeilingFanMediumCommand(ceilingFan); remoteControlCeilingFan.SetCommand(0, ceilingFanMedium, ceilingFanOff); remoteControlCeilingFan.SetCommand(1, ceilingFanHigh, ceilingFanOff); remoteControlCeilingFan.OnButtonWasPushed(0); remoteControlCeilingFan.OffButtonWasPushed(0); Console.WriteLine(remoteControlCeilingFan.ToString()); remoteControlCeilingFan.UndoButtonWasPushed(); //remoteControlCeilingFan.OnButtonWasPushed(0); remoteControlCeilingFan.OnButtonWasPushed(1); Console.WriteLine(remoteControlCeilingFan.ToString()); remoteControlCeilingFan.UndoButtonWasPushed(); Console.WriteLine(""); Console.WriteLine("MACRO COMAND (PARTY MODE)"); var tv = new Tv("Living room"); var hottub = new Hottub(); var tvOn = new TvOnCommand(tv); var tvOff = new TvOffCommand(tv); var hottubOn = new HottubOnCommand(hottub); var hottubOff = new HottubOffCommand(hottub); var commandsPartyOn = new List <ICommand>() { livingRoomLightOn, stereoOnWithCd, tvOn, hottubOn }; var commandsPartyOff = new List <ICommand>() { livingRoomLightOff, stereoOffWithCd, tvOff, hottubOff }; var partyOn = new MacroCommand(commandsPartyOn); var partyOff = new MacroCommand(commandsPartyOff); var remoteControlParty = new RemoteControl(); remoteControlParty.SetCommand(0, partyOn, partyOff); Console.WriteLine(remoteControlParty.ToString()); Console.WriteLine("PUSHING MACRO ON"); remoteControlParty.OnButtonWasPushed(0); Console.WriteLine("PUSHING MACRO OFF"); remoteControlParty.OffButtonWasPushed(0); Console.ReadKey(); }
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(); }
static void Main(string[] args) { #region Strategy Pattern //Duck mallardDuck = new MallardDuck(); //mallardDuck.PerformQuack(); //mallardDuck.PerformFly(); //Duck modelDuck = new ModeldDuck(); //modelDuck.PerformFly(); //modelDuck.PerformQuack(); //modelDuck.setFlyBhavior(new FlyRocketPowered());// to change behavior at runtime just call setter method //modelDuck.PerformFly(); //mallardDuck.Display(); #endregion #region Observer Pattern //WeatherData weatherData = new WeatherData(); //CurrentConditionsDisplay currentConditions = new CurrentConditionsDisplay(weatherData); //weatherData.SetMeaserments(32.46f, 65, 30.4f); #endregion #region Decorator Pattern //Beverage beverage = new Espresso(); //Console.WriteLine(beverage.GetDiscription() + " TK-" + beverage.cost()); //Console.ReadLine(); #endregion #region Factory Pattern //PizzaStore nyPizzaStore = new NYPizzaStore(); //ProductPizza pizza = nyPizzaStore.Orderpizza("Cheese"); #endregion #region Command Pattern RemoteControl remoteControl = new RemoteControl();// invoker, it will be passed a command object that can be used to make request #region #region Create all devices in their Proper Location Light livingRoomLight = new Light("Living Room");// receiver of the request Light kitchenRoomLight = new Light("kitchen Room"); CeilingFan ceilingFan = new CeilingFan("Living Room"); Stereo stereo = new Stereo("Living Room"); GarageDoor garageDoor = new GarageDoor(); #endregion #region Create all the command object LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight); // create a command & pass it to the receiver LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight); // create a command & pass it to the receiver LightOnCommand KichenRoomLightOn = new LightOnCommand(kitchenRoomLight); // create a command & pass it to the receiver LightOffCommand KichenRoomLightOff = new LightOffCommand(kitchenRoomLight); // create a command & pass it to the receiver CeilingFanOnCommand ceilingFanOn = new CeilingFanOnCommand(ceilingFan); CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan); StereoOffCommand stereoOff = new StereoOffCommand(stereo); StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo); GarageDoorOpenCommand garageDoorOpen = new GarageDoorOpenCommand(garageDoor); GarageDoorDownCommand garageDoorDown = new GarageDoorDownCommand(garageDoor); #endregion #region load all commands in the remote slot //remoteControl.SetCommand(0, livingRoomLightOn, livingRoomLightOff); //remoteControl.SetCommand(1, KichenRoomLightOn, KichenRoomLightOff); //remoteControl.SetCommand(2, ceilingFanOn, ceilingFanOff); //remoteControl.SetCommand(3, stereoOnWithCD, stereoOff); #endregion #region action //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); #endregion #endregion #region Undo Action in ceiling //CeilingFanHighCommand ceilingFanHigh = new CeilingFanHighCommand(ceilingFan); //CeilingFanMediumCommand ceilingFanMedium = new CeilingFanMediumCommand(ceilingFan); //CeilingFanLowCommand ceilingFanLow = new CeilingFanLowCommand(ceilingFan); //CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan); //remoteControl.SetCommand(0, ceilingFanMedium, ceilingFanOff); //remoteControl.SetCommand(1, ceilingFanHigh, ceilingFanOff); //remoteControl.OnButtonWasPushed(0); //remoteControl.OffButtonWasPushed(0); //Console.WriteLine(remoteControl); //remoteControl.undoButtonWasPushed(); //remoteControl.OnButtonWasPushed(1); //Console.WriteLine(remoteControl); //remoteControl.undoButtonWasPushed(); //Console.WriteLine(remoteControl); #endregion #region Macro Command ICommand[] partyOn = { livingRoomLightOn, stereoOnWithCD, ceilingFanOn, garageDoorOpen }; ICommand[] partyOff = { livingRoomLightOff, stereoOff, ceilingFanOff, garageDoorDown }; 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); //Console.WriteLine("----Pushing Macro Undo----"); //remoteControl.undoButtonWasPushed(); #endregion #endregion #region Adapter Pattern //MallaDuck duck = new MallaDuck(); //WildTurkey wildTurkey = new WildTurkey(); //IDuck turkeyAdapter = new TurkeyAdapter(wildTurkey); //Console.WriteLine("The Turkey Says..."); //wildTurkey.Gobble(); //wildTurkey.Fly(); //Console.WriteLine("\n The Duck Says..."); //testDuck(duck); //Console.WriteLine("\n The TurkeyAdapter Says..."); //testDuck(turkeyAdapter); #endregion #region Facade Pattern //Amplifier amp = new Amplifier(); //var tuner = new Tuner(); //DvdPlayer dvdPlayer = new DvdPlayer(); //CdPlayer cdPlayer = new CdPlayer(); //Projector projector = new Projector(); //TheaterLights theaterLights = new TheaterLights(); //Screen screen = new Screen(); //PopcornPopper popper = new PopcornPopper(); //TheaterLights light = new TheaterLights(); //HomeTheaterFacade theaterFacade = new HomeTheaterFacade( tuner, amp, dvdPlayer, cdPlayer, projector, light, screen, popper); //theaterFacade.WatchMovies("BatMan"); //Console.WriteLine("-------------------------"); //theaterFacade.EndMovie(); #endregion #region Template Method Pattern Tea tea = new Tea(); tea.PrepareRecipe(); #endregion #region #endregion #region #endregion #region #endregion #region #endregion Console.ReadLine(); }