DesignPatterns
1. 關於 Chain of Responsibility
2. UML
3. 將 UML 轉為程式碼
4. 情境
OS:Windows 10
IDE:Visual Studio 2019
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.
by Gang of Four
Chain of Responsibility(責任鏈)屬於行為型(Behavioral Patterns),當遇到未知請求 & 未知處理者時,可使用 Chain of Responsibility 來將請求依序對應全部處理者,找尋可處理請求的對象。當遇到過濾、攔截的需求時,可使用 Chain of Responsibility 來依照指定順序執行判斷邏輯。
優點:
缺點:
Class 間關聯:
Class:
處理請求的抽象類別
處理請求實作 1 / 2 / 3
handler
SetSuccessor
設置接續執行的handler
執行結果
我們接到了一個員工請假簽核的需求
請假簽核抽象類別
店長 & 區主管 & 總經理簽核實作
handler
SetSuccessor
設置接續執行的主管執行結果
GitHub:Behavioral_01_ChainOfResponsibility