您的位置:新葡亰496net > 奥门新萄京娱乐场 > 指令情势,完毕命令设计形式

指令情势,完毕命令设计形式

发布时间:2019-10-20 23:34编辑:奥门新萄京娱乐场浏览(190)

    正文的定义内容来自开首设计方式一书.

    07 《Head First设计方式》 读书笔记07 封装调用:命令方式

    一、   核心概述

    项目须求

    新葡亰496net 1

    有这么叁个可编制程序的新型遥控器, 它有7个可编制程序插槽, 每种插槽可连接区别的家用电器设备. 种种插槽对应五个按键: 开, 关(ON, OFF). 另外还可能有三个大局的吊销按键(UNDO).

    当今客商想使用这些遥控器来决定分化厂商的小家用电器, 例如电灯, 太阳光能热水器, 风扇, 音响等等.

    顾客提议让自己编写三个接口, 可以让这几个遥控器调整插在插槽上的一个或风流倜傥组织设立备.

    看一下当下各家厂家都有怎样家电:

    新葡亰496net 2

    标题来了, 那几个家电并从未豆蔻梢头块的标准....差相当的少各自都有谈得来的龙马精神套调节方法.. 并且其后还要加上很二种家电.

     

      问题:只要有八个调整器,该调控器上有7个可编制程序的插槽,每一个都得以钦点到一个分歧的小家用电器装置,每一种插槽都有相应的开关按键。这一个遥控器还具有四个完璧归赵的撤除按键。商家已经提供了决定家电基本管理类。希望您可以知道创建黄金时代组决定遥控器的API,让每个插槽都能够支配叁个或黄金年代组设置。(如下图,商家类)

    统一计划思路

    那就必要思考一下解决方案了:

    第大器晚成要思量分离关切点(Separation of concerns),  遥控器应该可以表达开关动作并得以发送央求, 然则它不应该通晓家电和哪些按钮家电等.

    唯独当前遥控器只可以做按钮成效, 那么怎么让它去调整电灯或许音响呢? 我们不想让遥控器知道这一个实际的家电, 更不想写出上面包车型大巴代码:

    if slot1 == Light then Light.On()
    else if slot1 == Hub....
    

    聊到那就只可以提到指令格局(Command Pattern)了.

    命令方式允许你把动作的诉求者和动作的莫过于履行者解耦. 这里, 动作的伏乞者正是遥控器, 而施行动作的对象正是有个别家电.

    那是怎么解耦的吧? 怎么或许达成呢?

    那就须要引入"命令对象(command object)"了. 命令对象会封装在某个对象上(比方主卧的灯)实行有些动作的央浼(比方开灯). 所以, 如若大家为每贰个按键都筹划叁个发令对象, 那么当按键被按下的时候, 大家就能够调用那么些命令对象去实行有些动作. 遥控器本身并不知器具体实践的动作是何许, 它只是有多少个指令对象, 那些命令对象通晓去对什么电器去做什么样的操作. 就这么, 遥控器和电灯解耦了.

    主题素材引进

      有一个黏附多组按键按键的遥控器,带有可编制程序插槽,各样都足以内定到多少个两样的家电装置;有为数不菲商家开采的各样家电装置调节类;希望创制意气风发组API,让每种插槽调控三个或风姿罗曼蒂克组织设立置。

      思量:不应该让遥控器知道太多商家类的内部原因,否则更加多的小家用电器加进去,就务须修改代码。

     新葡亰496net 3

    多少个发令情势的实际上例子

    多少个快餐厅:

    新葡亰496net 4

    客户给侍者订单, 服务员把订单放到柜台并说: "有新订单了", 然后大厨依据订单准备饭菜.

    让我们紧密解析一下它们是怎么交互的:

    新葡亰496net 5

    客商来了, 说自家想要亚特兰洲大学, 奶酪....正是创制了一个订单 (createOrder()).

    订单上面写着客商想要的饭菜. 

    侍者获得订单 takeOrder(), 把订单获得柜台喊道: "有新订单了" (调用orderUp())

    炊事员遵照订单的指令把饭菜做好 (orderUp()里面包车型地铁动作). 

     

    浅析一下那么些事例的角色和职分:

    • 订单里封装了做饭菜的央浼. 能够把订单想象成一个目的, 那个目的仿佛对起火这一个动作的诉求. 並且它能够来回传递. 订单达成了一个只有orderUp()方法的接口, 这些法子里面封装了起火的操作流程. 订单况且对动作实行者的引用(大厨). 因为都卷入了, 所以推销员不知道订单里面有啥也不晓得厨子是什么人. 前台经理只传递订单, 并调用orderUp().
    • 故此, 推销员的做事正是传递订单同时调用orderUp(). 前台经理的取订单takeOrder()方法会传进来不相同的参数(分歧顾客的不等订单), 不过那不是主题材料, 因为他知晓全部的订单都帮助orderUp()方法.
    • 大师傅知道什么样把饭做好. 豆蔻年华旦推销员调用了orderUp(), 大厨就接管了全方位办事把饭菜做好. 可是服务生和厨子是解耦的: 看板娘唯有订单, 订单里封装着饭菜, 推销员只是调用订单上的一个艺术而已. 同样的, 厨神只是从订单上收到指令, 他从没和服务员风流倜傥直接触.

     

      分析1:厂家提供的类,有非常多都怀有On()和off()方法,除此而外还恐怕有一点点别样的好多的章程。

    项目设计图

    回到大家的供给, 参谋快餐店的事例, 使用命令方式做一下规划:

    新葡亰496net 6

    客商Client成立了一个命令(Command)对象. 相当于旁人拿起了三个订单(点菜)策画上马点菜, 作者在钻探遥控器的槽须要插哪些家电. 命令对象和接收者是绑定在联合签名的. 约等于菜单和厨神, 遥控器的插槽和指标家电.

    指令情势,完毕命令设计形式。命令对象独有四个方法execute(), 里面封装了调用接收者实际决定操作的动作. 也正是酒馆订单的orderUp().

    客商调用setCommand()方法. 相当于客商想好点什么菜了, 就写在订单上面了. 小编也想好遥控器要调控什么家用电器了, 列好清单了. 

    调用者拿着早就setCommand的吩咐对象, 在将来某些时间点调用命令对象方面包车型大巴execute()方法. 也正是前台经理拿起订单走到柜台前, 大喊一声: "有订单来了, 开始做菜吧". 相当于自身把遥控器和设施的接口连接上了, 准备上马调控.

    最终接收者执行动作. 相当于大厨做饭. 家用电器使用自身独有的支配措施进行动作.

    这里面:

    客商 --- 饭馆客人, 作者

    命令 --- 订单, 插槽

    调用者 --- 服务员, 遥控器

    setCommand()设置命令 --- takeOrder() 取订单, 插上急需调节的电器

    execute() 施行 ---  orderUp() 告诉柜台做饭, 按按键

    接收者 --- 厨师, 家电

    缓慢解决方案

      使用命令格局,将动作的乞请者从动作的实行者对象中解耦。

      在该难点中,哀告者能够是遥控器,而实施者对象正是商家类当中之旭日东升的实例。

      利用“指令对象”,把须求封装成一个特定目的,每一个开关都存款和储蓄三个命令对象,当开关被按下的时候,就能够请命令对象做连锁的做事。

      遥控器并无需知道事行业内部容是如何,只要有个指令对象能和不利的对象关联,把事情做好就足以了。

      分析2:还不只那样,听上去就好像现在还有越多的厂家类,并且各种类还也可以有二种多种的艺术。

    代码施行

    不无命令对象须求达成的接口:

    namespace CommandPattern.Abstractions
    {
        public interface ICommand
        {
            void Execute();
        }
    }
    

    一盏灯:

    using System;
    
    namespace CommandPattern.Devices
    {
        public class Light
        {
            public void On()
            {
                Console.WriteLine("Light is on");
            }
    
            public void Off()
            {
                Console.WriteLine("Light is off");
            }
        }
    }
    

    调节灯张开的吩咐:

    using CommandPattern.Abstractions;
    using CommandPattern.Devices;
    
    namespace CommandPattern.Commands
    {
        public class LightOnCommand : ICommand
        {
            private readonly Light light;
    
            public LightOnCommand(Light light)
            {
                this.light = light;
            }
    
            public void Execute()
            {
                this.light.On();
            }
        }
    }
    

    车库门: 

    using System;
    
    namespace CommandPattern.Devices
    {
        public class GarageDoor
        {
            public void Up()
            {
                Console.WriteLine("GarageDoor is opened.");
            }
    
            public void Down()
            {
                Console.WriteLine("GarageDoor is closed.");
            }
        }
    }
    

    收起车库门命令:

    using CommandPattern.Abstractions;
    using CommandPattern.Devices;
    
    namespace CommandPattern.Commands
    {
        public class GarageDoorOpen : ICommand
        {
            private readonly GarageDoor garageDoor;
    
            public GarageDoorOpen(GarageDoor garageDoor)
            {
                this.garageDoor = garageDoor;
            }
    
            public void Execute()
            {
                garageDoor.Up();
            }
        }
    }
    

    简简单单的遥控器:

    using CommandPattern.Abstractions;
    
    namespace CommandPattern.RemoteControls
    {
        public class SimpleRemoteControl
        {
            public ICommand Slot { get; set; }
    public void ButtonWasPressed()
            {
                Slot.Execute();
            }
        }
    }
    

    运作测验:

    using System;
    using CommandPattern.Commands;
    using CommandPattern.Devices;
    using CommandPattern.RemoteControls;
    
    namespace CommandPattern
    {
        class Program
        {
            static void Main(string[] args)
            {
                var remote = new SimpleRemoteControl();
                var light = new Light();
                var lightOn = new LightOnCommand(light);
    
                remote.Slot = lightOn;
                remote.ButtonWasPressed();
    
                var garageDoor = new GarageDoor();
                var garageDoorOpen = new GarageDoorOpenCommand(garageDoor);
    
                remote.Slot = garageDoorOpen;
                remote.ButtonWasPressed();
            }
        }
    }
    

    新葡亰496net 7

     

      分析3:遥控器应该清楚如何解读按键被按下的动作,然后发出正确的央浼,然而遥控器不需清楚这么些家用电器自动化的细节。

    指令格局定义

    指令格局把乞请封装成一个对象, 进而能够动用分化的央求对另外对象开展参数化, 对供给排队, 记录需要的历史, 并扶持打消操作.

    类图:

    新葡亰496net 8

    效果图:

    新葡亰496net 9

    用实例证实

      在餐厅,客户将和煦的急需写在订单上,由推销员交给大厨,然后厨神依据订单指令筹划餐点。

      分析4:大家不想让遥控器包含一大堆if语句,举例“if slot1==Light,then light.On(),else if slot1==Hottub,then hottub.JetsOn()”.我们都晓得那样的布置性很倒霉。且假若有新的商家类进来,就务须修改现存的代码,产生潜在的失实。

    全职能代码的实施

    遥控器:

    using System.Text;
    using CommandPattern.Abstractions;
    using CommandPattern.Commands;
    
    namespace CommandPattern.RemoteControls
    {
        public class RemoteControl
        {
            private ICommand[] onCommands;
            private ICommand[] offCommands;
    
            public RemoteControl()
            {
                onCommands = new ICommand[7];
                offCommands = new ICommand[7];
    
                var noCommand = new NoCommand();
                for (int i = 0; i < 7; i  )
                {
                    onCommands[i] = noCommand;
                    offCommands[i] = noCommand;
                }
            }
    
            public void SetCommand(int slot, ICommand onCommand, ICommand offCommand)
            {
                onCommands[slot] = onCommand;
                offCommands[slot] = offCommand;
            }
    
            public void OnButtonWasPressed(int slot)
            {
                onCommands[slot].Execute();
            }
            public void OffButtonWasPressed(int slot)
            {
                offCommands[slot].Execute();
            }
    
            public override string ToString()
            {
                var sb = new StringBuilder("n------------Remote Control-----------n");
                for(int i =0; i< onCommands.Length; i  ){
                    sb.Append($"[slot{i}] {onCommands[i].GetType()}t{offCommands[i].GetType()} n");
                }
                return sb.ToString();
            }
        }
    }
    

    那之中有二个NoCommand, 它是叁个空的类, 只是为着发轫化command 以便今后绝不推断是不是为null.

    关灯:

    using CommandPattern.Abstractions;
    using CommandPattern.Devices;
    
    namespace CommandPattern.Commands
    {
        public class LightOffCommand: ICommand
        {
            private readonly Light light;
    
            public LightOffCommand(Light light)
            {
                this.light = light;
            }
    
            public void Execute()
            {
                light.Off();
            }
        }
    }
    

    下边试多个不怎么挑战性的, 音响:

    namespace CommandPattern.Devices
    {
        public class Stereo
        {
            public void On()
            {
                System.Console.WriteLine("Stereo is on.");
            }
    
            public void Off()
            {
                System.Console.WriteLine("Stereo is off.");
            }
    
            public void SetCD()
            {
                System.Console.WriteLine("Stereo is set for CD input.");
            }
    
            public void SetVolume(int volume)
            {
                System.Console.WriteLine($"Stereo's volume is set to {volume}");
            }
        }
    }
    

    响声打开命令:

    using CommandPattern.Abstractions;
    
    namespace CommandPattern.Devices
    {
        public class StereoOnWithCDCommand : ICommand
        {
            private readonly Stereo stereo;
    
            public StereoOnWithCDCommand(Stereo stereo)
            {
                this.stereo = stereo;
            }
    
            public void Execute()
            {
                stereo.On();
                stereo.SetCD();
                stereo.SetVolume(10);
            }
        }
    }
    

    测量检验运转:

    using System;
    using CommandPattern.Commands;
    using CommandPattern.Devices;
    using CommandPattern.RemoteControls;
    
    namespace CommandPattern
    {
        class Program
        {
            static void Main(string[] args)
            {
                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);
            }
        }
    }
    

    新葡亰496net 10

    该须求的设计图:

    新葡亰496net 11

    还应该有三个标题...撤销按键呢?

     

      分析5:指令格局可以将“动作的乞请者”从“动作的执行者”对象中解耦,在这里个事例中,央浼者能够是遥控器,而实行者对象正是厂商类在那之中之龙马精神的实例。

    兑现打消按键

    1. 能够在ICommand接口里面增多一个undo()方法, 然后在中间施行上三回动作相反的动作就可以:

      namespace CommandPattern.Abstractions {

       public interface ICommand
       {
           void Execute();
           void Undo();
       }
      

      }

    譬喻说开灯:

    using CommandPattern.Abstractions;
    using CommandPattern.Devices;
    
    namespace CommandPattern.Commands
    {
        public class LightOnCommand : ICommand
        {
            private readonly Light light;
    
            public LightOnCommand(Light light)
            {
                this.light = light;
            }
    
            public void Execute()
            {
                light.On();
            }
    
            public void Undo()
            {
                light.Off();
            }
        }
    }
    

    遥控器:

    using System.Text;
    using CommandPattern.Abstractions;
    using CommandPattern.Commands;
    
    namespace CommandPattern.RemoteControls
    {
        public class RemoteControlWithUndo
        {
            private ICommand[] onCommands;
            private ICommand[] offCommands;
            private ICommand undoCommand;
    
            public RemoteControlWithUndo()
            {
                onCommands = new ICommand[7];
                offCommands = new ICommand[7];
    
                var noCommand = new NoCommand();
                for (int i = 0; i < 7; i  )
                {
                    onCommands[i] = noCommand;
                    offCommands[i] = noCommand;
                }
                undoCommand = noCommand;
            }
    
            public void SetCommand(int slot, ICommand onCommand, ICommand offCommand)
            {
                onCommands[slot] = onCommand;
                offCommands[slot] = offCommand;
            }
    
            public void OnButtonWasPressed(int slot)
            {            
                onCommands[slot].Execute();
                undoCommand = onCommands[slot];
            }
    
            public void OffButtonWasPressed(int slot)
            {
                offCommands[slot].Execute();
                undoCommand = offCommands[slot];
            }
    
            public void UndoButtonWasPressed()
            {
                undoCommand.Undo();
            }
    
            public override string ToString()
            {
                var sb = new StringBuilder("n------------Remote Control-----------n");
                for(int i =0; i< onCommands.Length; i  ){
                    sb.Append($"[slot{i}] {onCommands[i].GetType()}t{offCommands[i].GetType()} n");
                }
                return sb.ToString();
            }
        }
    }
    

    测量试验一下:

    using System;
    using CommandPattern.Commands;
    using CommandPattern.Devices;
    using CommandPattern.RemoteControls;
    
    namespace CommandPattern
    {
        class Program
        {
            static void Main(string[] args)
            {
                var remote = new RemoteControl();
                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.OnButtonWasPressed(1);
                remote.OffButtonWasPressed(1);
            }
        }
    }
    

    新葡亰496net 12

    主导是OK的, 可是有一点点不是难题, 音响的按钮状态倒是撤消了, 但是它的轻重(也囊括播放介质, 可是那些自家就不去得以实现了)并从未苏醒.

    上面就来管理一下那一个难题.

    修改Stereo:

    namespace CommandPattern.Devices
    {
        public class Stereo
        {
    
            public Stereo()
            {
                Volume = 5;
            }
    
            public void On()
            {
                System.Console.WriteLine("Stereo is on.");
            }
    
            public void Off()
            {
                System.Console.WriteLine("Stereo is off.");
            }
    
            public void SetCD()
            {
                System.Console.WriteLine("Stereo is set for CD input.");
            }
    
            private int volume;
            public int Volume
            {
                get { return volume; }
                set
                {
                    volume = value;
                    System.Console.WriteLine($"Stereo's volume is set to {volume}");
                }
            }
    
        }
    }
    

    命令:

    using CommandPattern.Abstractions;
    
    namespace CommandPattern.Devices
    {
        public class StereoOnWithCDCommand : ICommand
        {
            private int previousVolume;
    
            private readonly Stereo stereo;
    public StereoOnWithCDCommand(Stereo stereo)
            {
                this.stereo = stereo;
           previousVolume = stereo.Volume;
            }
    
            public void Execute()
            {
                stereo.On();
                stereo.SetCD();
                stereo.Volume = 10;
            }
    
            public void Undo()
            {
                stereo.Volume = previousVolume;
                stereo.SetCD();
                stereo.Off();
            }
        }
    }
    

    运行:

    新葡亰496net 13

    概念命令方式

      指令情势将“诉求”封装成对象,以便利用不一样的伏乞、队列可能日志来参数化其余对象。

      一声令下方式也协理可裁撤的操作。

      命令对象将动作和接收者包进对象中。

      分析6:运用命令对象,把乞求(比如展开电灯)封装成贰个特定对象(举个例子客厅电灯对象)。所以,假使对种种按键都存款和储蓄二个命令对象,那么当按键被按下的时候,就足以请命令对象做相关的劳作。遥控器并没有供给知道工作内容是怎么着,只要有个指令对象能喝不利的靶子关联,把作业做好就足以了。

    须求改变----三个按键调控几个设施的五个动作

    Party Mode (集会情势):

    思路是开创黄金时代种命令, 它能够施行多个其余命令

    MacroCommand:

    using CommandPattern.Abstractions;
    
    namespace CommandPattern.Commands
    {
        public class MacroCommand : ICommand
        {
            private ICommand[] commands;
    
            public MacroCommand(ICommand[] commands)
            {
                this.commands = commands;
            }
    
            public void Execute()
            {
                for (int i = 0; i < commands.Length; i  )
                {
                    commands[i].Execute();
                }
            }
    
            public void Undo()
            {
                for (int i = 0; i < commands.Length; i  )
                {
                    commands[i].Undo();
                }
            }
        }
    }
    

    选择那一个MacroCommand:

    using System;
    using CommandPattern.Abstractions;
    using CommandPattern.Commands;
    using CommandPattern.Devices;
    using CommandPattern.RemoteControls;
    
    namespace CommandPattern
    {
        class Program
        {
            static void Main(string[] args)
            {
                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);
            }
        }
    }
    

    新葡亰496net 14

     

     

      分析7:动用这么些形式,大家能够创制二个API,将那么些命令对象加载到按钮插槽,让遥控器的代码尽量保持轻易。而把家用电器自动化的行事和拓宽该工作的对象一齐封装在命令对象中。

    命令情势其实采用举个例子

    指令方式的类图

     新葡亰496net 15

     

     

    用命令方式实现遥控器难题的类图:

    新葡亰496net 16 

     

    二、详细表明

    呼吁队列

    新葡亰496net 17

    其蒸蒸日上职业行列是如此职业的: 你增加命令到行列的最终, 在队列的另意气风发端有几个线程. 线程那样工作: 它们从队列移除二个限令, 调用它的execute()方法, 然后等待调用甘休, 然后遗弃这一个命令再赢得壹个新的命令.

    那样大家就足以把计算量限制到稳固的线程数上面了. 专门的学问行列和做工作的目的也是解耦的.

    撤除操作的落实

      在Command接口中参与undo()方法,然后每种具体的命令类都完毕那些点子:undo方法中调用的操作和execute()方法中的操作相反,比方on的反面是off。

      遥控器类也要做一些改变:到场新的实例变量记录上一个操作,当按下裁撤按键,就调用该实例变量的undo()方法。

      若是打消到某贰个场地要求记录一些变量值,则在execute()方法中步入保存操作前状态数值的话语,然后在undo()的时候恢复。

      假设想要数十次撤销,即撤消到很早从前的状态,可以行使两个仓房记录操作进度的每三个指令。

      命令情势:将呼吁封装成对象,那能够令你使用差异的呼吁、队列,也许日志诉求来参数化别的对象。命令情势也能够补助撤除操作。

    记录诉求

    新葡亰496net 18

    新葡亰496net 19

    其意气风发例子就是利用命令形式记录央求动作的野史, 如若出题目了, 能够依照那个历史进行复苏.

     

     

      今后留心看那些概念,我们掌握四个指令对象通过在一定接收者上绑定风姿罗曼蒂克组动作来封装多个呼吁。要达成那或多或少,命令对象将动作和接收者包进对象中。这些指标只暴流露多个execute()方法,当此方法被调用的时候,接收者就能够开展那个动作。从外边来看,别的对象不晓得毕竟哪位接收者进行了什么动作,只略知后生可畏二借使调用execute()方法,央浼的指标就能够到达。

    其它

    新葡亰496net,那么些连串的代码笔者放在那处了: 

     

    宏命令

      通过二个键,调节七种操作。

      在宏命令中,用命令数组存款和储蓄一大堆命令,当以此宏命令被遥控器实践时,就二次性推行数组里的各样命令。

    1.定义命令情势类图:

     

     新葡亰496net 20

    命令格局的更加多用途

     

    队列伏乞

      命令能够将运算块打包(多个接收者和意气风发组动作),然后将它传来传去,就如对日常的对象同样。

      咱们得以动用那样的特征衍生一些接纳,例如:日程安插,线程池,职业行列等。

      想象有二个干活行列:你在生龙活虎端增加命令,然后另勃勃生机端则是线程。

      线程从队列中抽取命令,调用它的execute()方法,等待那个调用完毕,然后将此命令对象遗弃,在抽取下二个发令……

     

    日记伏乞

      有些应用须要大家将具有的动作都记录在日记中,并能在系统死机之后,重新调用这么些动作恢复生机到以前的情景。

      通过新扩张八个点子store()、load(),命令方式就可以见到协理那一点。

      在Java中,大家能够应用指标的种类化(Serialization)完结那个主意,不过平常以为种类化最棒或然只用在对象的长久化上(persistence)。

      怎么办:当我们实践命令的时候,将历史记录积攒在磁盘中。后生可畏旦系统死机,我们就足以将下令对象重新加载,并成批地依次调用那几个目的的execute()方法。

     

    题外话:《Head First设计方式》读书笔记种类的换代暂停,作者不会告诉你是因为图书馆的书超期况且被人约定了为此本身借不到了。年后开课再说吧,希望本人能持有始有终。

     

    2.上边包车型客车类图提供了统一谋算的全貌:

     新葡亰496net 21

     

    问:假若全体了二个遥控器,却不可能光凭按下三个开关,就同一时候能弄暗电灯的光、张开音响和电视机、设置好mp4,并让热水器起头加温,那么要以此遥控器还应该有啥意义?

    答:此刻就可以用到命令形式的拉开“宏命令”,创设后生可畏种新的指令,用来施行别的一群命令。在宏命令中,用命令数组(集结)存款和储蓄一大堆命令,当以此宏命令被遥控器实践时,就一回性实行数组里的每种命令。(具体内容,可在代码列表查看)

     

    问:收信人一定有要求存在吗?为啥命令对象不直接实现execute()方法的底细。

    答:相似的话,大家尽量设计“笨蛋”命令对象,它只精通调用贰个接收者的七个行为。但是,有过多“聪明”命令对象会贯彻广大逻辑,直接到位二个伸手。当然你能够安排聪明的授命对象,只是那样一来,调用者和接收者之间的解耦程度是不如“呆子”命令对象的,何况,你也不可能把接收者当作参数字传送给命令。

     

    问:本人得以成立PartyCommand,然后在它的execute()方法中调用别的的一声令下,利用这种做法完成Party方式(Party Mode)吗?

    答:您能够这么做。然后,那约等于把Party情势“硬编码”到PartyCommand中。为啥要那样费劲呢?利用宏命令,你可以动态地决定PartyCommand是由什么命令组成,所以宏命令在选择上越来越灵敏。平时的话,宏命令的做法更加高贵,也亟需少之又少的新代码。

     

    问:作者哪些能够达成多档案的次序的吊销操作?换句话说,作者希望可以按下撤消按键许数十三遍,撤除往很早很早从前的境况。

    答:事实上这一定轻易形成,不要只记录最终二个被施行的命令,而选取二个库房记录操作进度的每三个限令。然后,不管什么样时候按下了收回开关,你都能够从货仓中抽取最上层的下令,然后调用它的Undo()方法。

     

    3.下令形式的更加多用途:

      (1)队列央求:命令可以将运算块打包(二个接收者微风姿罗曼蒂克组动作),然后将它传来传去,就好像日常的指标同样。它依然足以在不一致的线程中被调用。大家得以采纳那样的特色衍生一些运用,举例:日程布置、线程池、职业行列等。

    虚拟有一个干活行列,你在某风姿罗曼蒂克端加多命令,然后另新闯祸物正在蒸蒸日上端则是线程,线程举办上边包车型地铁动作,从队列中收取三个限令,调用它的Execute()方法,等待这么些调用完毕,然后将此命令对象舍弃,再抽取下贰个命令......

      (2)日记央求:一些应用需求大家将持有的动作都记录在日记中,并能在系统死机之后,重新调用这一个动作复苏到以前的情事。通过新增加多个办法(Store()、Load()),命令方式能够协理那或多或少。这种日志的主意对于遥控器来讲未有趣,不过,有不菲调用大型数据结构的动作的应用,不恐怕在每便改变发生时被高效地蕴藏。通过运用记录日志,大家得以将上次检查点之后的全部操作记录下来,如若系统出现景况,从检查点最先运用那些操作。

    比方说,对于手表格应用,大家只怕想要达成的谬误恢复生机措施是将时钟格的操作记录在日记中,实际不是历次电子钟格如火如荼有转移就记下整个手表格。对更加高等的施用来讲,这么些本事能够被增添应用到事务管理中,也等于说,一整组操作必须形成后才算有效。

     

    4.总结:

    (1)当供给将发出央求的指标和执行乞求的指标解耦的时候,使用命令方式

    (2)在被解耦的两个之间是通过命令对象开展关联的,命令对象封装了收信人和二个或风姿罗曼蒂克组动作。

    (3)调用者通过调用命令对象的execute()发出央求,那会使得接收者的动作被调用。

    (4)调用者还行命令充作参数,以至在运作时动态地张开。

    (5)命令能够支持裁撤,做法是促成二个Undo()方法来回到execute()被实行前的景况。

    (6)宏命令是命令的后生可畏种简单的延长,允许调用多个指令,宏方法也足以辅助撤废。

    (7)命令也足以用来落实日志和业务系统。

    三、代码列表

    新葡亰496net 22新葡亰496net 23

    /// <summary>
    /// 命令接口
    /// </summary>
    public interface ICommand
    {
        /// <summary>
        /// 执行
        /// </summary>
        void Execute();
        /// <summary>
        /// 撤销
        /// </summary>
        void Undo();
    }
    public class RemoteControl
    {
        private ICommand[] onCommands;
        private ICommand[] offCommands;
        private ICommand undoCommand;
    
        public RemoteControl()
        {
            onCommands = new ICommand[7];
            offCommands = new ICommand[7];
            ICommand noCommand = new NoCommand();
            for (int i = 0; i < 7; i  )
            {
                onCommands[i] = noCommand;
                offCommands[i] = noCommand;
            }
            undoCommand = noCommand;
        }
    
        public void SetCommand(int slot, ICommand onCommand, ICommand offCommand)
        {
            onCommands[slot] = onCommand;
            offCommands[slot] = offCommand;
        }
    
        public void OnButtonWasPushed(int slot)
        {
            onCommands[slot].Execute();
            undoCommand = onCommands[slot];
        }
    
        public void OffButtonWasPushed(int slot)
        {
            offCommands[slot].Execute();
            undoCommand = offCommands[slot];
        }
    
        public void UndoButtonWasPushed()
        {
            undoCommand.Undo();
        }
    
        public override string ToString()
        {
            StringBuilder sb=new StringBuilder();
            sb.Append("n---------Remote Control----------n");
            for (int i = 0; i < onCommands.Length; i  )
            {
                sb.AppendFormat("[slot {0}] {1}    {2}",i,onCommands[i].GetType().FullName, offCommands[i].GetType().FullName);
                sb.AppendLine();
            }
            return sb.ToString();
        }
    }
    public class SimpleRemoteControl
    {
        private ICommand slot;
    
        public void SetCommand(ICommand command)
        {
            slot = command;
        }
    
        public void ButtonWasPressed()
        {
            slot.Execute();
        }
    }
    //Worker
    /// <summary>
    /// 吊扇
    /// </summary>
    public class CeilLingFan
    {
        public CeilLingFanSpeed speed { get; private set; } = 0;
        private string name;
    
        public CeilLingFan(string name)
        {
            this.name = name;
        }
    
        /// <summary>
        /// 高速
        /// </summary>
        public void High()
        {
            speed = CeilLingFanSpeed.High;
            Console.WriteLine("{0} ceilLingFan is highs", name);
        }
        /// <summary>
        /// 中速
        /// </summary>
        public void Medium()
        {
            speed = CeilLingFanSpeed.Medium;
            Console.WriteLine("{0} ceilLingFan is medium", name);
        }
        /// <summary>
        /// 低速
        /// </summary>
        public void Low()
        {
            speed = CeilLingFanSpeed.Low;
            Console.WriteLine("{0} ceilLingFan is low", name);
        }
        /// <summary>
        /// 关闭
        /// </summary>
        public void Off()
        {
            speed = CeilLingFanSpeed.Off;
            Console.WriteLine("{0} ceilLingFan is Off", name);
        }
    
        public enum CeilLingFanSpeed { High = 3, Medium = 2, Low = 1, Off = 0 }
    }
    /// <summary>
    /// 车库门
    /// </summary>
    public class GarageDoor
    {
        private string name;
    
        public GarageDoor(string name)
        {
            this.name = name;
        }
    
        /// <summary>
        /// 上升
        /// </summary>
        public void Up()
        {
            Console.WriteLine("{0} Garage door is open", name);
        }
        /// <summary>
        /// 下降
        /// </summary>
        public void Down()
        {
            Console.WriteLine("{0} Garage door is close", name);
        }
        /// <summary>
        /// 停止
        /// </summary>
        public void Stop()
        {
            Console.WriteLine("{0} Garage door is stop", name);
        }
        /// <summary>
        /// 灯光开启
        /// </summary>
        public void LightOn()
        {
            Console.WriteLine("{0} Garage Light is on", name);
        }
        /// <summary>
        /// 灯光关闭
        /// </summary>
        public void LightOff()
        {
            Console.WriteLine("{0} Garage Light is off", name);
        }
    }
    /// <summary>
    /// 热浴盆
    /// </summary>
    public class Hottub
    {
        /// <summary>
        /// 水循环
        /// </summary>
        public void Circulata()
        {
            Console.WriteLine("Tub water is circulata");
        }
        /// <summary>
        /// 喷射打开
        /// </summary>
        public void JetsOn()
        {
            Console.WriteLine("Jets is on");
        }
        /// <summary>
        /// 喷射关闭
        /// </summary>
        public void JetsOff()
        {
            Console.WriteLine("Jets is Off");
        }
        /// <summary>
        /// 设置温度
        /// </summary>
        public void SetTemperature()
        {
            Console.WriteLine("Default temperature is 46°C");
        }
    }
    /// <summary>
    /// 灯光
    /// </summary>
    public class Light
    {
        private string name;
    
        public Light(string name)
        {
            this.name = name;
        }
    
        /// <summary>
        /// 开启
        /// </summary>
        public void On()
        {
            Console.WriteLine("{0} light is On", name);
        }
        /// <summary>
        /// 关闭
        /// </summary>
        public void Off()
        {
            Console.WriteLine("{0} light is Off", name);
        }
    }
    /// <summary>
    /// 立体声
    /// </summary>
    public class Stereo
    {
        private string name;
    
        public Stereo(string name)
        {
            this.name = name;
        }
    
        /// <summary>
        /// 开启
        /// </summary>
        public void On()
        {
            Console.WriteLine("{0} Stereo is on", name);
        }
        /// <summary>
        /// 关闭
        /// </summary>
        public void Off()
        {
            Console.WriteLine("{0} Stereo is off", name);
        }
        /// <summary>
        /// 设置CD
        /// </summary>
        public void SetCd()
        {
            Console.WriteLine("Stereo Cd is on");
        }
        /// <summary>
        /// 设置DVD
        /// </summary>
        public void SetDvd()
        {
            Console.WriteLine("Stereo Dvd is on");
        }
        /// <summary>
        /// 设置收音机
        /// </summary>
        public void SetRadio()
        {
            Console.WriteLine("Stereo radio is on");
        }
        /// <summary>
        /// 设置音量
        /// </summary>
        public void SetVolume(byte volume)
        {
            Console.WriteLine("Stereo volume is {0}", volume);
        }
    }
    /// <summary>
    /// 电视
    /// </summary>
    public class TV
    {
        private string name;
    
        public TV(string name)
        {
            this.name = name;
        }
    
        /// <summary>
        /// 开启
        /// </summary>
        public void On()
        {
            Console.WriteLine("{0} TV is on", name);
        }
        /// <summary>
        /// 关闭
        /// </summary>
        public void Off()
        {
            Console.WriteLine("{0} TV is off", name);
        }
        /// <summary>
        /// 设置频道
        /// </summary>
        public void SetInputChannel()
        {
            Console.WriteLine("Defualt channel is CCTV1");
        }
        /// <summary>
        /// 设置音量
        /// </summary>
        public void SetVolume()
        {
            Console.WriteLine("Defualt volume is 5");
        }
    }
    //Commands
    /// <summary>
    /// 吊扇基础命令类
    /// </summary>
    public abstract class CeilingFanCommand : ICommand
    {
        protected CeilLingFan ceilLingFan;
        private CeilLingFan.CeilLingFanSpeed speed;
    
        public void Execute()
        {
            speed = ceilLingFan.speed;
            FanExecute();
        }
    
        public abstract void FanExecute();
    
        public void Undo()
        {
            switch (speed)
            {
                case CeilLingFan.CeilLingFanSpeed.High:
                    ceilLingFan.High();
                    break;
                case CeilLingFan.CeilLingFanSpeed.Medium:
                    ceilLingFan.Medium();
                    break;
                case CeilLingFan.CeilLingFanSpeed.Low:
                    ceilLingFan.Low();
                    break;
                case CeilLingFan.CeilLingFanSpeed.Off:
                    ceilLingFan.Off();
                    break;
            }
        }
    }
    public class CeilLingFanHighCommand : CeilingFanCommand
    {
        public CeilLingFanHighCommand(CeilLingFan ceilLingFan)
        {
            base.ceilLingFan = ceilLingFan;
        }
    
        public override void FanExecute()
        {
            ceilLingFan.High();
        }
    }
    public class CeilLingFanLowCommand : CeilingFanCommand
    {
        public CeilLingFanLowCommand(CeilLingFan ceilLingFan)
        {
            base.ceilLingFan = ceilLingFan;
        }
    
        public override void FanExecute()
        {
            ceilLingFan.Low();
        }
    }
    public class CeilLingFanMediumCommand : CeilingFanCommand
    {
        public CeilLingFanMediumCommand(CeilLingFan ceilLingFan)
        {
            base.ceilLingFan = ceilLingFan;
        }
    
        public override void FanExecute()
        {
            ceilLingFan.Medium();
        }
    }
    public class CeilLingFanOffCommand : CeilingFanCommand
    {
        public CeilLingFanOffCommand(CeilLingFan ceilLingFan)
        {
            base.ceilLingFan = ceilLingFan;
        }
    
        public override void FanExecute()
        {
            ceilLingFan.Off();
        }
    }
    public class GarageDoorCloseCommand : ICommand
    {
        private GarageDoor garageDoor;
    
        public GarageDoorCloseCommand(GarageDoor garageDoor)
        {
            this.garageDoor = garageDoor;
        }
    
        public void Execute()
        {
            garageDoor.Down();
            garageDoor.LightOff();
        }
    
        public void Undo()
        {
            garageDoor.LightOn();
            garageDoor.Up();
        }
    }
    public class GarageDoorOpenCommand : ICommand
    {
        private GarageDoor garageDoor;
    
        public GarageDoorOpenCommand(GarageDoor garageDoor)
        {
            this.garageDoor = garageDoor;
        }
    
        public void Execute()
        {
            garageDoor.Up();
            garageDoor.LightOn();
        }
    
        public void Undo()
        {
            garageDoor.Down();
            garageDoor.LightOff();
        }
    }
    public class HottubOffCommand:ICommand
    {
        private Hottub hottub;
    
        public HottubOffCommand(Hottub hottub)
        {
            this.hottub = hottub;
        }
    
        public void Execute()
        {
            hottub.JetsOff();
        }
    
        public void Undo()
        {
            hottub.JetsOn();
        }
    }
    public class HottubOnCommand : ICommand
    {
        private Hottub hottub;
    
        public HottubOnCommand(Hottub hottub)
        {
            this.hottub = hottub;
        }
    
        public void Execute()
        {
            hottub.JetsOn();
            hottub.SetTemperature();
        }
    
        public void Undo()
        {
            hottub.JetsOff();
        }
    }
    public class LightOffCommand:ICommand
    {
        private Light light;
    
        public LightOffCommand(Light light)
        {
            this.light = light;
        }
    
        public void Execute()
        {
            light.Off();
        }
    
        public void Undo()
        {
            light.On();
        }
    }
    public class LightOnCommand : ICommand
    {
        private Light light;
    
        public LightOnCommand(Light light)
        {
            this.light = light;
        }
    
        public void Execute()
        {
            light.On();
        }
    
        public void Undo()
        {
            light.Off();
        }
    }
    /// <summary>
    /// 更多命令(宏命令)
    /// </summary>
    public class MacroCommand:ICommand
    {
        private ICommand[] commands;
    
        public MacroCommand(ICommand[] commands)
        {
            this.commands = commands;
        }
    
        public void Execute()
        {
            foreach (ICommand command in commands)
            {
                command.Execute();
            }
        }
    
        public void Undo()
        {
            foreach (ICommand command in commands)
            {
                command.Undo();
            }
        }
    }
    /// <summary>
    /// 没有命令
    /// </summary>
    public class NoCommand : ICommand
    {
        public void Execute()
        {
    
        }
    
        public void Undo()
        {
    
        }
    }
    public class StereoOffCommand : ICommand
    {
        private Stereo stereo;
    
        public StereoOffCommand(Stereo stereo)
        {
            this.stereo = stereo;
        }
    
        public void Execute()
        {
            stereo.Off();
        }
    
        public void Undo()
        {
            stereo.On();
        }
    }
    public class StereoOnCommand : ICommand
    {
        private Stereo stereo;
    
        public StereoOnCommand(Stereo stereo)
        {
            this.stereo = stereo;
        }
    
        public void Execute()
        {
            stereo.On();
        }
    
        public void Undo()
        {
            stereo.Off();
        }
    }
    public class StereoOnWithCDCommand:ICommand
    {
        private Stereo stereo;
    
        public StereoOnWithCDCommand(Stereo stereo)
        {
            this.stereo = stereo;
        }
    
        public void Execute()
        {
            stereo.On();
            stereo.SetCd();
            stereo.SetVolume(11);
        }
    
        public void Undo()
        {
            stereo.Off();
        }
    }
    public class TVOffCommand:ICommand
    {
        private TV tv;
    
        public TVOffCommand(TV tv)
        {
            this.tv = tv;
        }
    
        public void Execute()
        {
            tv.Off();
        }
    
        public void Undo()
        {
            tv.On();
        }
    }
    public class TVOnCommand : ICommand
    {
        private TV tv;
    
        public TVOnCommand(TV tv)
        {
            this.tv = tv;
        }
    
        public void Execute()
        {
            tv.On();
            tv.SetInputChannel();
            tv.SetVolume();
        }
    
        public void Undo()
        {
            tv.Off();
        }
    }
    //RunTest
    [Test]
    public void RemoteLoader()
    {
        //单个插槽的简单控制装置
        /*SimpleRemoteControl remote = new SimpleRemoteControl();
        Light light = new Light();
        GarageDoor garageDoor = new GarageDoor();
        LightOnCommand lightOn = new LightOnCommand(light);
        GarageDoorOpenCommand garageOpen = new GarageDoorOpenCommand(garageDoor);
        remote.SetCommand(lightOn);
        remote.ButtonWasPressed();
        remote.SetCommand(garageOpen);
        remote.ButtonWasPressed();*/
    
        //多个插槽(含有关闭功能)的控制装置
        /*RemoteControl remoteControl = new RemoteControl();
        Light livingRoomLight = new Light("Living Room");
        Light kitchenLight = new Light("Kitchen");
        CeilLingFan ceilLingFan = new CeilLingFan("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);
        CeilLingFanLowCommand ceilingFanLow = new CeilLingFanLowCommand(ceilLingFan);
        CeilLingFanOffCommand ceilLingFanOff = new CeilLingFanOffCommand(ceilLingFan);
        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, ceilingFanLow, ceilLingFanOff);
        remoteControl.SetCommand(3, garageDoorOpen, garageDoorClose);
        remoteControl.SetCommand(4, stereoOnWithCd, stereoOff);
    
        Console.WriteLine(remoteControl);
    
        remoteControl.OnButtonWasPushed(0);
        remoteControl.OffButtonWasPushed(0);
        remoteControl.OnButtonWasPushed(1);
        remoteControl.OffButtonWasPushed(1);
        remoteControl.OnButtonWasPushed(2);
        remoteControl.OffButtonWasPushed(2);
        remoteControl.OnButtonWasPushed(3);
        remoteControl.OffButtonWasPushed(3);
        remoteControl.OnButtonWasPushed(4);
        remoteControl.OffButtonWasPushed(4);*/
    
        //多个插槽(含有关闭功能,含有撤销功能)的控制装置
        /*RemoteControl remoteControl = new RemoteControl();
        Light livingRoomLight = new Light("Living Room");
        LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight);
        LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight);
        remoteControl.SetCommand(0, livingRoomLightOn, livingRoomLightOff);
    
        remoteControl.OnButtonWasPushed(0);
        remoteControl.OffButtonWasPushed(0);
        Console.WriteLine(remoteControl);
        remoteControl.UndoButtonWasPushed();
        remoteControl.OffButtonWasPushed(0);
        remoteControl.OnButtonWasPushed(0);
        Console.WriteLine(remoteControl);
        remoteControl.UndoButtonWasPushed();*/
    
        //多个插槽(含有状态对象的撤销功能)的控制装置
        /*RemoteControl remoteControl = new RemoteControl();
        CeilLingFan ceilLingFan = new CeilLingFan("Living Room");
        CeilLingFanHighCommand ceilingFanHigh = new CeilLingFanHighCommand(ceilLingFan);
        CeilLingFanMediumCommand ceilLingFanMedium = new CeilLingFanMediumCommand(ceilLingFan);
        CeilLingFanOffCommand ceilLingFanOff = new CeilLingFanOffCommand(ceilLingFan);
    
        remoteControl.SetCommand(0, ceilLingFanMedium, ceilLingFanOff);
        remoteControl.SetCommand(1, ceilingFanHigh, ceilLingFanOff);
    
        remoteControl.OnButtonWasPushed(0);
        remoteControl.OffButtonWasPushed(0);
        Console.WriteLine(remoteControl);
        remoteControl.UndoButtonWasPushed();
    
        remoteControl.OnButtonWasPushed(1);
        Console.WriteLine(remoteControl);
        remoteControl.UndoButtonWasPushed();*/
    
        //多个插槽(含有宏命令)的控制装置
        RemoteControl remoteControl = new RemoteControl();
        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);
    
        ICommand[] partyOn = { lightOn, stereoOn, tvOn, hottubOn };
        ICommand[] 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);
    }
    

    View Code

    ---------------------------------以上内容听大人讲《head frist design mode》进行整理

    本文由新葡亰496net发布于奥门新萄京娱乐场,转载请注明出处:指令情势,完毕命令设计形式

    关键词:

上一篇:微软大法好,情怀与前景

下一篇:没有了