Beispiel #1
0
        public static void RunRemoteControl()
        {
            var remote          = new RemoteControl();
            var light           = new Light();
            var lightOn         = new LightOnCommand(light);
            var lightOff        = new LightOffCommand(light);
            var garageDoor      = new GarageDoor();
            var garageDoorOpen  = new GarageDoorOpenCommand(garageDoor);
            var garageDoorClose = new GarageDoorCloseCommand(garageDoor);
            var stereo          = new Stereo();
            var stereoOnWithCD  = new StereoOnWithCDCommand(stereo);
            var stereoOff       = new StereoOffCommand(stereo);

            remote.SetCommand(0, lightOn, lightOff);
            remote.SetCommand(1, garageDoorOpen, garageDoorClose);
            remote.SetCommand(2, stereoOnWithCD, stereoOff);

            System.Console.WriteLine(remote);

            remote.OnButtonWasPressed(0);
            remote.OffButtonWasPressed(0);
            remote.OnButtonWasPressed(1);
            remote.OffButtonWasPressed(1);
            remote.OnButtonWasPressed(2);
            remote.OffButtonWasPressed(2);
        }
        static void Main(string[] args)
        {
            //SimpleRemoteControl remoteControl = new SimpleRemoteControl();
            ////厂商提供的电灯类,命令的接收者
            //Light light = new Light();

            ////我们封装的命令对象,设置接收者
            //LightOnCommand lightOnCommand = new LightOnCommand(light);

            ////设置遥控器开关对应的命令对象
            //remoteControl.setCommand(lightOnCommand);
            //remoteControl.ButtonWasPressed();

            RemoteControl   remoteControl   = new RemoteControl();
            Light           light           = new Light();
            LightOnCommand  lightOnCommand  = new LightOnCommand(light);
            LightOffCommand lightOffCommand = new LightOffCommand(light);

            remoteControl.setCommand(0, lightOnCommand, lightOffCommand);
            remoteControl.OnButtonWasPressed(0);
            remoteControl.OffButtonWasPressed(0);
            Console.WriteLine(remoteControl.ToString());
            remoteControl.UndoButtonWasPressed();

            remoteControl.OffButtonWasPressed(0);
            remoteControl.OnButtonWasPressed(0);

            remoteControl.UndoButtonWasPressed();
            Console.ReadKey();
        }
Beispiel #3
0
        public static void RunRemoteControlWithMacroCommand()
        {
            var light    = new Light();
            var lightOn  = new LightOnCommand(light);
            var lightOff = new LightOffCommand(light);

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

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

            var macroOnCommand  = new MacroCommand(new ICommand[] { lightOn, garageDoorOpen, stereoOnWithCD });
            var macroOffCommand = new MacroCommand(new ICommand[] { lightOff, garageDoorClose, stereoOff });

            var remote = new RemoteControl();

            remote.SetCommand(0, macroOnCommand, macroOffCommand);
            System.Console.WriteLine(remote);

            System.Console.WriteLine("--- Pushing Macro on ---");
            remote.OnButtonWasPressed(0);
            System.Console.WriteLine("--- Pushing Macro off ---");
            remote.OffButtonWasPressed(0);
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            RemoteControl RemoteControl = new RemoteControl();

            Light          light   = new Light("Living Room");
            LightOnCommand lightOn = new LightOnCommand(light);
            LightOfCommand lightOf = new LightOfCommand(light);

            CeilingFan ceiling = new CeilingFan("Living Room");

            CeilingFanLowcommand  fanLowcommand = new CeilingFanLowcommand(ceiling);
            CeilingFanHighCommand chigh         = new CeilingFanHighCommand(ceiling);

            CeilingFanOfcommand fanOfcommand = new CeilingFanOfcommand(ceiling);

            GarrageDoor            gd     = new GarrageDoor("Garrage");
            GarageDoorOpenCommand  gopen  = new GarageDoorOpenCommand(gd);
            GarageDoorCloseCommand gclose = new GarageDoorCloseCommand(gd);



            ICommand[] partyOn  = { lightOn, gopen, chigh };
            ICommand[] partyOff = { lightOf, gclose, fanLowcommand };

            MacroCommand partyOnMacro  = new MacroCommand(partyOn);
            MacroCommand partyOffMacro = new MacroCommand(partyOff);

            RemoteControl.SetCommand(0, partyOnMacro, partyOffMacro);

            Console.WriteLine(RemoteControl);
            Console.WriteLine("-----Pushing Macro On----");
            RemoteControl.OnButtonWasPressed(0);
            Console.WriteLine("--------- Pushing Macro Of------");
            RemoteControl.OffButtonWasPressed(0);
        }