1、概述
一般來講,「行爲請求者」與「行爲實現者」是緊耦合的。但在某些場合,好比要對行爲進行「記錄、撤銷/重作、事務」等處理,這種沒法抵禦變化的緊耦合是不合適的。在這些狀況下,將「行爲請求者」與「行爲實現者」解耦,實現兩者之間的鬆耦合就相當重要。命令模式是解決這類問題的一個比較好的方法。
2、命令模式
命令模式將一個請求封裝爲一個對象,從而使你可用不一樣的請求對客戶進行參數化;對請求排隊或記錄請求日誌,以及支持可撤銷的操做。
命令模式的結構圖以下
this
Command定義了命令的接口
ConcreteCommand實現Command接口,定義了具體的命令
Client用於建立具體的命令並設定接收者
Invoker要求Command執行相應的請求
Receiver實施與執行一個請求,任何一個類均可能做爲Receiver。
3、示例
假定要實現一個繪圖系統,要求支持撤銷功能,下面就用命令模式來實現這一需求。
首先定義一個抽象的命令接口spa
public interface IGraphCommand { void Draw(); void Undo(); }
接着實現具體的繪圖命令日誌
public class Line : IGraphCommand { private Point startPoint; private Point endPoint; public Line(Point start, Point end) { startPoint = start; endPoint = end; } public void Draw() { Console.WriteLine("Draw Line:{0} To {1}", startPoint.ToString(), endPoint.ToString()); } public void Undo() { Console.WriteLine("Erase Line:{0} To {1}", startPoint.ToString(), endPoint.ToString()); } } public class Rectangle : IGraphCommand { private Point topLeftPoint; private Point bottomRightPoint; public Rectangle(Point topLeft, Point bottomRight) { topLeftPoint = topLeft; bottomRightPoint = bottomRight; } public void Draw() { Console.WriteLine("Draw Rectangle: Top Left Point {0}, Bottom Right Point {1}", topLeftPoint.ToString(), bottomRightPoint.ToString()); } public void Undo() { Console.WriteLine("Erase Rectangle: Top Left Point {0}, Bottom Right Point {1}", topLeftPoint.ToString(), bottomRightPoint.ToString()); } } public class Circle : IGraphCommand { private Point centerPoint; private int radius; public Circle(Point center, int radius) { centerPoint = center; this.radius = radius; } public void Draw() { Console.WriteLine("Draw Circle: Center Point {0}, Radius {1}", centerPoint.ToString(), radius.ToString()); } publi cvoid Undo() { Console.WriteLine("Erase Circle: Center Point {0}, Radius {1}", centerPoint.ToString(), radius.ToString()); } }
而後再定義繪圖類做爲命令接收者code
public class Graphics { Stack<IGraphCommand> commands =new Stack<IGraphCommand>(); public void Draw(IGraphCommand command) { command.Draw(); commands.Push(command); } public void Undo() { IGraphCommand command = commands.Pop(); command.Undo(); } }
最後看一下如何調用對象
static void Main(string[] args) { Line line =new Line(new Point(10, 10), new Point(100, 10)); Rectangle rectangle =new Rectangle(new Point(20, 20), new Point(50, 30)); Circle circle =new Circle(new Point(500, 500), 200); Graphics graphics =new Graphics(); graphics.Draw(line); graphics.Draw(rectangle); graphics.Undo(); graphics.Draw(circle); Console.ReadLine(); }