コード例 #1
0
        static void Main(string[] args)
        {
            RemoteControl remote = new RemoteControl();

            Light    light           = new Light();
            ICommand lightOnCommand  = new LightOnCommand(light);
            ICommand lightOffCommand = new LightOffCommand(light);

            AC       ac           = new AC();
            ICommand acOnCommand  = new ACOnCommand(ac);
            ICommand acOffCommand = new ACOffCommand(ac);

            remote.SetOnCommand(lightOnCommand, 0);
            remote.SetOffCommand(lightOffCommand, 0);

            remote.SetOnCommand(acOnCommand, 1);
            remote.SetOffCommand(acOffCommand, 1);

            remote.ExecuteOnCommand(0);
            remote.ExecuteOnCommand(0);

            remote.ExecuteOnCommand(1);
            remote.ExecuteOnCommand(1);
            remote.ExecuteOnCommand(1);
            remote.ExecuteOffCommand(1);
            remote.ExecuteOffCommand(1);
            remote.ExecuteOnCommand(1);
            remote.ExecuteOnCommand(1);
            remote.ExecuteOnCommand(1);
            remote.ExecuteOnCommand(1);
            remote.ExecuteOnCommand(1);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: vglinca/Lessons
        static void Main(string[] args)
        {
            var light       = new Light();
            var lightSwitch = new Switch();

            var onCommand  = new LightOnCommand(light);
            var offCommand = new LightOffCommand(light);

            //lightSwitch.ExecuteCommand(onCommand);
            //lightSwitch.ExecuteCommand(offCommand);

            var toggleCommand = new ToggleLightCommand(light);
            //lightSwitch.ExecuteCommand(toggleCommand);
            //lightSwitch.ExecuteCommand(toggleCommand);

            var room1Light   = new Light();
            var room2Light   = new Light();
            var kitchenLight = new Light();
            var bedroomLight = new Light();

            lightSwitch.ExecuteCommand(new ToggleLightCommand(room1Light));
            lightSwitch.ExecuteCommand(new ToggleLightCommand(room2Light));
            var lights = new List <Light>
            {
                room1Light, room2Light, kitchenLight, bedroomLight
            };
            var toggleallLightsCommand = new ToggleAllLightsCommand(lights);

            lightSwitch.ExecuteCommand(toggleallLightsCommand);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: TheJubadze/Patterns
        static void Main()
        {
            var remote = new RemoteControl();

            var light  = new Light();
            var stereo = new Stereo();

            var lightOnCommand    = new LightOnCommand(light);
            var lightOffCommand   = new LightOffCommand(light);
            var stereoOnCommand   = new StereoOnCommand(stereo);
            var stereoOffCommand  = new StereoOffCommand(stereo);
            var stereoOnCdCommand = new StereoOnCdCommand(stereo);

            remote.OnCommands[0]  = lightOnCommand;
            remote.OnCommands[1]  = stereoOnCommand;
            remote.OnCommands[2]  = stereoOnCdCommand;
            remote.OffCommands[0] = lightOffCommand;
            remote.OffCommands[1] = stereoOffCommand;

            remote.OnButtonWasPushed(0);
            remote.OnButtonWasPushed(1);
            remote.OnButtonWasPushed(2);
            Console.WriteLine(remote);
            remote.OffButtonWasPushed(0);
            remote.OffButtonWasPushed(1);
            remote.UndoButtonPushed();
        }
コード例 #4
0
        public void LightOnCommandTest()
        {
            ILight         light   = MockRepository.GenerateMock <ILight>();
            LightOnCommand command = new LightOnCommand(light);

            command.Execute();
            light.AssertWasCalled(x => x.On());
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: losscut/DesignPattern
        static void Main(string[] args)
        {
            SimpleRemoteControl remote = new SimpleRemoteControl();
            Light          light       = new Light();
            LightOnCommand lightOn     = new LightOnCommand(light);

            remote.SetCommand(lightOn);
            remote.ButtonWasPressd();
        }
コード例 #6
0
        public void OneButtonRemoteLightOnTest()
        {
            OneButtonRemote remote  = new OneButtonRemote();
            ILight          light   = MockRepository.GenerateMock <ILight>();
            LightOnCommand  command = new LightOnCommand(light);

            remote.SetCommand(command);
            remote.PushButton();
            light.AssertWasCalled(x => x.On());
        }
コード例 #7
0
        static void Main(string[] args)
        {
            SimpleRemoteControl remote = new SimpleRemoteControl();
            Light          light       = new Light();
            LightOnCommand lightOnCmd  = new LightOnCommand(light);

            remote.setCommand(lightOnCmd);
            remote.buttonWasPressed();
            Console.ReadLine();
        }
コード例 #8
0
ファイル: Program.cs プロジェクト: evanslaton/design-patterns
        static void Main(string[] args)
        {
            RemoteControl remote          = new RemoteControl();
            Light         livingRoomLight = new Light();
            Stereo        stereo          = new Stereo();
            CeilingFan    ceilingFan      = new CeilingFan("Living Room");

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

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

            CeilingFanHighCommand   ceilingFanHigh   = new CeilingFanHighCommand(ceilingFan);
            CeilingFanMediumCommand ceilingFanMedium = new CeilingFanMediumCommand(ceilingFan);
            CeilingFanLowCommand    ceilingFanLow    = new CeilingFanLowCommand(ceilingFan);
            CeilingFanOffCommand    ceilingFanOff    = new CeilingFanOffCommand(ceilingFan);

            Console.WriteLine(remote.ToString());

            remote.SetCommand(0, livingRoomLightOn, livingRoomLightOff);
            remote.SetCommand(1, stereoOn, stereoOff);
            remote.SetCommand(2, ceilingFanHigh, ceilingFanOff);
            remote.SetCommand(3, ceilingFanMedium, ceilingFanOff);
            remote.SetCommand(4, ceilingFanLow, ceilingFanOff);

            Console.WriteLine(remote.ToString());

            //remote.OnButtonWasPushed(0);
            //remote.OnButtonWasPushed(1);
            //remote.OffButtonWasPushed(0);
            //remote.OffButtonWasPushed(1);
            //remote.UndoButtonWasPushed();

            //remote.OnButtonWasPushed(2);
            //remote.OnButtonWasPushed(3);
            //remote.UndoButtonWasPushed();
            //remote.OffButtonWasPushed(2);
            //remote.UndoButtonWasPushed();

            Command[]    onCommands     = { livingRoomLightOn, stereoOn, ceilingFanHigh };
            MacroCommand macroCommandOn = new MacroCommand(onCommands);

            Command[]    offCommands     = { livingRoomLightOff, stereoOff, ceilingFanOff };
            MacroCommand macroCommandOff = new MacroCommand(offCommands);

            remote.SetCommand(5, macroCommandOn, macroCommandOff);

            remote.OnButtonWasPushed(5);
            Console.WriteLine();
            //remote.OffButtonWasPushed(5);
            remote.UndoButtonWasPushed();
        }
コード例 #9
0
        static void Main(string[] args)
        {
            Light           light           = new Light();
            LightOnCommand  lightOnCommand  = new LightOnCommand(light);
            LightOffCommand lightOffCommand = new LightOffCommand(light);

            SimpleRemoteControl simpleRemoteControl = new SimpleRemoteControl();

            simpleRemoteControl.SetCommand(lightOnCommand);
            simpleRemoteControl.ButtonPress();
            simpleRemoteControl.ButtonUp();
        }
コード例 #10
0
        public static void Run()
        {
            SimpleRemoteControl remote       = new SimpleRemoteControl();
            Light                 light      = new Light();
            LightOnCommand        lightOn    = new LightOnCommand(light);
            GarageDoor            door       = new GarageDoor();
            GarageDoorOpenCommand garageOpen = new GarageDoorOpenCommand(door);

            remote.SetCommand(lightOn);
            remote.ButtonWasPressed();
            remote.SetCommand(garageOpen);
            remote.ButtonWasPressed();
        }
コード例 #11
0
ファイル: Program.cs プロジェクト: hrai/design-patterns
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var remote = new SimpleRemoteControl();

            var kitchenLight = new Light("kitchen light");
            var livingLight  = new Light("living room light");
            var garageDoor   = new GarageDoor();
            var stereo       = new Stereo();

            var kitLightOnCommand  = new LightOnCommand(kitchenLight);
            var kitLightOffCommand = new LightOffCommand(kitchenLight);

            var lightOnCommand  = new LightOnCommand(livingLight);
            var lightOffCommand = new LightOffCommand(livingLight);

            var garageDoorOpenCommand  = new GarageDoorOpenCommand(garageDoor);
            var garageDoorCloseCommand = new GarageDoorCloseCommand(garageDoor);

            var stereoOnWithCDCommand = new StereoOnWithCDCommand(stereo);
            var stereoOffCommand      = new StereoOffCommand(stereo);

            var ceilingFan            = new CeilingFan("Ceiling fan");
            var ceilingFanHighCommand = new CeilingFanHighCommand(ceilingFan);
            var ceilingFanLowCommand  = new CeilingFanLowCommand(ceilingFan);
            var ceilingFanOffCommand  = new CeilingFanOffCommand(ceilingFan);

            remote.SetCommand(0, lightOnCommand, lightOffCommand);
            remote.SetCommand(1, kitLightOnCommand, kitLightOffCommand);
            remote.SetCommand(2, garageDoorOpenCommand, garageDoorCloseCommand);
            remote.SetCommand(3, stereoOnWithCDCommand, stereoOffCommand);
            remote.SetCommand(4, ceilingFanHighCommand, ceilingFanOffCommand);
            remote.SetCommand(5, ceilingFanLowCommand, ceilingFanOffCommand);

            remote.OnButtonPressed(0);
            remote.OffButtonPressed(0);
            remote.OnButtonPressed(1);
            remote.OffButtonPressed(1);
            remote.OnButtonPressed(2);
            remote.OffButtonPressed(2);
            remote.OnButtonPressed(3);
            remote.OffButtonPressed(3);
            remote.OnButtonPressed(4);
            remote.OffButtonPressed(4);
            remote.UndoButtonPressed();
            remote.OnButtonPressed(5);
            remote.OffButtonPressed(5);
            remote.UndoButtonPressed();
        }
コード例 #12
0
        static void Main(string[] args)
        {
            Light   ronnysRoomLight     = new Light();
            Command turnOnLightCommand  = new LightOnCommand(ronnysRoomLight);
            Command turnOffLightCommand = new LightOffCommand(ronnysRoomLight);

            SimpleRemoteControl remoteControl = new SimpleRemoteControl();

            remoteControl.SetCommand(turnOnLightCommand);
            remoteControl.PressButton();

            remoteControl.SetCommand(turnOffLightCommand);
            remoteControl.PressButton();
        }
コード例 #13
0
        static void Main(string[] args)
        {
            Light    light  = new Light();
            ICommand on     = new LightOnCommand(light);
            ICommand off    = new LightOffCommand(light);
            ICommand up     = new LightUpCommand(light);
            ICommand down   = new LightDownCommand(light);
            Remote   remote = new Remote(on, off, up, down);

            remote.PressOn();
            remote.PressOff();
            remote.PressUp();
            remote.PressDown();
            Console.ReadKey();
        }
コード例 #14
0
        static void Main(string[] args)
        {
            RemoteControl remote = new RemoteControl();

            Light light = new Light();

            ICommand lightsOn  = new LightOnCommand(light);
            ICommand lightsOff = new LightOffCommand(light);

            remote.SetCommand(lightsOn);
            remote.pressButton();

            remote.SetCommand(lightsOff);
            remote.pressButton();
        }
コード例 #15
0
        static void Main(string[] args)
        {
            Console.WriteLine("  提示:空格键进下一步\n命令模式过程输出:");
            Console.ReadKey();

            CommandController commandController = new CommandController(); //创建命令调用者-遥控器

            Light light = new Light();                                     //创建命令接收者-灯

            LightOnCommand lightoncommand = new LightOnCommand(light);     //创建(继承后的)命令

            commandController.setCommand(lightoncommand);                  //向遥控器传入开灯命令
            commandController.buttonisPressed();                           //向遥控器传入按下按钮的命令

            Console.ReadKey();
        }
コード例 #16
0
        static void Main(string[] args)
        {
            SimpleRemoteControl remote = new SimpleRemoteControl();

            Light      livingRoomLight  = new Light("Living Romm");
            Light      kitchenRoomLight = new Light("kitchen Romm");
            GarageDoor garageDoor       = new GarageDoor("");
            CeilingFan ceilingFan       = new CeilingFan("Living Room");
            Stereo     stereo           = new Stereo("Living Room");

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

            LightOnCommand  kitchenRoomLightOn  = new LightOnCommand(kitchenRoomLight);
            LightOffCommand kitchenRoomLightOff = new LightOffCommand(kitchenRoomLight);

            CeilingFanHighCommand ceilingFanOn  = new CeilingFanHighCommand(ceilingFan);
            CeilingFanOffCommand  ceilingFanOff = new CeilingFanOffCommand(ceilingFan);

            GarageDoorOpenCommand  garageDoorOpenCommand  = new GarageDoorOpenCommand(garageDoor);
            GarageDoorCloseCommand garageDoorCloseCommand = new GarageDoorCloseCommand(garageDoor);

            StereoOnWithCDCommand stereoOnWithCdCommand = new StereoOnWithCDCommand(stereo);
            StereoOffCommand      stereoOffCommand      = new StereoOffCommand(stereo);

            remote.SetCommand(0, livingRoomLightOn, livingRoomLightOff);
            remote.SetCommand(1, kitchenRoomLightOn, kitchenRoomLightOff);
            remote.SetCommand(2, ceilingFanOn, ceilingFanOff);
            remote.SetCommand(3, stereoOnWithCdCommand, stereoOffCommand);

            Console.WriteLine(remote.ToString());

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

            remote.OnButtonWasPressed(1);
            remote.OffButtonWasPressed(1);

            remote.OnButtonWasPressed(2);
            remote.OffButtonWasPressed(2);

            remote.OnButtonWasPressed(3);
            remote.OffButtonWasPressed(3);


            Console.ReadLine();
        }
コード例 #17
0
        public static void Load()
        {
            RemoteControlWithUndo remoteControl = new RemoteControlWithUndo();

            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");

            LightOnCommand  livingRoomLightOn  = new LightOnCommand(livingRoomLight);
            LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight);
            LightOnCommand  kitchenLightOn     = new LightOnCommand(kitchenLight);
            LightOffCommand kitchenLightOff    = new LightOffCommand(kitchenLight);

            CeilingFanOnCommand  ceilingFanOn  = new CeilingFanOnCommand(ceilingFan);
            CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan);

            GarageDoorOpenCommand  garageDoorOpen  = new GarageDoorOpenCommand(garageDoor);
            GarageDoorCloseCommand garageDoorClose = new GarageDoorCloseCommand(garageDoor);

            StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo);
            StereoOffCommand      stereoOff      = new StereoOffCommand(stereo);

            remoteControl.SetCommand(0, livingRoomLightOn, livingRoomLightOff);
            remoteControl.SetCommand(1, kitchenLightOn, kitchenLightOff);
            remoteControl.SetCommand(2, ceilingFanOn, ceilingFanOff);
            remoteControl.SetCommand(3, stereoOnWithCD, stereoOff);

            remoteControl.OnButtonWasPushed(0);
            remoteControl.OffButtonWasPushed(0);
            Console.WriteLine(remoteControl);
            remoteControl.UndoButtonWasPushed();
            remoteControl.OffButtonWasPushed(0);
            remoteControl.OnButtonWasPushed(0);
            Console.WriteLine(remoteControl);
            remoteControl.UndoButtonWasPushed();

            Console.WriteLine(remoteControl);

            for (int i = 0; i < 4; i++)
            {
                remoteControl.OnButtonWasPushed(i);
                remoteControl.OffButtonWasPushed(i);
            }
        }
