Пример #1
0
        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();
        }
Пример #2
0
        static void Main(string[] args)
        {
            RemoteControl remoteControl = new RemoteControl();
            #region
            // 
            //Light light = new Light();
            //Light garageDoorLight = new Light();
            //Stereo stereo = new Stereo();
            //// 
            //LightOnCommand lightOnCommand = new LightOnCommand(light);
            //GarageLightOnCommand garageLightOnCommand = new GarageLightOnCommand(garageDoorLight);
            //StereoOnWithCdCommand stereoOnWithCdCommand = new StereoOnWithCdCommand(stereo);

            //LightOffCommand lightOffCommand = new LightOffCommand(light);
            //GarageLightOffCommand garageLightOffCommand = new GarageLightOffCommand(garageDoorLight);
            //StereoOffCommand stereoOffCommand = new StereoOffCommand(stereo);
            ////
            //remoteControl.SetCommand(0, lightOnCommand, lightOffCommand);
            //remoteControl.SetCommand(1, garageLightOnCommand, garageLightOffCommand);
            //remoteControl.SetCommand(2, stereoOnWithCdCommand, stereoOffCommand);
            ////
            //Console.WriteLine(remoteControl);

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

            ////remoteControl.OnButtonWasPushed(1);
            ////remoteControl.OffButtonWasPushed(1);
            ////remoteControl.OnButtonWasPushed(2);
            ////remoteControl.OffButtonWasPushed(2);
            #endregion
            var ceilingFan = new CeilingFan();
            var ceilingFanHight = new CeilingFanHightCommand(ceilingFan);
            var ceilingFanMedium = new CeilingFanMediumCommand(ceilingFan);
            var ceilingFanOff = new CeilingFanOffCommand(ceilingFan);

            remoteControl.SetCommand(0, ceilingFanMedium, ceilingFanOff);
            remoteControl.SetCommand(1, ceilingFanHight, ceilingFanOff);

            remoteControl.OnButtonWasPushed(0);
            remoteControl.OffButtonWasPushed(0);
            Console.WriteLine(remoteControl);
            remoteControl.UndoButtonWasPushed();
            remoteControl.OnButtonWasPushed(1);
            Console.WriteLine(remoteControl);
            remoteControl.UndoButtonWasPushed();
            
            Console.ReadKey(true);

        }
Пример #3
0
        static void Main(string[] args)
        {
            var r     = new RemoteControl();
            var light = new Light();

            r.SetCommand(0, new LightOnCommand(light), new LightOffCommand(light));
            var stereo = new Stereo();

            r.SetCommand(1, new StereoOnCommand(stereo), new StereoOffCommand(stereo));
            r.SetCommand(2, new StereoVolumeUpCommand(stereo), new StereoVolumeDownCommand(stereo));
            r.Display();
            r.OnButtonWasPushed(0);
            r.OffButtonWasPushed(0);
            r.OnButtonWasPushed(1);
            r.OffButtonWasPushed(2);
            r.OnButtonWasPushed(2);
            r.OffButtonWasPushed(1);
            r.OnButtonWasPushed(2);
            r.OffButtonWasPushed(2);
            Console.Read();
        }
Пример #4
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();
        }