C# 命令模式(Command)
理解:把每个命令封装成一个类,当发令者要发布多个命令时,可以一个个排队地发布给接收者,同时记录下每个命令的详细信息,发布时间等。还可以撤销已发布的命令。
代码:
using System.Collections.Generic;
using System.Windows.Forms;
namespace DesignMode.Command
{
//被命令者
public class QQ
{
public void Response_SendMessage()
{
MessageBox.Show("消息发送中。。。");
}
public void Response_PrintScreen()
{
MessageBox.Show("截图中。。。");
}
public void Response_Remote()
{
MessageBox.Show("远程中。。。");
}
}
//抽象命令类
public abstract class AbstractCommand
{
protected QQ _qq;
public AbstractCommand(QQ qq)
{
this._qq = qq;
}
public abstract void Execute();
}
//发送消息命令类
public class SendMessageCommand : AbstractCommand
{
public SendMessageCommand(QQ qq):base(qq)
{ }
public override void Execute()
{
base._qq.Response_SendMessage();
}
}
//截图命令类
public class PrintScreenCommand : AbstractCommand
{
public PrintScreenCommand(QQ qq):base(qq)
{ }
public override void Execute()
{
base._qq.Response_PrintScreen();
}
}
//远程命令类
public class RemoteCommand : AbstractCommand
{
public RemoteCommand(QQ qq) : base(qq)
{ }
public override void Execute()
{
base._qq.Response_Remote();
}
}
//发号命令者
public class Me
{
IList<AbstractCommand> cmdList = new List<AbstractCommand>();
//发布命令
public void IssueCommand(AbstractCommand command)
{
cmdList.Add(command);
}
//取消命令
public void CancelCommond(AbstractCommand command)
{
cmdList.Remove(command);
}
//执行命令
public void ExecuteCommand()
{
foreach (AbstractCommand c in cmdList)
{
c.Execute();
}
}
}
}
using System.Windows.Forms;
namespace DesignMode.Command
{
//被命令者
public class QQ
{
public void Response_SendMessage()
{
MessageBox.Show("消息发送中。。。");
}
public void Response_PrintScreen()
{
MessageBox.Show("截图中。。。");
}
public void Response_Remote()
{
MessageBox.Show("远程中。。。");
}
}
//抽象命令类
public abstract class AbstractCommand
{
protected QQ _qq;
public AbstractCommand(QQ qq)
{
this._qq = qq;
}
public abstract void Execute();
}
//发送消息命令类
public class SendMessageCommand : AbstractCommand
{
public SendMessageCommand(QQ qq):base(qq)
{ }
public override void Execute()
{
base._qq.Response_SendMessage();
}
}
//截图命令类
public class PrintScreenCommand : AbstractCommand
{
public PrintScreenCommand(QQ qq):base(qq)
{ }
public override void Execute()
{
base._qq.Response_PrintScreen();
}
}
//远程命令类
public class RemoteCommand : AbstractCommand
{
public RemoteCommand(QQ qq) : base(qq)
{ }
public override void Execute()
{
base._qq.Response_Remote();
}
}
//发号命令者
public class Me
{
IList<AbstractCommand> cmdList = new List<AbstractCommand>();
//发布命令
public void IssueCommand(AbstractCommand command)
{
cmdList.Add(command);
}
//取消命令
public void CancelCommond(AbstractCommand command)
{
cmdList.Remove(command);
}
//执行命令
public void ExecuteCommand()
{
foreach (AbstractCommand c in cmdList)
{
c.Execute();
}
}
}
}
客户端代码:
private void btn_Command_Click(object sender, EventArgs e)
{
//定义命令承受者
QQ qq = new QQ();
//定义具体命令
SendMessageCommand msg = new SendMessageCommand(qq);
PrintScreenCommand prt = new PrintScreenCommand(qq);
RemoteCommand remote = new RemoteCommand(qq);
//定义命令发布者
Me me = new Me();
//发布命令
me.IssueCommand(msg);
me.IssueCommand(prt);
me.IssueCommand(remote);
//取消截图命令
me.CancelCommond(prt);
//执行命令
me.ExecuteCommand();
}
{
//定义命令承受者
QQ qq = new QQ();
//定义具体命令
SendMessageCommand msg = new SendMessageCommand(qq);
PrintScreenCommand prt = new PrintScreenCommand(qq);
RemoteCommand remote = new RemoteCommand(qq);
//定义命令发布者
Me me = new Me();
//发布命令
me.IssueCommand(msg);
me.IssueCommand(prt);
me.IssueCommand(remote);
//取消截图命令
me.CancelCommond(prt);
//执行命令
me.ExecuteCommand();
}