コード例 #18
0
ファイル: Program.cs プロジェクト: helintonf/DesignPatterns
        public void Undo()
        {
            RemoteControl remoteControl = new RemoteControl();

            Light light = new Light();

            LightOnCommand  lightOn  = new LightOnCommand(light);
            LightOffCommand lightOff = new LightOffCommand(light);

            remoteControl.SetCommand(0, lightOn, lightOff);

            remoteControl.OnButtonPushed(0);
            remoteControl.OffButtonPushed(0);

            remoteControl.UndoButtonWasPushed();

            Console.Write(remoteControl.ToString());
        }
コード例 #19
0
ファイル: Program.cs プロジェクト: helintonf/DesignPatterns
        public void ExecuteSimple()
        {
            SimpleRemoteControl remoteControl = new SimpleRemoteControl();

            Light light = new Light();

            LightOnCommand lightOn = new LightOnCommand(light);

            remoteControl.Slot = lightOn;

            remoteControl.PressButton();

            GarageDoor garageDoor = new GarageDoor();

            GarageDoorOpenCommand garageDoorOpen = new GarageDoorOpenCommand(garageDoor);

            remoteControl.Slot = garageDoorOpen;

            remoteControl.PressButton();
        }
コード例 #20
0
        void Start()
        {
            var      lightObj = new Command.Receiver.Light();
            ICommand command  = new LightOnCommand(lightObj);

            _simpleRemoteController.SetCommand(0, command);
            _simpleRemoteController.CommandExecute(0);

            var      ceilingFan        = new CeilingFan();
            ICommand ceilingFanCommand = new CeilingFanHighCommand(ceilingFan);

            _simpleRemoteController.SetCommand(1, ceilingFanCommand);
            _simpleRemoteController.CommandExecute(1);
            _simpleRemoteController.Undo();

            var macroCommand = new MacroCommand(new [] { command, ceilingFanCommand });

            _simpleRemoteController.SetCommand(2, macroCommand);
            _simpleRemoteController.CommandExecute(2);
            _simpleRemoteController.Undo();
        }
