DesignPatterns
1. 關於 Template Method
2. UML
3. 將 UML 轉為程式碼
4. 情境
OS:Windows 10
IDE:Visual Studio 2019
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
by Gang of Four
Template Method(樣板方法)屬於行為型(Behavioral Patterns),當遇到大部分流程相同只有部分邏輯有差異 時,可藉由 Template Method 將差異邏輯部分抽出各自實作,再依據需求選擇即可。
優點:
缺點:
Class 間關聯:
Class:
定義演算法步驟的抽象類別
實作演算法特定步驟 A / B
執行結果
我們接到了一依據資料時間選擇資料庫的需求
取得客戶訂單邏輯的抽象類別
分別實作從資料庫 A / B 取得訂單資料
OrderInDBA
並取得半年內訂單資訊OrderInDBB
並取得超過半年以上訂單資訊執行結果
GitHub:Behavioral_10_TemplateMethod