将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作。
名称 |
Command |
结构 |
![](/images/cnblogs_com/darkangel/Command.gif) |
意图 |
将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作。 |
适用性 |
- 抽象出待执行的动作以参数化某对象,你可用过程语言中的回调(c a l l b a c k )函数表达这种参数化机制。所谓回调函数是指函数先在某处注册,而它将在稍后某个需要的时候被调用。C o m m a n d 模式是回调机制的一个面向对象的替代品。
- 在不同的时刻指定、排列和执行请求。一个C o m m a n d 对象可以有一个与初始请求无关的生存期。如果一个请求的接收者可用一种与地址空间无关的方式表达,那么就可将负责该请求的命令对象传送给另一个不同的进程并在那儿实现该请求。
- 支持取消操作。C o m m a n d 的E x c u t e 操作可在实施操作前将状态存储起来,在取消操作时这个状态用来消除该操作的影响。C o m m a n d 接口必须添加一个U n e x e c u t e 操作,该操作取消上一次E x e c u t e 调用的效果。执行的命令被存储在一个历史列表中。可通过向后和向前遍历这一列表并分别调用U n e x e c u t e 和E x e c u t e 来实现重数不限的“取消”和“重做”。
- 支持修改日志,这样当系统崩溃时,这些修改可以被重做一遍。在C o m m a n d 接口中添加装载操作和存储操作,可以用来保持变动的一个一致的修改日志。从崩溃中恢复的过程包括从磁盘中重新读入记录下来的命令并用E x e c u t e 操作重新执行它们。
- 用构建在原语操作上的高层操作构造一个系统。这样一种结构在支持事务( t r a n s a c t i o n )的信息系统中很常见。一个事务封装了对数据的一组变动。C o m m a n d 模式提供了对事务进行建模的方法。C o m m a n d 有一个公共的接口,使得你可以用同一种方式调用所有的事务。同时使用该模式也易于添加新事务以扩展系统。
|
Code Example |
1 // Command 2![](/Images/OutliningIndicators/None.gif) 3 // Intent: "Encapsulate a request as an object, thereby letting you 4 // parameterize clients with different requests, queue or log 5 // requests, and support undoable operations". 6![](/Images/OutliningIndicators/None.gif) 7 // For further information, read "Design Patterns", p233, Gamma et al., 8 // Addison-Wesley, ISBN:0-201-63361-2 9![](/Images/OutliningIndicators/None.gif) 10![](/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//* Notes: 11 * Commands are at the heart of a modern GUI app. 12 * They must be nameable, undoable, recordable, 13 * configurable, executable and repeatable. 14 * 15 * In addition to the command here, a command type could also be useful. 16 * It could store the name of a command, information about its icon, etc. 17 */ 18 19 namespace Command_DesignPattern 20![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) { 21 using System; 22![](/Images/OutliningIndicators/InBlock.gif) 23 abstract class Command 24![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 25 abstract public void Execute(); 26 protected Receiver r; 27 public Receiver R 28![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 29 set 30![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 31 r = value; 32 } 33 } 34 } 35![](/Images/OutliningIndicators/InBlock.gif) 36 class ConcreteCommand : Command 37![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 38 override public void Execute() 39![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 40 Console.WriteLine("Command executed"); 41 r.InformAboutCommand(); 42 } 43 } 44![](/Images/OutliningIndicators/InBlock.gif) 45 class Receiver 46![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 47 public void InformAboutCommand() 48![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 49 Console.WriteLine("Receiver informed about command"); 50 } 51 52 } 53![](/Images/OutliningIndicators/InBlock.gif) 54 class Invoker 55![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 56 private Command command; 57 public void StoreCommand(Command c) 58![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 59 command = c; 60 } 61 public void ExecuteCommand() 62![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 63 command.Execute(); 64 } 65 } 66![](/Images/OutliningIndicators/InBlock.gif) 67![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary> 68 /// Summary description for Client. 69 /// </summary> 70 public class Client 71![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 72 public static int Main(string[] args) 73![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 74 // Set up everything 75 Command c = new ConcreteCommand(); 76 Receiver r = new Receiver(); 77 c.R = r; 78 Invoker i = new Invoker(); 79 i.StoreCommand(c); 80![](/Images/OutliningIndicators/InBlock.gif) 81 // now let application run 82![](/Images/OutliningIndicators/InBlock.gif) 83 // the invoker is how the command is exposed for the end-user 84 // (or a client) initiates the command, 85 // (e.g. toolbar button, menu item) 86![](/Images/OutliningIndicators/InBlock.gif) 87 i.ExecuteCommand(); 88![](/Images/OutliningIndicators/InBlock.gif) 89 return 0; 90 } 91 } 92 } 93![](/Images/OutliningIndicators/None.gif) 94![](/Images/OutliningIndicators/None.gif) |