コード例 #21
0
ファイル: Program.cs プロジェクト: KassabNova/DesignPatterns
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            SimpleRemoteControl remote = new SimpleRemoteControl();
            Light           light      = new Light("Bathroom");
            Light           light2     = new Light("Kitchen");
            LightOnCommand  lightOn    = new LightOnCommand(light);
            LightOnCommand  lightOn2   = new LightOnCommand(light2);
            LightOffCommand lightOff   = new LightOffCommand(light);
            LightOffCommand lightOff2  = new LightOffCommand(light2);

            remote.setCommand(lightOn, lightOff);
            remote.onButtonWasPressed();
            remote.setCommand(lightOn2, lightOff2);
            remote.onButtonWasPressed();
            //turn off
            remote.offButtonWasPressed();
            remote.setCommand(lightOn, lightOff);
            remote.offButtonWasPressed();

            ComplexRemoteControl complexRemote = new ComplexRemoteControl();

            GarageDoor      door      = new GarageDoor();
            DoorUpCommand   openDoor  = new DoorUpCommand(door);
            DoorDownCommand closeDoor = new DoorDownCommand(door);
            DoorStopCommand stopDoor  = new DoorStopCommand(door);

            complexRemote.addCommand("Open", openDoor);
            complexRemote.addCommand("Close", closeDoor);
            complexRemote.addCommand("Stop", stopDoor);

            complexRemote.ButtonPress("Open");
            complexRemote.ButtonPress("Close");
            complexRemote.ButtonPress("Open");
            complexRemote.ButtonPress("Stop");
        }
