Exemple #1
0
        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();
            }
        }
Exemple #2
0
        private static string ProcessRemoteControlWithUndo()
        {
            StringBuilder sb = new StringBuilder();

            RemoteControlWithUndo remoteControlWithUndo = new RemoteControlWithUndo();

            Light livingRoomLight = new Light("Living Room");

            LightOnCommand  livingRoomLightOn  = new LightOnCommand(livingRoomLight);
            LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight);

            remoteControlWithUndo.OnCommands[0]  = livingRoomLightOn;
            remoteControlWithUndo.OffCommands[0] = livingRoomLightOff;

            sb.AppendLine(remoteControlWithUndo.OnButtonWasPushed(0));
            sb.AppendLine(remoteControlWithUndo.OffButtonWasPushed(0));
            sb.AppendLine(remoteControlWithUndo.ToString());
            sb.AppendLine(remoteControlWithUndo.UndoButtonWasPushed());
            sb.AppendLine(remoteControlWithUndo.OffButtonWasPushed(0));
            sb.AppendLine(remoteControlWithUndo.OnButtonWasPushed(0));
            sb.AppendLine(remoteControlWithUndo.ToString());
            sb.AppendLine(remoteControlWithUndo.UndoButtonWasPushed());

            return(sb.ToString());
        }
        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);
        }
        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();
        }
Exemple #5
0
        static void Main(string[] args)
        {
            var remoteControl = new RemoteControlWithUndo();

            var livingRoonLight = new Light("Living Room");

            var livingRoomLightOn  = new LightOnCommand(livingRoonLight);
            var livingRoomLightOff = new LightOffCommand(livingRoonLight);

            remoteControl.SetCommand(0, livingRoomLightOn, livingRoomLightOff);

            remoteControl.OnButtonWasPressed(0);
            remoteControl.OffButtonWasPressed(0);
            Console.WriteLine(remoteControl);
            remoteControl.UndoButtonWasPressed();
            remoteControl.OffButtonWasPressed(0);
            remoteControl.OnButtonWasPressed(0);
            Console.WriteLine(remoteControl);
            remoteControl.UndoButtonWasPressed();

            Console.ReadLine();
        }
Exemple #6
0
        public static void RunRemoteControlWithUndo()
        {
            var remote         = new RemoteControlWithUndo();
            var light          = new Light();
            var lightOn        = new LightOnCommand(light);
            var lightOff       = new LightOffCommand(light);
            var stereo         = new Stereo();
            var stereoOnWithCD = new StereoOnWithCDCommand(stereo);
            var stereoOff      = new StereoOffCommand(stereo);

            remote.SetCommand(0, lightOn, lightOff);
            remote.SetCommand(1, stereoOnWithCD, stereoOff);
            System.Console.WriteLine(remote);

            remote.OnButtonWasPressed(0);
            remote.OffButtonWasPressed(0);
            remote.UndoButtonWasPressed();

            remote.OnButtonWasPressed(1);
            remote.UndoButtonWasPressed();
            remote.OffButtonWasPressed(1);
        }
        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");

            // Set Commands
            remoteControl.SetCommand(0, () => { livingRoomLight.On(); }, () => { livingRoomLight.Off(); });
            remoteControl.SetCommand(1, () => { kitchenLight.On(); }, () => { kitchenLight.Off(); });
            remoteControl.SetCommand(2, () => { ceilingFan.Low(); }, () => { ceilingFan.Off(); });
            remoteControl.SetCommand(3, () => { ceilingFan.Medium(); }, () => { ceilingFan.Off(); });
            remoteControl.SetCommand(4, () => { ceilingFan.High(); }, () => { ceilingFan.Off(); });
            remoteControl.SetCommand(5, () => { garageDoor.Up(); }, () => { garageDoor.Down(); });

            Action stereoOnWithCD = () => { stereo.On(); stereo.SetCd(); stereo.SetVolume(11); };

            remoteControl.SetCommand(6, stereoOnWithCD, () => { stereo.Off(); });

            // 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);
            }
        }
Exemple #8
0
        private static string ProcessRemoteControlWithUndoComplex()
        {
            StringBuilder sb = new StringBuilder();

            RemoteControlWithUndo remoteControlUndo = new RemoteControlWithUndo();
            CeilingFan            ceilingFan1       = new CeilingFan("Living Room");

            remoteControlUndo.OnCommands[0]  = new CeilingFanMediumCommand(ceilingFan1);
            remoteControlUndo.OffCommands[0] = new CeilingFanOffCommand(ceilingFan1);

            remoteControlUndo.OnCommands[1]  = new CeilingFanHighCommand(ceilingFan1);
            remoteControlUndo.OffCommands[1] = new CeilingFanOffCommand(ceilingFan1);

            sb.AppendLine(remoteControlUndo.OnButtonWasPushed(0));
            sb.AppendLine(remoteControlUndo.OffButtonWasPushed(0));
            sb.AppendLine(remoteControlUndo.ToString());
            sb.AppendLine(remoteControlUndo.UndoButtonWasPushed());

            sb.AppendLine(remoteControlUndo.OnButtonWasPushed(1));
            sb.AppendLine(remoteControlUndo.ToString());
            sb.AppendLine(remoteControlUndo.UndoButtonWasPushed());

            return(sb.ToString());
        }
Exemple #9
0
        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();
        }
        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();
            }
        }