Command模式

来源:互联网 发布:鼠疫加缪知乎 编辑:程序博客网 时间:2024/05/14 00:50

       将来自客户端的请求传入一个对象,无需了解这个请求激活的 动作或有关接受这个请求的处理细节。
这是一种两台机器之间通讯联系性质的模式,类似传统过程语 言的 CallBack功能。 解耦了发送者和接受者之间联系。 发送者调用一个操作,接受者接受请求执行相应的动作,因为使用Command模式解耦,发送者无需知道接受者任何接口。

 

namespace DesignPatternConsoleApp{    public class Receiver    {        public void Action()        {            Console.WriteLine("Execute Action");        }    }    public abstract class Command    {        protected Receiver receiver;        public Command(Receiver receiver)        {            this.receiver = receiver;        }        public abstract void Execute();    }    public class ConcreteCommand : Command    {        public ConcreteCommand(Receiver receiver) : base(receiver) { }        public override void Execute()        {            receiver.Action();        }    }    public class Invoker    {        private Command command;        public void SetCommand(Command command)        {            this.command = command;        }        public void ExecuteCommand()        {            this.command.Execute();        }    }}



 

原创粉丝点击