コード例 #22
0
        static void Main()
        {
            SimpleRemoteControl remote = new SimpleRemoteControl();
            Light light = new Light();
            LightOnCommand lightOn = new LightOnCommand(light);
            LightOffCommand lightOff = new LightOffCommand(light);

            GarageDoor garageDoor = new GarageDoor();
            GarageDoorOpenCommand garageOpen = new GarageDoorOpenCommand(garageDoor);
            GarageDoorCloseCommand garageClose = new GarageDoorCloseCommand(garageDoor);

            remote.SetCommand(0, lightOn, lightOff);
            remote.OnButtonWasPressed(0);
            remote.OffButtonWasPressed(0);
            remote.UndoButtonWasPressed();

            remote.SetCommand(1, garageOpen, garageClose);
            remote.OnButtonWasPressed(1);
            remote.OffButtonWasPressed(1);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
コード例 #23
0
        private static void Main()
        {
            var remoteControl = new RemoteControl();

            var light      = new Light("Living Room");
            var stereo     = new Stereo("Living Room");
            var ceilingFan = new CeilingFan("Living Room");

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

            var stereoOnWithCd = new StereoOnWithCdCommand(stereo);
            var stereoOff      = new StereoOffCommand(stereo);

            var fanOn  = new CeilingFanOnCommand(ceilingFan);
            var fanOff = new CeilingFanOffCommand(ceilingFan);

            var partyOnMacro  = new MacroCommand(new ICommand[] { lightOn, stereoOnWithCd, fanOn });
            var partyOffMacro = new MacroCommand(new ICommand[] { lightOff, stereoOff, fanOff });

            remoteControl.SetCommand(0, lightOn, lightOff);
            remoteControl.SetCommand(1, stereoOnWithCd, stereoOff);
            remoteControl.SetCommand(2, fanOn, fanOff);
            remoteControl.SetCommand(3, partyOnMacro, partyOffMacro);

            Console.WriteLine(remoteControl);

            remoteControl.PressOnButton(0);
            remoteControl.PressOffButton(0);
            remoteControl.PressUndoButton();

            Console.WriteLine("--- Pushing Macro On ---");
            remoteControl.PressOnButton(3);
            Console.WriteLine("--- Pushing Macro Off ---");
            remoteControl.PressOffButton(3);
        }
コード例 #24
0
        public void MultiButtonRemoteWithMultipleLightsTest()
        {
            var kitchenLight              = MockRepository.GenerateMock <ILight>();
            var kitchenLightOnCommand     = new LightOnCommand(kitchenLight);
            var kitchenLightOffCommand    = new LightOffCommand(kitchenLight);
            var livingRoomLight           = MockRepository.GenerateMock <ILight>();
            var livingRoomLightOnCommand  = new LightOnCommand(livingRoomLight);
            var livingRoomLightOffCommand = new LightOffCommand(livingRoomLight);

            MultiButtonRemote remote = new MultiButtonRemote();

            remote.SetSlot(0, kitchenLightOnCommand, kitchenLightOffCommand);
            remote.SetSlot(1, livingRoomLightOnCommand, livingRoomLightOffCommand);

            remote.PushOnButton(0);
            kitchenLight.AssertWasCalled(x => x.On());
            remote.PushOnButton(1);
            livingRoomLight.AssertWasCalled(x => x.On());

            remote.PushOffButton(0);
            kitchenLight.AssertWasCalled(x => x.Off());
            remote.PushOffButton(1);
            kitchenLight.AssertWasCalled(x => x.Off());
        }
コード例 #25
0
 public void OneButtonRemoteLightOnTest()
 {
     OneButtonRemote remote = new OneButtonRemote();
     ILight light = MockRepository.GenerateMock<ILight>();
     LightOnCommand command = new LightOnCommand(light);
     remote.SetCommand(command);
     remote.PushButton();
     light.AssertWasCalled(x => x.On());
 }
コード例 #26
0
        public static void Main()
        {
            RemoteControl remoteControl = new RemoteControl();

            Light      livingRoomLight = new Light("Living room");
            Light      kitchenLight    = new Light("Kitchen Light");
            CeilingFan ceilingFan      = new CeilingFan("Living room");
            GarageDoor garageDoor      = new GarageDoor();
            Stereo     stereo          = new Stereo("Living room");
            TV         tv     = new TV("Living room");
            Hottub     hottub = new Hottub();

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

            LightOnCommand  kitchenLightOn  = new LightOnCommand(kitchenLight);
            LightOffCommand kitchenLightOff = new LightOffCommand(kitchenLight);

            CeilingFanOnCommand  ceilingFanHigh = new CeilingFanOnCommand(ceilingFan);
            CeilingFanOffCommand ceilingFanOff  = new CeilingFanOffCommand(ceilingFan);

            GarageDoorOpenCommand  garageDoorUp   = new GarageDoorOpenCommand(garageDoor);
            GarageDoorCloseCommand garageDoorDown = new GarageDoorCloseCommand(garageDoor);

            StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo);
            StereoOffCommand      stereoOff      = new StereoOffCommand(stereo);

            TVOnCommand  tvOn  = new TVOnCommand(tv);
            TVOffCommand tvOff = new TVOffCommand(tv);

            HottubOnCommand  hottubOn  = new HottubOnCommand(hottub);
            HottubOffCommand hottubOff = new HottubOffCommand(hottub);

            ICommand[] partyOn  = { livingRoomLightOn, stereoOnWithCD, tvOn, hottubOn };
            ICommand[] partyOff = { livingRoomLightOff, stereoOff, tvOff, hottubOff };

            //TODO: deal with the Macrocommand name
            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, stereoOnWithCD, stereoOff);
            remoteControl.SetCommand(4, partyOnMacro, partyOffMacro);

            Console.WriteLine(remoteControl);

            remoteControl.OnButtonWasPressed(0);
            remoteControl.OffButtonWasPressed(0);
            remoteControl.UndoButtonWasPressed();
            remoteControl.OnButtonWasPressed(1);
            remoteControl.OffButtonWasPressed(1);
            remoteControl.OnButtonWasPressed(2);
            remoteControl.OffButtonWasPressed(2);
            remoteControl.UndoButtonWasPressed();
            remoteControl.OnButtonWasPressed(3);
            remoteControl.OffButtonWasPressed(3);
            remoteControl.UndoButtonWasPressed();
            remoteControl.OnButtonWasPressed(4);
            Console.WriteLine("Party is on!");
            remoteControl.OffButtonWasPressed(4);
            Console.WriteLine("Party is over");


            Console.ReadLine();
        }
