Invoker.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace CommandPattern
  7. {
  8. /// <summary>
  9. /// 调用者角色
  10. /// </summary>
  11. public class Invoker
  12. {
  13. /// <summary>
  14. /// 申明调用的命令,并用构造函数注入
  15. /// </summary>
  16. private readonly Command command;
  17. public string InovkerName { get; set; }
  18. public Invoker(Command command)
  19. {
  20. this.command = command;
  21. }
  22. /// <summary>
  23. /// 调用以执行具体命令
  24. /// </summary>
  25. public void Invoke()
  26. {
  27. Console.WriteLine(string.Format("『{0}』下达命令:{1}", this.InovkerName, this.command.CommandName));
  28. this.command.Execute();
  29. }
  30. }
  31. /// <summary>
  32. /// 命令者角色
  33. /// </summary>
  34. public abstract class Command
  35. {
  36. protected readonly Receiver receiver;
  37. public string CommandName { get; set; }
  38. public Command(Receiver receiver)
  39. {
  40. this.receiver = receiver;
  41. }
  42. /// <summary>
  43. /// 抽象执行具体命令方法
  44. /// 由之类实现
  45. /// </summary>
  46. public abstract void Execute();
  47. }
  48. /// <summary>
  49. /// 武力收复台湾命令
  50. /// </summary>
  51. public class RecaptureTaiwanByFocusCommand : Command
  52. {
  53. string commandName = "武力收复台湾!";
  54. /// <summary>
  55. /// 重写默认构造函数,指定默认接收者
  56. /// 以降低高层模块对底层模块的依赖
  57. /// </summary>
  58. public RecaptureTaiwanByFocusCommand() :
  59. base(new OperationCenter())
  60. {
  61. base.CommandName = commandName;
  62. }
  63. /// <summary>
  64. /// 也可通过构造函数重新指定接收者
  65. /// </summary>
  66. /// <param name="receiver"></param>
  67. public RecaptureTaiwanByFocusCommand(Receiver receiver)
  68. : base(receiver)
  69. {
  70. base.CommandName = commandName;
  71. }
  72. public override void Execute()
  73. {
  74. this.receiver.Plan();
  75. this.receiver.Action();
  76. }
  77. }
  78. /// <summary>
  79. /// 和平方式收复台湾命令
  80. /// </summary>
  81. public class RecaptureTaiwanByPeaceCommand : Command
  82. {
  83. string commandName = "和平收复台湾!";
  84. /// <summary>
  85. /// 重写默认构造函数,指定默认接收者
  86. /// 以降低高层模块对底层模块的依赖
  87. /// </summary>
  88. public RecaptureTaiwanByPeaceCommand() :
  89. base(new NegotiationCenter())
  90. {
  91. base.CommandName = commandName;
  92. }
  93. /// <summary>
  94. /// 也可通过构造函数重新指定接收者
  95. /// </summary>
  96. /// <param name="receiver"></param>
  97. public RecaptureTaiwanByPeaceCommand(Receiver receiver)
  98. : base(receiver)
  99. {
  100. base.CommandName = commandName;
  101. }
  102. public override void Execute()
  103. {
  104. this.receiver.Plan();
  105. this.receiver.Action();
  106. }
  107. }
  108. /// <summary>
  109. /// 接收者角色
  110. /// </summary>
  111. public abstract class Receiver
  112. {
  113. protected string ReceiverName { get; set; }
  114. //定义每个执行者都必须处理的业务逻辑
  115. public abstract void Plan();
  116. public abstract void Action();
  117. }
  118. /// <summary>
  119. /// 作战中心
  120. /// </summary>
  121. public class OperationCenter : Receiver
  122. {
  123. public OperationCenter()
  124. {
  125. this.ReceiverName = "作战中心";
  126. }
  127. public override void Plan()
  128. {
  129. Console.WriteLine(string.Format("{0}:制定作战计划。", this.ReceiverName));
  130. }
  131. public override void Action()
  132. {
  133. Console.WriteLine(string.Format("{0}:海陆空按照即定作战计划作战,收复台湾!", this.ReceiverName));
  134. }
  135. }
  136. /// <summary>
  137. /// 谈判中心
  138. /// </summary>
  139. public class NegotiationCenter : Receiver
  140. {
  141. public NegotiationCenter()
  142. {
  143. this.ReceiverName = "谈判中心";
  144. }
  145. public override void Plan()
  146. {
  147. Console.WriteLine(string.Format("{0}:制定谈判计划。", this.ReceiverName));
  148. }
  149. public override void Action()
  150. {
  151. Console.WriteLine(string.Format("{0}:落实谈判结果,收复台湾!", this.ReceiverName));
  152. }
  153. }
  154. }