浅析issuevision中的command模式

浅析issuevision中的command模式

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

在issuevision中有个command设计模式,就像一个指挥官,一声令下,他的士兵们都得听他的命令。

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /> Img-0000.jpg

issuevision中,

Commander又分为ToolbarButtonCommanderMenuItemCommander,即有两个指挥官,一个管toolbar,一个管menu

public class Command

{

        // The EnableChanged event is raised when the IsEnabled value of the command

        // changes. This is handled in the Commander objects to set the Enabled property

        // of the managed controls.命令类,指挥官要下达的命令.这里用代表的方式来解决

        public delegate void EnableChangedEventHandler(object sender, Command.EnableChangedEventArgs e);

        public virtual event EnableChangedEventHandler EnableChanged;

        public delegate void Action();

       

        private Action m_action;

        private bool m_isEnabled = true;

       

        // The IsEnabled property is used to set/retrieve the enabled state of all UI

        // controls that the command manages 是否可用

        public bool IsEnabled

        {

            get

            {

                return m_isEnabled;

            }

 

            set

            {

                if (m_isEnabled != value)

                {

                    m_isEnabled = value;

                    if (EnableChanged != null) //设置是否可用时,触发该事件                    {

                        EnableChanged(this, new EnableChangedEventArgs(IsEnabled));

                    }

                }

            }

        }

 

        public Command(Action action)

        {

            m_action = action;

        }

 

        // Invokes the method assigned to this command.执行命令

        public void Execute()

        {

            m_action();

        }

       

        // Arguments passed to the EnableChanged event.

        public class EnableChangedEventArgs : EventArgs

        {

            private bool m_isEnabled = false;

 

            public bool IsEnabled

            {

                get

                {

                    return m_isEnabled;

                }

            }

 

            public EnableChangedEventArgs(bool isEnabled)

            {

                m_isEnabled = isEnabled;

            }

        }

}

 

public abstract class Commander //总指挥官

{

        protected Command m_command;

        protected abstract void HandleEnableChangedEvent(object sender, Command.EnableChangedEventArgs e);

 

        protected Commander(Command command)

        {

            m_command = command;

            m_command.EnableChanged += new Command.EnableChangedEventHandler(this.HandleEnableChangedEvent);

        }

}

   

    // MenuItemCommander类 下属指挥官

    public class MenuItemCommander : Commander

    {

        private MenuItem m_item;

 

        protected MenuItemCommander(MenuItem item, Command command) : base(command)

        {

            m_item = item;

            m_item.Click += new EventHandler(this.HandleUIEvent);

        }

 

        protected override void HandleEnableChangedEvent(object sender, Command.EnableChangedEventArgs e)

        {

            m_item.Enabled = e.IsEnabled;

        }

 

        private void HandleUIEvent(object sender, EventArgs e)

        {

            m_command.Execute();

        }

 

        // Connect is a shared (static) method that performs the task of adapting a menu

        // item to a command.  The commander exists only to wire up the two objects --

        // it is not used further

        public static void Connect(MenuItem item, Command command) //把士兵管理起来

        {

            MenuItemCommander unused = new MenuItemCommander(item, command);

        }

    }

   

    public class ToolBarButtonCommander : Commander //下属指挥官

    {

        private ToolBarButton m_button;

 

        protected ToolBarButtonCommander(ToolBarButton button, Command command) : base(command)

        {

            m_button = button;

            button.Parent.ButtonClick += new ToolBarButtonClickEventHandler(this.HandleUIEvent);

        }

 

        protected override void HandleEnableChangedEvent(object sender, Command.EnableChangedEventArgs e)

        {

            m_button.Enabled = e.IsEnabled;

        }

 

        private void HandleUIEvent(object sender, ToolBarButtonClickEventArgs e)

        {

            if (m_button == e.Button)

            {

                m_command.Execute();

            }

        }

 

        // Connect is a shared (static) method that performs the task of adapting a toolbar

        // button to a command.  The commander exists only to wire up the two objects --

        // it is not used further

        public static void Connect(ToolBarButton button, Command command)

        {

            ToolBarButtonCommander unused = new ToolBarButtonCommander(button, command);

        }

    }

 

然后指挥官又下达一系列的命令:

private void InitializeCommands()

        {

            // Wire up menus and toolbar buttons

            //Command pattern: File | Send & Receive

            m_sendReceiveCommand = new Command(new Command.Action(this.SendReceive_Action));

            //菜单项需要执行这个命令

            MenuItemCommander.Connect(menuSendReceive, m_sendReceiveCommand);

            //工具栏项需要执行这个命令

            ToolBarButtonCommander.Connect(tlbSendReceive, m_sendReceiveCommand);          

        }  

例如这个命令就是让tlbSendRecieve这个士兵去执行m_sendReceiveCommand(发送/接收)的命令,这里命令是由ToolBarButtonCommander下达的

 

如果我们不用command模式,那么可能就应该这样写:

当菜单项点击时调用SendReceive_Action方法,当工具栏点击时调用SendReceive_Action方法

如果我们系统所有的工具栏改用双击事件了,那么我们再调用SendReceive_Action这个方法时,我们又要修改整个系统了,很麻烦。

而用命令模式,就能很好的解决这个问题,我们只需要改ToolbarButtonCommander中的一个方法就可以了。

只要总的指挥官可以对他下属下达命令,具体的执行就看他的下属怎么做了。这里toolbarButtonCommanderMenuItemCommander就是Commander这个总指挥官的下属。

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值