コード例 #27
0
 public void LightOnCommandTest()
 {
     ILight light = MockRepository.GenerateMock<ILight>();
     LightOnCommand command = new LightOnCommand(light);
     command.Execute();
     light.AssertWasCalled(x => x.On());
 }
コード例 #28
0
        static void Main(string[] args)
        {
            RemoteControl remote     = new RemoteControl();
            CeilingFan    ceilingFan = new CeilingFan("Living Room");
            //Light livingRoomLight = new Light("Living Room");
            //Light kitchenLight = new Light("Kitchen");
            //GarageDoor garageDoor = new GarageDoor();
            //Stereo stereo = new Stereo("Living room");
            //AirCondition airCondition = new AirCondition();
            //
            //LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight);
            //LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight);
            //
            //LightOnCommand kitchenLightOn = new LightOnCommand(kitchenLight);
            //LightOffCommand kitchenLightOff = new LightOffCommand(kitchenLight);
            //
            //CeilingFanOnCommand ceilingFanOn = new CeilingFanOnCommand(ceilingFan);
            //CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan);
            //
            //GarageDoorOpenCommand garageDoorUp = new GarageDoorOpenCommand(garageDoor);
            //GarageDoorCloseCommand garageDoorOff = new GarageDoorCloseCommand(garageDoor);
            //
            //StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo);
            //StereoOffCommand stereoOff = new StereoOffCommand(stereo);
            //
            //AirConditionOnCommand airCondtionOnCommand = new AirConditionOnCommand(airCondition);
            //AirconditionOffCommand airCondtionOffCommand = new AirconditionOffCommand(airCondition);
            //
            //remote.SetCommand(0, livingRoomLightOn, livingRoomLightOff);
            //remote.SetCommand(1, kitchenLightOn, kitchenLightOff);
            //remote.SetCommand(2, ceilingFanOn, ceilingFanOff);
            //remote.SetCommand(3, stereoOnWithCD, stereoOff);
            //remote.SetCommand(4, airCondtionOnCommand, airCondtionOffCommand);
            //
            //Console.WriteLine(remote);
            //
            //remote.OffButtonWasPressed(0);
            //remote.OffButtonWasPressed(1);
            //remote.OffButtonWasPressed(2);
            //remote.OffButtonWasPressed(3);
            //remote.OffButtonWasPressed(4);
            //Console.WriteLine();
            //remote.OnButtonWasPressed(0);
            //remote.OnButtonWasPressed(1);
            //remote.OnButtonWasPressed(2);
            //remote.OnButtonWasPressed(3);
            //remote.OnButtonWasPressed(4);

            // CeilingFanHighCommand ceilingFanHigh = new CeilingFanHighCommand(ceilingFan);
            // CeilingFanMediumCommand ceilingFanMedium = new CeilingFanMediumCommand(ceilingFan);
            // CeilingFanLowCommand ceilingFanLow = new CeilingFanLowCommand(ceilingFan);
            // CeilingFanOffCommand ceilingFanOffCommand = new CeilingFanOffCommand(ceilingFan);
            //
            // remote.SetCommand(0, ceilingFanLow, ceilingFanOffCommand);
            // remote.SetCommand(1, ceilingFanMedium, ceilingFanOffCommand);
            // remote.SetCommand(2, ceilingFanHigh, ceilingFanOffCommand);
            //
            // remote.OnButtonWasPressed(0);
            // remote.OnButtonWasPressed(1);
            // remote.OnButtonWasPressed(2);
            //
            // Console.WriteLine(remote);
            //
            // remote.OffButtonWasPressed(1);
            // remote.UndoButtonWasPressed();


            Light  light  = new Light("Living room");
            TV     tv     = new TV();
            Stereo stereo = new Stereo("Living Room");
            Hottub hottub = new Hottub();

            LightOnCommand        lightOnCommand  = new LightOnCommand(light);
            TvOnCommand           tvOnCommand     = new TvOnCommand(tv);
            StereoOnWithCDCommand stereoOnCommand = new StereoOnWithCDCommand(stereo);
            HottubOnCommand       hottunOnCommand = new HottubOnCommand(hottub);

            LightOffCommand  lightOffCommand  = new LightOffCommand(light);
            TvOffCommand     tvOffCommand     = new TvOffCommand(tv);
            StereoOffCommand stereoffCommand  = new StereoOffCommand(stereo);
            HottubOffCommand hottunOffCommand = new HottubOffCommand(hottub);

            ICommand[] partyOn =
            {
                lightOnCommand,
                tvOnCommand,
                stereoOnCommand,
                hottunOnCommand
            };
            ICommand[] partOff =
            {
                lightOffCommand,
                tvOffCommand,
                stereoffCommand,
                hottunOffCommand
            };

            MacroCommand OnMacro  = new MacroCommand(partyOn);
            MacroCommand OffMacro = new MacroCommand(partOff);

            remote.SetCommand(0, OnMacro, OffMacro);
            Console.WriteLine(remote);
            remote.OnButtonWasPressed(0);
            remote.UndoButtonWasPressed();
            remote.OnButtonWasPressed(0);
        }
