柚子Nan--回归原点

Everything can be as easy as you like or as complex as you need.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Command模式的C#简单实现

Posted on 2005-01-13 11:01  柚子Nan  阅读(2846)  评论(3)    收藏  举报
借鉴了IssueVision的方式,实现了Web下的Button按钮点击事件 ,使用了事件和代理的原理。

Command模式的基本原理的东西,在网上可以找到,这个可以说是一个简单的实现模型,具有扩展性。
public class Command
    
{
        
public delegate void EnableChangedEventHandler(object sender, Command.EnableChangedEventArgs e);
        
public virtual event EnableChangedEventHandler EnableChanged;
        
public delegate void Action();

        
public Command()
        
{
            
        }

        
private Action m_action;
        
private bool m_isEnabled = true;        
        
        
public bool IsEnabled
        
{
            
get
            
{
                
return m_isEnabled;
            }


            
set
            
{
                
if (m_isEnabled != value)
                
{
                    m_isEnabled 
= value;
                    
if (EnableChanged != null)
                    
{
                        EnableChanged(
thisnew EnableChangedEventArgs(IsEnabled));
                    }

                }

            }

        }


        
public Command(Action action)
        
{
            m_action 
= action;
        }


        
// Invokes the method assigned to this command.
        public void Execute()
        
{
            m_action();
        }

        
        
public class EnableChangedEventArgs : EventArgs
        
{
            
private bool m_isEnabled = false;

            
public bool IsEnabled
            
{
                
get
                
{
                    
return m_isEnabled;
                }

            }


            
public EnableChangedEventArgs(bool isEnabled)
            
{
                m_isEnabled 
= isEnabled;
            }

        }

    }

Commander类,扩展性就在这里

public abstract class Commander
    
{
        
protected Command aCommand;
        
protected abstract void HandleEnableChangedEvent(object sender, Command.EnableChangedEventArgs e);
        
protected Commander( Command inCommand)
        
{
            aCommand 
= inCommand;
            aCommand.EnableChanged 
+= new CommandPattern.Command.EnableChangedEventHandler(HandleEnableChangedEvent);
        }

    }


    
public class WebButtonCommander : Commander
    
{
        
private System.Web.UI.WebControls.Button aButton;

        
protected WebButtonCommander(System.Web.UI.WebControls.Button inButton,Command inComand) : base(inComand)
        
{
            aButton 
= inButton;
            aButton.Click 
+=new EventHandler(this.HandleUIEvent);
        }

        
protected override void HandleEnableChangedEvent(object sender, Command.EnableChangedEventArgs e)
        
{
            aButton.Enabled 
= e.IsEnabled;
        }


        
private void HandleUIEvent(object sender,EventArgs e)
        
{
            aCommand.Execute();
        }


        
public static void Connect(System.Web.UI.WebControls.Button inButton, Command inCommand)
        
{
            WebButtonCommander unused 
= new WebButtonCommander(inButton,inCommand);
        }

    }

然后,如何使用这些呢?建议一个Web项目,在页面上放2个按钮,清楚的知道是哪个按钮事件发生了。

protected System.Web.UI.WebControls.Button btnFirst;
        
protected System.Web.UI.WebControls.Button btnSecond;

        
Command Definition

/// <summary>
        
/// 初始化按钮事件
        
/// </summary>

        private void InitializeButton()
        
{
            
this.FirstCommand = new Command(new Command.Action(this.FirstAction));
            WebButtonCommander.Connect(
this.btnFirst,this.FirstCommand);

            
this.SecondCommand = new Command(new Command.Action(this.SecondAction));
            WebButtonCommander.Connect(
this.btnSecond,this.SecondCommand);
        }


override protected void OnInit(EventArgs e)
        
{
            
//
            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            
//
            InitializeComponent();
            InitializeButton();    // look at this
            
base.OnInit(e);
        }


Button click Action