命令模式的作用是將一個請求封裝為一個對象,從而使你可用不同的請求對客戶進行參數化;對請求排隊或記錄請求日志,以及支持可撤銷的操作。
由于“行為請求者”與“行為實現者”的緊耦合,使用命令模式,可以對請求排隊或記錄請求日志,以及支持可撤銷的操作。
命令模式把請求一個操作的對象與知道怎么執行一個操作的對象分割開。
Command模式關鍵就是講一個請求封裝到一個類中(Command),再提供處理對象(Receiver),最后Command命令由Invoker激活。另外,我們可以將請求接收者的處理抽象出來作為參數傳給Command對象,實際也就是回調的機制來實現這一點。也就是講處理操作方法地址通過參數傳遞給Command對象,Command對象在適當的時候再調用該函數。
Command模式將調用操作的對象和知道如何實現該操作的對象解耦,在上面Command的結構圖中,Invoker對象根本就不知道具體的是哪個對象在處理Execute操作(當然要知道是Command類別的對象)。
在Command要增加新的處理操作對象很容易,我們可以通過創建新的繼承自Command的子類來實現這一點。
Command模式可以和Memento模式結合起來,支持取消的操作。
結構圖:
Command模式通過將請求封裝到一個對象Command中,并將請求的接收者存放到具體的ConcreteCommand類中,從而實現調用操作的對象和操作的具體實現者之間的解耦。
Command模式結構圖中,將請求的接收者(處理者)放到Command的具體子類ConcreteCommand中,當請求到來時(Invoker發出Invoke消息激活Command對象),ConcreteCommand將處理請求交給Receiver對象進行處理。
例子:
namespace Bridge_DesignPattern{ using System; class Abstraction { protected Implementation impToUse; public void SetImplementation(Implementation i) { impToUse = i; } virtual public void DumpString(string str) { impToUse.DoStringOp(str); } } class DerivedAbstraction_One : Abstraction { override public void DumpString(string str) { str += ".com"; impToUse.DoStringOp(str); } } class Implementation { public virtual void DoStringOp(string str) { Console.WriteLine("Standard implementation - print string as is"); Console.WriteLine("string = {0}", str); } } class DerivedImplementation_One : Implementation { override public void DoStringOp(string str) { Console.WriteLine("DerivedImplementation_One - don't print string"); } } class DerivedImplementation_Two : Implementation { override public void DoStringOp(string str) { Console.WriteLine("DerivedImplementation_Two - print string twice"); Console.WriteLine("string = {0}", str); Console.WriteLine("string = {0}", str); } } /// <summary> /// Summary description for Client. /// </summary> public class Client { Abstraction SetupMyParticularAbstraction() { // we localize to this method the decision which abstraction and // which implementation to use. These need to be decided // somewhere and we do it here. All teh rest of the client // code can work against the abstraction object. Abstraction a = new DerivedAbstraction_One(); a.SetImplementation(new DerivedImplementation_Two()); return a; } public static int Main(string[] args) { Client c = new Client(); Abstraction a = c.SetupMyParticularAbstraction(); // From here on client code thinks it is talking to the // abstraction, and will not need to be changed as // derived abstractions are changed. // more client code using the abstraction goes here // . . . a.DumpString("Clipcode"); return 0; } }}
命令模式的優點:
1,它能較容易地設計一個命令隊列;
2,在需要的情況下,可以較容易地將命令記入日志;
3,允許接收請求的一方決定是否要否決請求。
4,可以容易地實現對請求的撤銷和重做;
5,由于加進新的具體命令類不影響其他的類,因此增加新的具體命令類很容易。
適用場景:
新聞熱點
疑難解答
圖片精選