接續上一篇 [命令模式]執行一個請求與如何執行間的解耦: 使用C#
1. 命令模式可以較容易設計一個命令佇列
2. 命令模式也可以輕鬆加入日誌功能
3. 命令模式也可以決定是否要否決命令(設計一個invoker內部庫存計數器)
4. 命令模式可以實現對請求的取消或重作
5. 命令模式優點是把接受命令(Invoker服務生)和如何執行一個命令(receiver內場人員)解耦。
---------------------------------------------------------------------------------------
客戶端冰品可以依照個人喜好加入配料(+珍珠、+花生、+紅豆、+牛奶)有可能是以上任意組合,
假設一個客人點了以下四種口味 +珍珠 +紅豆 +花生 +牛奶
後來後悔加入花生,想改成兩份牛奶 OOXX…。
-花生
+牛奶
所以最後會是
+珍珠
+紅豆
+牛奶
+牛奶
由Invoker(服務生)決定加入/刪除某個項目(ConcreteCommand),最後由Receiver(內場人員)執行製作冰品。
(1) receiver: 知道如何執行該命令
1: class Receiver
2: {
3: public void Action(string action)
4: {
5: Console.WriteLine("+{0} 動作執行", action);
6: }
7: }
(2) Command: 一個抽象指令,擁有一個Receiver實體,在創建子階段綁定。
新增name(指令名稱)
1: abstract class Command
2: {
3: protected string name;
4: public string Name { get { return name; } set { name = value; } }
5: protected Receiver receiver;
6: public Command(Receiver receiver, string name)
7: {
8: this.receiver = receiver;
9: this.name = name;
10: }
11: abstract public void Execute();
12: }
(3) Concrete Command: 繼承Command
1: class PearlCommand : Command
2: {
3: public PearlCommand(Receiver receiver, string name) : base(receiver, name) { }
4: public override void Execute()
5: {
6: receiver.Action(this.Name);
7: }
8: }
9: class PeanutCommand : Command
10: {
11: public PeanutCommand(Receiver receiver, string name) : base(receiver, name) { }
12: public override void Execute()
13: {
14: receiver.Action(this.Name);
15: }
16: }
17: class RedBeansCommand : Command
18: {
19: public RedBeansCommand(Receiver receiver, string name) : base(receiver, name) { }
20: public override void Execute()
21: {
22: receiver.Action(this.Name);
23: }
24: }
25: class MilkCommand : Command
26: {
27: public MilkCommand(Receiver receiver, string name) : base(receiver, name) { }
28: public override void Execute()
29: {
30: receiver.Action(this.Name);
31: }
32: }
(4) invoker: 請求一個命令的執行,擁有一個Command實體,利用AddCommand/DeleteCommand加入/刪除一個命令(Concrete Command)以及ExecuteCommand執行命令。
1: class Invoker
2: {
3: IList<Command> cmdList = new List<Command>();
4:
5: public void AddCommand(Command command)
6: {
7: cmdList.Add(command);
8: Console.WriteLine("+{0}", command.Name);
9: }
10: public void ClearCommand()
11: {
12: Console.WriteLine("清空命令");
13: cmdList.Clear();
14: }
15: public void ExecuteCommand()
16: {
17: Console.WriteLine("=================");
18: foreach (Command cmd in cmdList)
19: {
20: cmd.Execute();
21: }
22: Console.WriteLine("=================");
23: }
24: public void DeleteCommand(Command command)
25: {
26: cmdList.Remove(command);
27: Console.WriteLine("-{0}", command.Name);
28: }
29: }
客戶端
1: static void Main(string[] args)
2: {
3: Receiver receiver = new Receiver();
4: Command pearl = new PearlCommand(receiver,"珍珠");
5: Command redBeans = new RedBeansCommand(receiver, "紅豆");
6: Command peanut = new PeanutCommand(receiver, "花生");
7: Command milk = new MilkCommand(receiver, "牛奶");
8: Invoker invoker = new Invoker();
9:
10: invoker.AddCommand(pearl);
11: invoker.AddCommand(redBeans);
12: invoker.AddCommand(peanut);
13: invoker.AddCommand(milk);
14:
15: invoker.DeleteCommand(peanut);
16: invoker.AddCommand(milk);
17: invoker.ExecuteCommand();
18: invoker.ClearCommand();
19:
20: invoker.AddCommand(peanut);
21: invoker.AddCommand(milk);
22: invoker.ExecuteCommand();
23:
24:
25: Console.Read();
26: }
留言列表