Beispiel #1
0
        static void Main(string[] args)
        {
            Title.UpperIndent(Assembly.GetExecutingAssembly());

            Controller controler = new Controller();

            LightInRoomManager    lightInRoomManager    = new LightInRoomManager();
            LightInRoomOnCommand  lightInRoomOnCommand  = new LightInRoomOnCommand(lightInRoomManager);
            LightInRoomOffCommand lightInRoomOffCommand = new LightInRoomOffCommand(lightInRoomManager);

            controler.SetComands(0, lightInRoomOnCommand, lightInRoomOffCommand);

            System.Console.WriteLine(controler);

            controler.OnClick(0);
            controler.OffClick(0);

            GarageManager gm = new GarageManager();
            MacroCommand  mc = new MacroCommand();

            mc.AddComand(new GarageDoorDownCommnad(gm));
            mc.AddComand(new GarageDoorUpCommnad(gm));
            mc.AddComand(new GarageDoorStopCommand(gm));
            mc.AddComand(new GarageDoorLightOnCommand(gm));
            mc.AddComand(new GarageDoorLightOffCommand(gm));

            controler.SetComands(1, mc, lightInRoomOffCommand);
            System.Console.WriteLine();
            controler.OnClick(1);

            Title.BottonIndent();
        }
Beispiel #2
0
        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();
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            var commands = new List <ICommand>();

            commands.Add(new NullCommand());
            commands.Add(new WriteCommand());
            var com = new MacroCommand(commands);

            com.Execute();

            Console.ReadKey();
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            List <Command> commands = new List <Command>();

            commands.Add(new PasteCommand());
            commands.Add(new DrawCommand());

            Command macro = new MacroCommand(commands);

            macro.Execute();

            Console.ReadLine();
        }
Beispiel #5
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();
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            Pult pult = new Pult();
            TV   tv   = new TV();

            pult.SetCommand(new TVOnCommand(tv));
            pult.PressButton();
            pult.PressUndo();

            Microwave microwave = new Microwave();

            pult.SetCommand(new MicrowaveCommand(microwave, 5000));
            pult.PressButton();

            MacroCommand macro = new MacroCommand(new List <ICommand>()
            {
                new TVOnCommand(tv), new MicrowaveCommand(microwave, 5000)
            });

            pult.SetCommand(macro);
            pult.PressButton();
        }
        static void Main(string[] args)
        {
            Document    document  = new Document("doc1");
            Document    document2 = new Document("doc2");
            Application app       = new Application();

            app.Add(document);
            app.Add(document2);

            Command com = new OpenCommand(app);

            com.Execute();

            Command com2 = new PasteCommand(document2);

            com2.Execute();

            MacroCommand coms = new MacroCommand();

            coms.Add(com);
            coms.Add(com2);
            coms.Execute();
        }
Beispiel #8
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);
        }
        private static void Main(string[] args)
        {
            Console.WriteLine("======= Demonstration of Command =======");
            var doc = new Document();

            const string line1 = "Chapter: The command pattern.";
            const string line2 = "Section 1: Problem";
            const string line3 = "Command is a pattern that makes behavior an object.";

            Console.WriteLine("======= First - using method calls =======");
            doc.Write(line1);
            doc.Write(line2);
            doc.Write(line3);
            Console.WriteLine(doc);

            Console.WriteLine("---> Erasing last entered line");
            doc.Erase(line3);
            Console.WriteLine(doc);


            Console.WriteLine("======= Next - command objects assigned to F1..F3 =======");
            doc = new Document();
            // Create the commands
            ICommand write1 = new WriteCommand(doc, line1);
            ICommand write2 = new WriteCommand(doc, line2);
            ICommand write3 = new WriteCommand(doc, line3);
            // Note - nothing has happened to the document yet!

            // assign bindings to the F1 keys
            var f1 = new FKey();
            var f2 = new FKey();
            var f3 = new FKey();
            // assign write commands to the keys
            f1.Assign(write1);
            f2.Assign(write2);
            f3.Assign(write3);

            // next press F1 to F3 and see the result in the document
            f1.Press();
            f2.Press();
            f3.Press();
            Console.WriteLine(doc);

            // reassigning F2
            Console.WriteLine("======= F2 reassigned and pressed =======");
            ICommand write4 = new WriteCommand(doc, "A wrong line");
            f2.Assign(write4);
            f2.Press();
            Console.WriteLine(doc);

            // undoing the last operation
            Console.WriteLine("======= Undo of last insert =======");
            write4.Undo();
            Console.WriteLine(doc);

            Console.WriteLine("======= Next - macro magic =======");
            doc = new Document();
            var macro1 = new WriteCommand(doc, "Hello world!");
            var marco2 = new WriteCommand(doc, "Hello world, again...");
            var marco3 = new WriteCommand(doc, "Hello world, again and again...");

            ICommand macroCommand = new MacroCommand(doc, new[] { macro1, marco2, marco3});
            var macroKey = new FKey();
            macroKey.Assign(macroCommand);
            macroKey.Press();
            Console.WriteLine(doc);
            macroCommand.Undo();
            Console.WriteLine(doc);

            Console.Read();
        }
        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();
        }
Beispiel #11
0
 public void SetHistory(MacroCommand history) => this.history = history;
Beispiel #12
0
        private static void Main(string[] args)
        {
            Console.WriteLine("======= Demonstration of Command =======");
            var doc = new Document();

            const string line1 = "Chapter: The command pattern.";
            const string line2 = "Section 1: Problem";
            const string line3 = "Command is a pattern that makes behavior an object.";

            Console.WriteLine("======= First - using method calls =======");
            doc.Write(line1);
            doc.Write(line2);
            doc.Write(line3);
            Console.WriteLine(doc);

            Console.WriteLine("---> Erasing last entered line");
            doc.Erase(line3);
            Console.WriteLine(doc);


            Console.WriteLine("======= Next - command objects assigned to F1..F3 =======");
            doc = new Document();
            // Create the commands
            ICommand write1 = new WriteCommand(doc, line1);
            ICommand write2 = new WriteCommand(doc, line2);
            ICommand write3 = new WriteCommand(doc, line3);
            // Note - nothing has happened to the document yet!

            // assign bindings to the F1 keys
            var f1 = new FKey();
            var f2 = new FKey();
            var f3 = new FKey();

            // assign write commands to the keys
            f1.Assign(write1);
            f2.Assign(write2);
            f3.Assign(write3);

            // next press F1 to F3 and see the result in the document
            f1.Press();
            f2.Press();
            f3.Press();
            Console.WriteLine(doc);

            // reassigning F2
            Console.WriteLine("======= F2 reassigned and pressed =======");
            ICommand write4 = new WriteCommand(doc, "A wrong line");

            f2.Assign(write4);
            f2.Press();
            Console.WriteLine(doc);

            // undoing the last operation
            Console.WriteLine("======= Undo of last insert =======");
            write4.Undo();
            Console.WriteLine(doc);

            Console.WriteLine("======= Next - macro magic =======");
            doc = new Document();
            var macro1 = new WriteCommand(doc, "Hello world!");
            var marco2 = new WriteCommand(doc, "Hello world, again...");
            var marco3 = new WriteCommand(doc, "Hello world, again and again...");

            ICommand macroCommand = new MacroCommand(doc, new[] { macro1, marco2, marco3 });
            var      macroKey     = new FKey();

            macroKey.Assign(macroCommand);
            macroKey.Press();
            Console.WriteLine(doc);
            macroCommand.Undo();
            Console.WriteLine(doc);

            Console.Read();
        }
Beispiel #13
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);
        }