Design Pattern – Command Pattern

概述

      在软件系统中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”。但在某些场合,比如要对行为进行“记录、撤销/重做、事务”等处理,这种无法抵御变化的紧耦合是不合适的。在这种情况下,如何将“行为请求者”与“行为实现者”解耦?将一组行为抽象为对象,可以实现二者之间的松耦合 -- 这就是本文要说的Command模式。

 

Code Snippers
  1.  
  2. public class Document
  3. {
  4.     public void Display()
  5.     {
  6.         Console.WriteLine("Display");
  7.     }
  8.  
  9.     public void Undo()
  10.     {
  11.         Console.WriteLine("Undo");
  12.     }
  13.  
  14.     public void Redo()
  15.     {
  16.         Console.WriteLine("Redo");
  17.     }
  18. }
  19.  
  20. class Program
  21. {
  22.     static void Main(string[] args)
  23.     {
  24.         Document doc = new Document();
  25.  
  26.         doc.Display();
  27.  
  28.         doc.Undo();
  29.  
  30.         doc.Redo();
  31.     }
  32. }

 

       在上例中,是未用command Pattern之前的代码,直接调用document中的三个方法。我们用command Pattern实现如上的功能,需要把每个命令抽象成具体的类,代码如下:

Code Snippers
  1.  
  2. /// <summary>
  3. /// Abstarct command class.
  4. /// </summary>
  5. public abstract class DocumentCommand
  6. {
  7.     protected Document _document;
  8.  
  9.     public DocumentCommand(Document doc)
  10.     {
  11.         this._document = doc;
  12.     }
  13.  
  14.     public abstract void Execute();
  15. }
  16.  
  17. public class DisplayCommand : DocumentCommand
  18. {
  19.     public DisplayCommand(Document doc): base(doc)
  20.     {
  21.     }
  22.  
  23.     public override void Execute()
  24.     {
  25.         _document.Display();
  26.     }
  27. }
  28.  
  29. public class UndoCommand : DocumentCommand
  30. {
  31.     public UndoCommand(Document doc): base(doc)
  32.     {
  33.     }
  34.  
  35.     public override void Execute()
  36.     {
  37.         _document.Undo();
  38.     }
  39. }
  40.  
  41. public class RedoCommand : DocumentCommand
  42. {
  43.     public RedoCommand(Document doc): base(doc)
  44.     {
  45.     }
  46.  
  47.     public override void Execute()
  48.     {
  49.         _document.Redo();
  50.     }
  51. }
  52.  
  53. public class DocumentInvoker
  54. {
  55.     DocumentCommand _discmd;
  56.     DocumentCommand _undcmd;
  57.     DocumentCommand _redcmd;
  58.  
  59.     public DocumentInvoker(DocumentCommand discmd, DocumentCommand undcmd, DocumentCommand redcmd)
  60.     {
  61.         this._discmd = discmd;
  62.         this._undcmd = undcmd;
  63.         this._redcmd = redcmd;
  64.     }
  65.  
  66.     public void Display()
  67.     {
  68.         _discmd.Execute();
  69.     }
  70.  
  71.     public void Undo()
  72.     {
  73.         _undcmd.Execute();
  74.     }
  75.  
  76.     public void Redo()
  77.     {
  78.         _redcmd.Execute();
  79.     }
  80. }
  81.  
  82. class Program
  83. {
  84.     static void Main(string[] args)
  85.     {
  86.         Document doc = new Document();
  87.  
  88.         DocumentCommand discmd = new DisplayCommand(doc);
  89.         DocumentCommand undcmd = new UndoCommand(doc);
  90.         DocumentCommand redcmd = new RedoCommand(doc);
  91.  
  92.         DocumentInvoker invoker = new DocumentInvoker(discmd, undcmd, redcmd);
  93.  
  94.         invoker.Display();
  95.         invoker.Undo();
  96.         invoker.Redo();
  97.     }
  98. }

 

      command Pattern根本目的在于将“行为请求者”与“行为实现者”解耦。

适用性

在下面的情况下应当考虑使用命令模式:

1.使用命令模式作为"CallBack"在面向对象系统中的替代。"CallBack"讲的便是先将一个函数登记上,然后在以后调用此函数。

2.需要在不同的时间指定请求、将请求排队。一个命令对象和原先的请求发出者可以有不同的生命期。换言之,原先的请求发出者可能已经不在了,而命令对象本身仍然是活动的。这时命令的接收者可以是在本地,也可以在网络的另外一个地址。命令对象可以在序列化之后传送到另外一台机器上去。

3.系统需要支持命令的撤消(undo)。命令对象可以把状态存储起来,等到客户端需要撤销命令所产生的效果时,可以调用undo()方法,把命令所产生的效果撤销掉。命令对象还可以提供redo()方法,以供客户端在需要时,再重新实施命令效果。

4.如果一个系统要将系统中所有的数据更新到日志里,以便在系统崩溃时,可以根据日志里读回所有的数据更新命令,重新调用Execute()方法一条一条执行这些命令,从而恢复系统在崩溃前所做的数据更新。

posted @ 2009-12-30 16:00  咋攒都五块  阅读(331)  评论(1编辑  收藏  举报