SlideShare una empresa de Scribd logo
1 de 18
Strategy
Ted
• 考慮變化的設計
– 針對介面設計程式而不要針對時做來設計程式
– 優先使用聚合,而不是類別繼承
– 考慮設計中甚麼是可變的,在變化發生時不需
重新設計
國際電子商務的例子
• Switch Creep
• 難以閱讀
• 難以理解
• 很容易有遺漏
• 使用繼承的方式解決?
• 看起來不錯?
新的需求…
• 貨運服務
• 國外可能沒有黑貓宅配或超商取貨
What can we do?
• Override Rule的method,使他不做任何事
• 這樣做真的對嗎?
• 而且這樣做會造成冗餘的程式碼,假設加
拿大跟日本的貨運是使用同樣的方式,但
是我們並無法將程式碼寫在同一個地方(使
用override),即使可以也會造成弱內聚(使
用Switch)
是否有其他的做法
• 想想開頭說的考慮變化的設計
– 尋找變化並封裝他
– 將這個類別包含在另一個類別中
• 解決了冗餘的程式碼也不需使用switch來判
斷要使用哪個rule
• 意圖:可以根據所處上下文使用不同的規則
或演算法
• 問題:隊所需演算法的選擇取決於發出請求
的對象
• 解決方案:將對演算法的選擇和實作分離
• 參與者與協作者
– Strategy指定了如何使用不同的演算法
– 利用各ConcreteStrategy實作
– Main 透過type選擇使用的演算法(多型)
• Strategy = new FirstStrategy()
• Strategy = new SecondStrategy()
• 效果
– Strategy定義了一系列的演算法
– 可不使用Switch
– 必須使用同樣介面呼叫演算法
• 實作
– Sample code
實作經驗
• 在使用getTax的範例中,英國某個年齡層的
人並不需付食物消費稅。
• 如何解決這個問題
• 將customer物件傳給rule物件
• 將customer的成員傳給rule物件
• 將this傳給rule物件
• 即使需要修改,但是我們還是可預期修改
幅度不致於太大,而且不太可能引入新問
題
• 封裝邏輯
• 簡化測試

Más contenido relacionado

Más de Ted Liang

google play service 7.8 & new tech in M
google play service 7.8 & new tech in M google play service 7.8 & new tech in M
google play service 7.8 & new tech in M Ted Liang
 
what new in google io 2015
what new in google io 2015 what new in google io 2015
what new in google io 2015 Ted Liang
 
Android Activity Transition(ShareElement)
Android Activity Transition(ShareElement)Android Activity Transition(ShareElement)
Android Activity Transition(ShareElement)Ted Liang
 
Android Activity Transition(ShareElement)
Android Activity Transition(ShareElement)Android Activity Transition(ShareElement)
Android Activity Transition(ShareElement)Ted Liang
 
無標題簡報
無標題簡報無標題簡報
無標題簡報Ted Liang
 
Dagger & rxjava & retrofit
Dagger & rxjava & retrofitDagger & rxjava & retrofit
Dagger & rxjava & retrofitTed Liang
 
Adapter Pattern
Adapter PatternAdapter Pattern
Adapter PatternTed Liang
 
設計模式的解析與活用 -開拓視野
設計模式的解析與活用 -開拓視野設計模式的解析與活用 -開拓視野
設計模式的解析與活用 -開拓視野Ted Liang
 
Design pattern intro
Design pattern introDesign pattern intro
Design pattern introTed Liang
 
物件導向範型
物件導向範型物件導向範型
物件導向範型Ted Liang
 

Más de Ted Liang (11)

google play service 7.8 & new tech in M
google play service 7.8 & new tech in M google play service 7.8 & new tech in M
google play service 7.8 & new tech in M
 
what new in google io 2015
what new in google io 2015 what new in google io 2015
what new in google io 2015
 
Android Activity Transition(ShareElement)
Android Activity Transition(ShareElement)Android Activity Transition(ShareElement)
Android Activity Transition(ShareElement)
 
Android Activity Transition(ShareElement)
Android Activity Transition(ShareElement)Android Activity Transition(ShareElement)
Android Activity Transition(ShareElement)
 
無標題簡報
無標題簡報無標題簡報
無標題簡報
 
Dagger & rxjava & retrofit
Dagger & rxjava & retrofitDagger & rxjava & retrofit
Dagger & rxjava & retrofit
 
Adapter Pattern
Adapter PatternAdapter Pattern
Adapter Pattern
 
設計模式的解析與活用 -開拓視野
設計模式的解析與活用 -開拓視野設計模式的解析與活用 -開拓視野
設計模式的解析與活用 -開拓視野
 
Design pattern intro
Design pattern introDesign pattern intro
Design pattern intro
 
物件導向範型
物件導向範型物件導向範型
物件導向範型
 
Git簡報
Git簡報Git簡報
Git簡報
 

Strategy Pattern