C#设计模式之命令模式
将事件处理抽象成一个接口Icommander,其接口公布一个方法---void execute();有了这样一个接口,从Icommander继承的实现类就可以拥有自己独特的处理代码,而与UI无关。
对于每一个控件应该有一种方法,该方法可以指定这个控件与哪一个命令
关联。那我们可以这样处理,抽象一个接口Icommanderholder,其接口公布2个方法,void setcommander(Icommander cmd),Icommander getcommander(); 让控件也要实现该接口的方法。这种开发的思想就是------命令模式。有命令来驱动!
private void commandclick(object sender,EventArgs e)
{
//有了这样的设计方法,不需要在写很多类似的代码段了
Icommander cmd=((Icommanderholder)sender).getcommander();
cmd.execute();
}
private void init()
{
EventHandler evh=new EventHandler(commandclick);
btnred.Click+=evh;
btnred.setcommander(new redcommand(this));//设定处理命令
btnblue.Click+=evh;
btnblue.setcommander(new bluecommand(this));//设定处理命令
}
/*---------------------------------------------------------------------------------------------*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WFM
{
/// <summary>
/// frmMain 的摘要说明。
/// </summary>
public class frmMain : System.Windows.Forms.Form
{
private WFM.commanderbutton btnblue;
private WFM.commanderbutton btnred;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public frmMain()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
init();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnblue = new WFM.commanderbutton();
this.btnred = new WFM.commanderbutton();
this.SuspendLayout();
//
// btnblue
//
this.btnblue.BackColor = System.Drawing.Color.Lime;
this.btnblue.Location = new System.Drawing.Point(72, 64);
this.btnblue.Name = "btnblue";
this.btnblue.Size = new System.Drawing.Size(80, 32);
this.btnblue.TabIndex = 0;
this.btnblue.Text = "bluebtn";
//
// btnred
//
this.btnred.BackColor = System.Drawing.Color.Red;
this.btnred.Location = new System.Drawing.Point(216, 64);
this.btnred.Name = "btnred";
this.btnred.Size = new System.Drawing.Size(72, 32);
this.btnred.TabIndex = 1;
this.btnred.Text = "redbtn";
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(384, 237);
this.Controls.Add(this.btnred);
this.Controls.Add(this.btnblue);
this.Name = "frmMain";
this.Text = "天气晴朗,星星都出来了!";
this.ResumeLayout(false);
}
private void commandclick(object sender,EventArgs e)
{
Icommander cmd=((Icommanderholder)sender).getcommander();
cmd.execute();
}
private void init()
{
EventHandler evh=new EventHandler(commandclick);
btnred.Click+=evh;
btnred.setcommander(new redcommand(this));
btnblue.Click+=evh;
btnblue.setcommander(new bluecommand(this));
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void
{
Application.Run(new frmMain());
}
}
public interface Icommander
{
void execute();
}
public class redcommand:Icommander
{
#region commander 成员
public redcommand(Form win)
{
m_frm=win;
}
public void execute()
{
// TODO: 添加 redcommand.execute 实现
m_frm.BackColor=System.Drawing.Color.Red;
}
private Form m_frm;
#endregion
}
public class bluecommand:Icommander
{
#region commander 成员
public bluecommand(Form win)
{
m_frm=win;
}
public void execute()
{
// TODO: 添加 redcommand.execute 实现
m_frm.BackColor=System.Drawing.Color.Lime;
}
private Form m_frm;
#endregion
}
public interface Icommanderholder
{
void setcommander(commander cmd);
Icommander getcommander();
}
public
class commanderbutton:System.Windows.Forms.Button,Icommanderholder
{
#region commanderholder 成员
private Icommander m_cmd;
public void setcommander(Icommander cmd)
{
// TODO: 添加 buttoncommander.setcommander 实现
m_cmd=cmd;
}
public Icommander getcommander()
{
// TODO: 添加 buttoncommander.getcommander 实现
return m_cmd;
}
#endregion
}
}