這篇是《大話設計模式》命令模式的讀書筆記:用路邊攤點烤肉串的例子,從「顧客直接對老闆喊」的緊耦合寫法開始,一步步改成透過服務生和命令物件下單的鬆耦合設計,最後再讓服務生能記錄訂單、取消訂單、婉拒賣完的品項。
第一版:緊耦合 路邊攤只有老闆一個人,顧客直接對老闆喊要什麼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class Barbecuer { public void BakeMutton () => Console.WriteLine("烤羊肉串" ); public void BakeChickenWing () => Console.WriteLine("烤雞翅" ); } Barbecuer boy = new Barbecuer(); boy.BakeMutton(); boy.BakeMutton(); boy.BakeMutton(); boy.BakeChickenWing(); boy.BakeMutton(); boy.BakeMutton(); boy.BakeChickenWing();
行為的請求者(顧客)和行為的實現者(老闆)緊密耦合 。客人少的時候沒問題,客人一多就亂了:誰點了什麼、誰先誰後、誰已經付錢,全都記不清楚,也無法取消或修改。
第二版:用命令物件解耦 開一家店,請一位服務生。顧客點餐時,服務生把每個要求寫成一張訂單(命令物件) ,再交給烤肉師傅:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 public abstract class Command { protected Barbecuer receiver; public Command (Barbecuer receiver ) { this .receiver = receiver; } public abstract void ExecuteCommand () ; } class BakeMuttonCommand : Command { public BakeMuttonCommand (Barbecuer receiver ) : base (receiver ) { } public override void ExecuteCommand () => receiver.BakeMutton(); } class BakeChickenWingCommand : Command { public BakeChickenWingCommand (Barbecuer receiver ) : base (receiver ) { } public override void ExecuteCommand () => receiver.BakeChickenWing(); } public class Waiter { private Command command; public void SetOrder (Command command ) { this .command = command; } public void Notify () { command.ExecuteCommand(); } }
顧客不用認識烤肉師傅,只要把訂單交給服務生。
但這一版還不夠實用:
服務生一次只能收一張 訂單,顧客點一份就要通知一次 沒有記錄 點了多少,無法收費和統計 無法取消 訂單 賣完的品項無法事先婉拒 第三版:服務生記錄所有訂單 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 public class Waiter { private readonly IList<Command> orders = new List<Command>(); public void SetOrder (Command command ) { if (command is BakeChickenWingCommand) { Console.WriteLine("服務生:雞翅沒有了,請點別的燒烤。" ); } else { orders.Add(command); Console.WriteLine("增加訂單:" + command.GetType().Name + " 時間:" + DateTime.Now); } } public void CancelOrder (Command command ) { orders.Remove(command); Console.WriteLine("取消訂單:" + command.GetType().Name + " 時間:" + DateTime.Now); } public void Notify () { foreach (Command cmd in orders) { cmd.ExecuteCommand(); } } } Barbecuer boy = new Barbecuer(); Command bakeMutton1 = new BakeMuttonCommand(boy); Command bakeMutton2 = new BakeMuttonCommand(boy); Command bakeChickenWing1 = new BakeChickenWingCommand(boy); Waiter girl = new Waiter(); girl.SetOrder(bakeMutton1); girl.SetOrder(bakeMutton2); girl.SetOrder(bakeChickenWing1); girl.Notify();
原本筆記用 command.ToString() == "命令模式.BakeChickenWingCommand" 判斷品項,字串一改名就失效,改成 command is BakeChickenWingCommand 型別判斷。也順便把 ExcuteCommand 拼錯的地方改成 ExecuteCommand,並修正程式碼中的全形引號。
定義與角色 命令模式:將一個請求封裝為一個物件 ,從而讓你可以用不同的請求對用戶端進行參數化,對請求排隊或記錄請求日誌,以及支援可復原的操作。
角色 範例 Command(命令) 訂單 Command ConcreteCommand(具體命令) BakeMuttonCommand、BakeChickenWingCommandReceiver(接收者,真正做事的人) 烤肉師傅 Barbecuer Invoker(呼叫者) 服務生 Waiter
命令模式的好處 把「請求」變成物件之後,就能對它做很多事:
比較容易設計一個命令佇列 需要時可以把命令記錄到日誌 中 允許接收請求的一方決定是否要否決 請求(例如雞翅賣完了) 容易實作請求的撤銷和重做 (Undo / Redo) 新增具體命令類別不會影響其他類別 最關鍵的一點:命令模式把請求一個操作的物件 和知道怎麼執行這個操作的物件 分開了。
不要過度設計 如果目前不需要佇列、記錄、撤銷這些功能,就不必一開始就用命令模式。敏捷開發的原則是:等真正需要撤銷 / 重做等功能時,再把原本的程式重構成命令模式 也不遲。
實務上常見的例子:GUI 的按鈕和選單動作、任務排程器的 Job、訊息佇列裡的訊息、資料庫交易日誌。
《大話設計模式》讀書筆記系列