コード例 #29
0
        public void MultiButtonRemoteWithMultipleLightsTest()
        {
            var kitchenLight = MockRepository.GenerateMock<ILight>();
            var kitchenLightOnCommand = new LightOnCommand(kitchenLight);
            var kitchenLightOffCommand = new LightOffCommand(kitchenLight);
            var livingRoomLight = MockRepository.GenerateMock<ILight>();
            var livingRoomLightOnCommand = new LightOnCommand(livingRoomLight);
            var livingRoomLightOffCommand = new LightOffCommand(livingRoomLight);

            MultiButtonRemote remote = new MultiButtonRemote();
            remote.SetSlot(0, kitchenLightOnCommand, kitchenLightOffCommand);
            remote.SetSlot(1, livingRoomLightOnCommand, livingRoomLightOffCommand);

            remote.PushOnButton(0);
            kitchenLight.AssertWasCalled(x => x.On());
            remote.PushOnButton(1);
            livingRoomLight.AssertWasCalled(x => x.On());

            remote.PushOffButton(0);
            kitchenLight.AssertWasCalled(x => x.Off());
            remote.PushOffButton(1);
            kitchenLight.AssertWasCalled(x => x.Off());
        }
コード例 #30
0
        static void Main(string[] args)
        {
            Light                 light          = new Light();
            GarageDoor            garageDoor     = new GarageDoor();
            LightOnCommand        lightOn        = new LightOnCommand(light);
            LightOffCommand       lightOff       = new LightOffCommand(light);
            GarageDoorOpenCommand garageDoorOpen = new GarageDoorOpenCommand(garageDoor);
            GarageDoorDownCommand garageDoorDown = new GarageDoorDownCommand(garageDoor);

            //01.简单控制器--------------------------
            Console.WriteLine("\n-----01.简单控制器-----\n");
            SimpleRemoteControl remote = new SimpleRemoteControl();

            remote.SetCommand(lightOn);
            remote.ButtonWasPressed();
            remote.SetCommand(garageDoorOpen);
            remote.ButtonWasPressed();

            //02.复杂控制器--------------------------------------
            Console.WriteLine("\n-----02.复杂控制器-----\n");
            RemoteControl remoteControl = new RemoteControl();

            remoteControl.SetCommand(0, lightOn, lightOff);
            remoteControl.SetCommand(1, garageDoorOpen, garageDoorDown);
            Console.WriteLine(remoteControl.ToString());
            remoteControl.OnButtonWasPushed(0);
            remoteControl.OffButtonWasPushed(0);
            remoteControl.OnButtonWasPushed(1);
            remoteControl.OffButtonWasPushed(1);

            //03.复杂控制器,撤销功能--------------------------------------
            Console.WriteLine("\n-----03.复杂控制器,撤销功能-----\n");
            RemoteControlWithUndo remoteControlWithUndo = new RemoteControlWithUndo();

            remoteControlWithUndo.SetCommand(0, lightOn, lightOff);
            remoteControlWithUndo.OnButtonWasPushed(0);
            remoteControlWithUndo.OffButtonWasPushed(0);
            Console.WriteLine(remoteControlWithUndo.ToString());
            remoteControlWithUndo.UndoButtonWasPushed();
            remoteControlWithUndo.OffButtonWasPushed(0);
            remoteControlWithUndo.OnButtonWasPushed(0);
            Console.WriteLine(remoteControlWithUndo.ToString());
            remoteControlWithUndo.UndoButtonWasPushed();

            //04.复杂撤销功能-天花板吊扇
            Console.WriteLine("\n-----04.复杂撤销功能-天花板吊扇-----\n");
            remoteControlWithUndo = new RemoteControlWithUndo();
            CeilingFan              ceilingFan              = new CeilingFan("Living Room");
            CeilingFanHighCommand   ceilingFanHighCommand   = new CeilingFanHighCommand(ceilingFan);
            CeilingFanMediumCommand ceilingFanMediumCommand = new CeilingFanMediumCommand(ceilingFan);
            CeilingFanOffCommand    ceilingFanOffCommand    = new CeilingFanOffCommand(ceilingFan);

            remoteControlWithUndo.SetCommand(0, ceilingFanHighCommand, ceilingFanOffCommand);   //0号插槽的On键设置为高速,Off键设置为关闭
            remoteControlWithUndo.SetCommand(1, ceilingFanMediumCommand, ceilingFanOffCommand); //1号插槽的On键设置为中速,Off键设置为关闭

            remoteControlWithUndo.OnButtonWasPushed(0);                                         //首先以高速开启吊扇
            remoteControlWithUndo.OffButtonWasPushed(0);                                        //然后关闭
            Console.WriteLine(remoteControlWithUndo.ToString());
            remoteControlWithUndo.UndoButtonWasPushed();                                        //撤销,回到中速

            remoteControlWithUndo.OnButtonWasPushed(1);                                         //开启中速
            remoteControlWithUndo.OffButtonWasPushed(1);                                        //关闭
            Console.WriteLine(remoteControlWithUndo.ToString());                                //撤销,回到中速
            remoteControlWithUndo.UndoButtonWasPushed();

            //05.Party模式
            Console.WriteLine("\n-----05.Party模式-----\n");
            ICommand[]   partyOn              = { lightOn, garageDoorOpen, ceilingFanHighCommand }; //一个数组用来记录开启命令
            ICommand[]   partyOff             = { lightOff, garageDoorDown, ceilingFanOffCommand }; //另一个数组用来记录关闭命令
            MacroCommand partyOnMacroCommand  = new MacroCommand(partyOn);                          //创建对应的宏持有开启命令数组
            MacroCommand partyOffMacroCommand = new MacroCommand(partyOff);                         //创建对应的宏持有关闭命令数组

            remoteControlWithUndo = new RemoteControlWithUndo();
            remoteControlWithUndo.SetCommand(0, partyOnMacroCommand, partyOffMacroCommand);//将宏命令指定一个按钮
            Console.WriteLine(remoteControlWithUndo);
            Console.WriteLine("----Pushing Macro On----");
            remoteControlWithUndo.OnButtonWasPushed(0);
            Console.WriteLine("----Pushing Macro Off----");
            remoteControlWithUndo.OffButtonWasPushed(0);
            Console.WriteLine("----Pushing Macro Undo----");
            remoteControlWithUndo.UndoButtonWasPushed();

            Console.ReadKey();
        }
