读书笔记18:命令模式

来源:互联网 发布:印度南部 知乎 编辑:程序博客网 时间:2024/05/22 15:33

1、定义:
将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作。[GOF 《设计模式》]
2、角色
Command——用来执行操作的接口。
CreateCommand——将一个接收者对象绑定一个动作,并实现执行命令操作。
Invoker——要求该命令执行。
Receiver——知道如何实施与执行一个与请求相关的操作,任何类都可作为一个接收者。
3、模型
Command

[csharp] view plaincopyprint?
  1. abstract class Command  
  2.   {  
  3.       protected Receiver receiver;  
  4.       public Command(Receiver creceiver)  
  5.       {  
  6.           this.receiver = creceiver;  
  7.       }  
  8.   
  9.       abstract public void Execute();  
  10.   }  

CreateCommand

[csharp] view plaincopyprint?
  1. class CreateCommand : Command  
  2.     {  
  3.         public CreateCommand(Receiver receiver)  
  4.   
  5.             : base(receiver)  
  6.         {  
  7.   
  8.         }  
  9.   
  10.         public override void Execute()  
  11.         {  
  12.             receiver.Action();  
  13.         }  
  14.     }  

Receiver

[csharp] view plaincopyprint?
  1. class Receiver  
  2.   {  
  3.       public void Action()  
  4.       {  
  5.           Console.WriteLine("执行命令");  
  6.       }  
  7.   }   

Invoker

[csharp] view plaincopyprint?
  1. class Invoker  
  2. {  
  3.     private Command command;  
  4.   
  5.     public void SetCommand(Command command)  
  6.     {  
  7.         this.command = command;  
  8.     }  
  9.   
  10.     public void ExecuteCommand()  
  11.     {  
  12.         command.Execute();  
  13.     }  
  14. }  

调用

[csharp] view plaincopyprint?
  1. static void Main(string[] args)  
  2. {  
  3.     Receiver receiver = new Receiver();  
  4.   
  5.     Command command = new CreateCommand(receiver);  
  6.   
  7.     Invoker invoker = new Invoker();  
  8.   
  9.     invoker.SetCommand(command);  
  10.   
  11.     invoker.ExecuteCommand();  
  12.   
  13.     Console.ReadLine();  
  14. }  

结果:

命令模式模型代码:http://download.csdn.net/detail/yysyangyangyangshan/4089536

命令模式,很多个Receiver,并与之对应Command也有很多个,Invoker则负责将Receiver和Command关联,并执行。

应用模型:

[csharp] view plaincopyprint?
  1. abstract class Command1  
  2. {  
  3.     protected Receiver1 receiver1;  
  4.   
  5.     public Command1(Receiver1 creceiver1)  
  6.     {  
  7.         this.receiver1 = creceiver1;  
  8.     }  
  9.   
  10.     abstract public void Execute();  
  11. }  
  12.   
  13. abstract class Command2  
  14. {  
  15.     protected Receiver2 receiver2;  
  16.   
  17.     public Command2(Receiver2 creceiver2)  
  18.     {  
  19.         this.receiver2 = creceiver2;  
  20.     }  
  21.   
  22.     abstract public void Execute();  
  23. }  
  24.     class CreateCommand1 : Command1  
  25. {  
  26.     public CreateCommand1(Receiver1 receiver1)  
  27.   
  28.         : base(receiver1)  
  29.     {  
  30.   
  31.     }  
  32.   
  33.     public override void Execute()  
  34.     {  
  35.         receiver1.Action();  
  36.     }  
  37. }  
  38.     class CreateCommand2 : Command2  
  39. {  
  40.     public CreateCommand2(Receiver2 receiver2)  
  41.   
  42.         : base(receiver2)  
  43.     {  
  44.   
  45.     }  
  46.   
  47.     public override void Execute()  
  48.     {  
  49.         receiver2.Action();  
  50.     }  
  51. }  
  52.     class Invoker  
  53. {  
  54.     private Command1 command1;  
  55.   
  56.     private Command2 command2;  
  57.   
  58.     public void SetCommand1(Command1 command)  
  59.     {  
  60.         this.command1 = command;  
  61.     }  
  62.   
  63.     public void ExecuteCommand1()  
  64.     {  
  65.         command1.Execute();  
  66.     }  
  67.   
  68.     public void SetCommand2(Command2 command)  
  69.     {  
  70.         this.command2 = command;  
  71.     }  
  72.   
  73.     public void ExecuteCommand2()  
  74.     {  
  75.         command2.Execute();  
  76.     }  
  77. }  
  78.     class Receiver1  
  79. {  
  80.     public void Action()  
  81.     {  
  82.         Console.WriteLine("执行命令一");  
  83.     }  
  84. }  
  85.     class Receiver2  
  86. {  
  87.     public void Action()  
  88.     {  
  89.         Console.WriteLine("执行命令二");  
  90.     }  
  91. }  

调用

[csharp] view plaincopyprint?
  1. static void Main(string[] args)  
  2. {  
  3.     Receiver1 receiver1 = new Receiver1();  
  4.   
  5.     Receiver2 receiver2 = new Receiver2();  
  6.   
  7.     Command1 command1 = new CreateCommand1(receiver1);  
  8.   
  9.     Command2 command2 = new CreateCommand2(receiver2);  
  10.   
  11.     Invoker invoker = new Invoker();  
  12.   
  13.     invoker.SetCommand1(command1);  
  14.   
  15.     invoker.ExecuteCommand1();  
  16.   
  17.     invoker.SetCommand2(command2);  
  18.   
  19.     invoker.ExecuteCommand2();  
  20.   
  21.     Console.ReadLine();  
  22. }  

结果:

   
应用代码:http://download.csdn.net/detail/yysyangyangyangshan/4089547

0 0
原创粉丝点击