Exemplo n.º 1
0
        public static void MacroCommandTests()
        {
            var light  = new Light("Living Room");
            var tv     = new TV("Living Room");
            var stereo = new Stereo("Living Room");
            var hottub = new Hottub();

            var lightOn  = new LightOnCommand(light);
            var stereoOn = new StereoOnWithCdCommand(stereo);
            var tvOn     = new TvOnCommand(tv);
            var hottubOn = new HottubOnCommand(hottub);

            var lightOff  = new LightOffCommand(light);
            var stereoOff = new StereoOffCommand(stereo);
            var tvOff     = new TvOffCommand(tv);
            var hottubOff = new HottubOffCommand(hottub);

            IUndoableCommand[] partyOn       = { lightOn, stereoOn, tvOn, hottubOn };
            IUndoableCommand[] partyOff      = { lightOff, stereoOff, tvOff, hottubOff };
            MacroCommand       partyOnMacro  = new MacroCommand(partyOn);
            MacroCommand       partyOffMacro = new MacroCommand(partyOff);

            var remoteControl = new RemoteControl();

            remoteControl.SetCommand(1, partyOnMacro, partyOffMacro);

            Console.WriteLine(remoteControl);
            Console.WriteLine("---Pushing Macro On-- -");
            remoteControl.OnButtonWasPushed(1);
            Console.WriteLine("---Pushing Macro Off-- -");
            remoteControl.OffButtonWasPushed(1);

            Console.ReadKey();
        }
Exemplo n.º 2
0
        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();
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        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();
        }