コード例 #31
0
ファイル: Program.cs プロジェクト: Lena19/Command
        static void Main(string[] args)
        {
            RemoteControl remoteControl = new RemoteControl();

            Light      light      = new Light();
            Hottub     hottub     = new Hottub();
            GarageDoor garagedoor = new GarageDoor();
            CeilingFan ceilingfan = new CeilingFan();
            Stereo     stereo     = new Stereo();

            CeilingFanOnCommand  ceilingFanOn  = new CeilingFanOnCommand(ceilingfan);
            CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingfan);

            GarageDoorUpCommand   garageDoorUp   = new GarageDoorUpCommand(garagedoor);
            GarageDoorDownCommand garageDoorDown = new GarageDoorDownCommand(garagedoor);

            HottubOnCommand  hottubOn  = new HottubOnCommand(hottub);
            HottubOffCommand hottubOff = new HottubOffCommand(hottub);

            LightOnCommand  lightOn  = new LightOnCommand(light);
            LightOffCommand lightOff = new LightOffCommand(light);

            LivingroomLightOnCommand  livingRoomOn  = new LivingroomLightOnCommand(light);
            LivingroomLightOffCommand livingRoomOff = new LivingroomLightOffCommand(light);

            StereoOnWithCDCommand stereoOn  = new StereoOnWithCDCommand(stereo);
            StereoOffCommand      stereoOff = new StereoOffCommand(stereo);

            remoteControl.setCommand(0, ceilingFanOn, ceilingFanOff);
            remoteControl.setCommand(1, garageDoorUp, garageDoorDown);
            remoteControl.setCommand(2, hottubOn, hottubOff);
            remoteControl.setCommand(3, lightOn, lightOff);
            remoteControl.setCommand(4, livingRoomOn, livingRoomOff);
            remoteControl.setCommand(5, stereoOn, stereoOff);

            Console.WriteLine(remoteControl.toString());

            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();

            remoteControl.onButtonWasPushed(4);
            remoteControl.offButtonWasPushed(4);
            remoteControl.undoButtonWasPushed();

            remoteControl.onButtonWasPushed(5);
            remoteControl.offButtonWasPushed(5);
            remoteControl.undoButtonWasPushed();
        }