DesignPatterns
1. 關於 Command
2. UML
3. 將 UML 轉為程式碼
4. 情境
OS:Windows 10
IDE:Visual Studio 2019
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
by Gang of Four
Command(命令)屬於行為型(Behavioral Patterns),當遇到想將不同工作參數化傳入並加入觸發邏輯時,可使用 Command 來將工作與觸發邏輯切割開來,調用端不需要知道實作細節即可呼叫。
優點:
缺點:
Class 間關聯:
Class:
命令介面
命令實作
被呼叫端
儲存與調用命令
command
中加載receiver
invoker
中加載command
並執行執行結果
我們接到了一個線上下訂咖啡、餐盒的需求
點餐介面
點咖啡實作 & 點餐盒實作
員工,包含熟食部 & 飲料部
儲存與調用命令
employee
加載到點咖啡orderCoffee
& 點餐盒orderBoxedMeal
invoker
加載點咖啡 & 點餐盒並執行點餐執行結果
GitHub:Behavioral_02_Command