命令设计模式
//只有在需要撤销或恢复功能时,写为命令模式才用意义。
namespace ConsoleApplication1
{
//摘要:
//当一个请求被封装为对象时,从而使得可以用不同的请求对客户端进行参数化;对请求排队或记录请求日志,以及支持撤销的操作。
public abstract class CommandPattern
{
protected Hashtable hash;
protected Receiver receiver;
public CommandPattern(Receiver r)
{
receiver = r;
}
public abstract void Excute();
public abstract void RollBack();
public abstract void SortCommand();
}
public class ConcreteCommand : CommandPattern
{
public ConcreteCommand(Receiver r) : base(r) { }
public override void Excute()
{
receiver.Action();
}
public override void RollBack()
{
throw new NotImplementedException();
}
public override void SortCommand()
{
throw new NotImplementedException();
}
}
//摘要:
//命令的执行者
public class Receiver
{
public void Action()
{
Console.WriteLine("I am excuting an command!");
}
}
//摘要:
//发出命令的对象
public class Invoker
{
private CommandPattern command;
public void SetCommand(CommandPattern c)
{
this.command = c;
}
public void EecudeCommand()
{
this.command.Excute();
}
}
public class Client
{
public void Test()
{
Receiver r = new Receiver();
CommandPattern c = new ConcreteCommand(r);
Invoker q = new Invoker();
q.SetCommand(c);
q.EecudeCommand();
}
}
}