using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CommandPattern { /// /// 调用者角色 /// public class Invoker { /// /// 申明调用的命令,并用构造函数注入 /// private readonly Command command; public string InovkerName { get; set; } public Invoker(Command command) { this.command = command; } /// /// 调用以执行具体命令 /// public void Invoke() { Console.WriteLine(string.Format("『{0}』下达命令:{1}", this.InovkerName, this.command.CommandName)); this.command.Execute(); } } /// /// 命令者角色 /// public abstract class Command { protected readonly Receiver receiver; public string CommandName { get; set; } public Command(Receiver receiver) { this.receiver = receiver; } /// /// 抽象执行具体命令方法 /// 由之类实现 /// public abstract void Execute(); } /// /// 武力收复台湾命令 /// public class RecaptureTaiwanByFocusCommand : Command { string commandName = "武力收复台湾!"; /// /// 重写默认构造函数,指定默认接收者 /// 以降低高层模块对底层模块的依赖 /// public RecaptureTaiwanByFocusCommand() : base(new OperationCenter()) { base.CommandName = commandName; } /// /// 也可通过构造函数重新指定接收者 /// /// public RecaptureTaiwanByFocusCommand(Receiver receiver) : base(receiver) { base.CommandName = commandName; } public override void Execute() { this.receiver.Plan(); this.receiver.Action(); } } /// /// 和平方式收复台湾命令 /// public class RecaptureTaiwanByPeaceCommand : Command { string commandName = "和平收复台湾!"; /// /// 重写默认构造函数,指定默认接收者 /// 以降低高层模块对底层模块的依赖 /// public RecaptureTaiwanByPeaceCommand() : base(new NegotiationCenter()) { base.CommandName = commandName; } /// /// 也可通过构造函数重新指定接收者 /// /// public RecaptureTaiwanByPeaceCommand(Receiver receiver) : base(receiver) { base.CommandName = commandName; } public override void Execute() { this.receiver.Plan(); this.receiver.Action(); } } /// /// 接收者角色 /// public abstract class Receiver { protected string ReceiverName { get; set; } //定义每个执行者都必须处理的业务逻辑 public abstract void Plan(); public abstract void Action(); } /// /// 作战中心 /// public class OperationCenter : Receiver { public OperationCenter() { this.ReceiverName = "作战中心"; } public override void Plan() { Console.WriteLine(string.Format("{0}:制定作战计划。", this.ReceiverName)); } public override void Action() { Console.WriteLine(string.Format("{0}:海陆空按照即定作战计划作战,收复台湾!", this.ReceiverName)); } } /// /// 谈判中心 /// public class NegotiationCenter : Receiver { public NegotiationCenter() { this.ReceiverName = "谈判中心"; } public override void Plan() { Console.WriteLine(string.Format("{0}:制定谈判计划。", this.ReceiverName)); } public override void Action() { Console.WriteLine(string.Format("{0}:落实谈判结果,收复台湾!", this.ReceiverName)); } } }