Example #1
0
        static void Main(string[] args)
        {
            var remoteControl = new Controllers.RemoteControl();

            var livingRoonLight = new Light("Living Room");
            var television      = new Television("Living Room");
            var stereo          = new Stereo("Living Room");
            var hotTub          = new HotTub();

            var lightOn  = new LightOnCommand(livingRoonLight);
            var lightOff = new LightOffCommand(livingRoonLight);

            var televisionOn  = new TelevisionOnCommand(television);
            var televisionOff = new TelevisionOffCommand(television);

            var stereoOn  = new StereoOnCommand(stereo);
            var stereoOff = new StereoOffCommand(stereo);

            var hotTubOn  = new HotTubOnCommand(hotTub);
            var hotTubOff = new HotTubOffCommand(hotTub);

            var partyOn  = new ICommand[] { lightOn, stereoOn, televisionOn, hotTubOn };
            var partyOff = new ICommand[] { lightOff, stereoOff, televisionOff, hotTubOff };

            var partyMacroOn  = new MacroCommand(partyOn);
            var partyMacroOff = new MacroCommand(partyOff);

            remoteControl.SetCommand(0, partyMacroOn, partyMacroOff);

            Console.WriteLine(remoteControl);
            Console.WriteLine("----------Push Macro On----------");
            remoteControl.OnButtonWasPressed(0);
            Console.WriteLine("----------Push Macro Off---------");
            remoteControl.OffButtonWasPressed(0);

            Console.ReadLine();
        }
Example #2
0
/*        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)                                     // Client
        {
            // Example 1
            SimpleRemoteControl remote         = new SimpleRemoteControl(); // Invoker
            LightBulb           light          = new LightBulb();           // Receiver
            LightOnCommand      lightOnCommand = new LightOnCommand(light); // Command

            remote.SetCommand(lightOnCommand);
            remote.ButtonWasPressed();
            remote.UndoButtonWasPressed();

            // Example 2
            AdvancedRemote remote2 = new AdvancedRemote();

            GarageDoor garageDoor = new GarageDoor();
            Stereo     stereo     = new Stereo();

            GarageDoorUpCommand   garageDoorUpCommand   = new GarageDoorUpCommand(garageDoor);
            GarageDoorDownCommand garageDoorDownCommand = new GarageDoorDownCommand(garageDoor);

            StereoOnCommand  stereoOnCommand  = new StereoOnCommand(stereo);
            StereoOffCommand stereoOffCommand = new StereoOffCommand(stereo);

            remote2.SetCommand(0, garageDoorUpCommand, garageDoorDownCommand);
            remote2.SetCommand(1, stereoOnCommand, stereoOffCommand);

            remote2.OnButtonWasPressed(0);
            remote2.OnButtonWasPressed(1);
            remote2.OffButtonWasPressed(0);
            remote2.OffButtonWasPressed(1);

            remote2.UndoButtonWasPressed();

            // Example 3

            AdvancedRemote remote3 = new AdvancedRemote();

            Fan fan = new Fan();

            FanHighCommand   fanHighCommand   = new FanHighCommand(fan);
            FanMeduimCommand fanMeduimCommand = new FanMeduimCommand(fan);
            FanLowCommand    fanLowCommand    = new FanLowCommand(fan);
            FanOffCommand    fanOffCommand    = new FanOffCommand(fan);

            // Remote from Example 2 Reused
            remote3.SetCommand(0, fanHighCommand, fanOffCommand);
            remote3.SetCommand(1, fanMeduimCommand, fanOffCommand);

            remote3.OnButtonWasPressed(0);              // High
            remote3.OffButtonWasPressed(0);             // Off
            remote3.UndoButtonWasPressed();             // Back To High

            // Example 4
            RoomTV          roomTV          = new RoomTV();
            HotTub          hotTub          = new HotTub();
            HotTubOnCommand hotTubOnCommand = new HotTubOnCommand(hotTub);

            TVOnCommand tvOnCommand = new TVOnCommand(roomTV);

            Command[] PartyCommands = new Command[2];
            PartyCommands[0] = hotTubOnCommand;
            PartyCommands[1] = tvOnCommand;


            MacroCommand macroCommand = new MacroCommand(PartyCommands);    // Put All Commands in this MacroCommand

            AdvancedRemote remote4 = new AdvancedRemote();

            remote4.SetCommand(0, macroCommand, new NoCommand());           // No Command for Off Button
            remote4.OnButtonWasPressed(0);
        }