概述

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

 

  •  
  • public class Document
  • {
  •     public void Display()
  •     {
  •         Console.WriteLine("Display");
  •     }
  •  
  •     public void Undo()
  •     {
  •         Console.WriteLine("Undo");
  •     }
  •  
  •     public void Redo()
  •     {
  •         Console.WriteLine("Redo");
  •     }
  • }
  •  
  • class Program
  • {
  •     static void Main(string[] args)
  •     {
  •         Document doc = new Document();
  •  
  •         doc.Display();
  •  
  •         doc.Undo();
  •  
  •         doc.Redo();
  •     }
  • }
  • 相关文章: