Design pattern strategy pattern 策略模式

20
1 Design Pattern 之 Strategy Pattern 2013/0 3

description

策略模式簡介

Transcript of Design pattern strategy pattern 策略模式

Page 1: Design pattern strategy pattern 策略模式

1

Design Pattern 之Strategy Pattern

2013/03

Page 2: Design pattern strategy pattern 策略模式

大綱

2

設計模式簡介案例實作方法分析解決方案策略模式QA

Page 3: Design pattern strategy pattern 策略模式

設計模式簡介

3

「設計模式」一詞源於美國建築理論學家 Christopher Alexander ,他解釋: 每一則模式都在描述「某種一再出現的問題」,並描述解決方案的核心,讓你能據以變化出各種招式,解決上萬個類似的問題。 (Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.)

Page 4: Design pattern strategy pattern 策略模式

設計模式簡介

4

給 Pattern 一個簡單的定義 :只要是一再重複出現的事物,就是 Pattern 。基本上, Pattern  就是一種公式化的表現。以工程來說,公式化是好事。這些公式都是「千錘百鍊」的結果,運用這些公式可以確保工程具備一定的品質,並加快工程的進行。

花樣 , 圖案I don't like the pattern on the fabric.

形態 , 樣式 ; 格局They like new patterns of family life.

樣品 , 樣本( 服裝裁剪的 ) 紙樣 , ( 澆鑄用的 ) 模具Mary used a paper pattern to make her new dress.

模範 , 榜樣 , 典型This hospital is a pattern of what a good hospital should be.

PatternKK [ˋpætɚn]

Page 5: Design pattern strategy pattern 策略模式

設計模式簡介

5

設計模式 (Design Patterns) 由 Erich Gamma 、 Richard Helm 、 Ralph Johnson 、John M. Vlissides 等四人在 90 年代從建築設計領域引入到計算機科學中。

對軟體設計中普遍存在或反覆出現的各種問題,所提出的解決方案。 在四人合著的「 Design Patterns (設計模式)」一書,將軟體設計問題,共歸納出

23 種模式。 並非所有的軟體模式都是設計模式,設計模式主要是針對單一問題的解決方法,範

疇比較小,如架構、演算法便不算。

Designing object-oriented software is hard, and designing reusable object-oriented software is even harder.

Page 6: Design pattern strategy pattern 策略模式

案例

6

以模擬鴨子池塘遊戲為例

Page 7: Design pattern strategy pattern 策略模式

實作方法分析

7

比爾是個程式設計師,他為一家開發模擬鴨子池塘遊戲的公司工作,該公司的主要產品是一種可模擬多種會游泳和呱呱叫的鴨子遊戲。這個遊戲使用物件導向技術開發,系統裡所有鴨子都繼承於 Duck 基底類別:

Page 8: Design pattern strategy pattern 策略模式

實作方法分析

8

某天英明 (?) 的高層決定給系統增加一些超炫的功能,以徹底擊垮競爭對手。經過討論,最終決定如果能讓某些鴨子飛起來,那麼一定可以給對手致命一擊!

這樣真的 ok 嗎?如果是…剛剛的橡皮鴨呢?

Page 9: Design pattern strategy pattern 策略模式

實作方法分析

9

How about using the interface?

那如果有天增加到有幾十、上百種鴨子的時候怎麼辦?

Page 10: Design pattern strategy pattern 策略模式

問題分析

10

並不是所有的鴨子都會飛、會叫,所以直接繼承不是正確的方法。

使用 interface 解決上面的問題,但會破壞物件導向重用的優點。

所有的鴨子的飛行、叫聲等行為都是一模一樣的嗎?不可能!

Page 11: Design pattern strategy pattern 策略模式

幾個物件導向設計的原則

11

Identify the aspects of your application that vary and separate them from what stays the same. (找到系統中變化的部分,將變化的部分同其它穩定的部分隔開。 )找到變化並且把它封裝起來,就可以在不影響其它部分的情況下修改或擴展被封裝的變化部分。穩定的行為: swim() 不穩定的行為: fly() 、 quack()

Program to an interface, not an implementation. (針對介面寫程式,而不要針對實作。)程式只依賴於介面 (interface, abstract, super class) ,而不管這些介面提供的功能是如何實現的,也就是利用介面將不同部分隔離開,同時又將它們連接在一起。 將變化的部份 (fly() 、 quack()) 用介面隔離起來利用物件導向多型 (polymorphism) 的特性

Page 12: Design pattern strategy pattern 策略模式

介面設計

12

將鴨子的行為實現在一組獨立的類別裡。鴨子透過介面來呼叫行為,所以不用管介面如何被實作。

Page 13: Design pattern strategy pattern 策略模式

類別設計

13

賦予動態改變行為的能力呼叫行為

宣告行為介面的變數

Page 14: Design pattern strategy pattern 策略模式

類別設計

14

主程式

Page 15: Design pattern strategy pattern 策略模式

策略模式

15

定義:The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.(策略模式定義了一系列的演算法,並將每一個演算法封裝起來,且使它們還可以相互替換。策略模式讓演算法獨立於使用它的客戶而獨立變化。)

Page 16: Design pattern strategy pattern 策略模式

策略模式

16

Context: 需要使用 Concrete Strategy 提供的演算法。 內部宣告一個 Strategy 的實體。 負責動態設定運行時 Strategy 具體的實現演算法。 負責跟 Strategy 之間的溝通和數據傳遞。Strategy:定義了一個共用介面,各種不同的演算法以不同的方式實作這個介面, Context 使用這個介面呼叫不同的演算法,一般使用 interface 或 abstract class 實現。Concrete Strategy:實作 Strategy 定義的介面,提供演算法實體類別 (instance) 。

Page 17: Design pattern strategy pattern 策略模式

策略模式

17

使用時機:當多個類別只有表現的行為不同時,可在執行期 (runtime)動態選擇具體要執行的行為。

( 例如 FlyBehavior 和 QuackBehavior)需要在不同情況下使用不同的策略 ( 演算法 ) ,或者策略還可能在未來用其它方式來實

作。 ( 例如 FlyBehavior 和 QuackBehavior 的實體可任意變化或擴充 ) 對客戶端 (Duck)隱藏具體策略 ( 演算法 ) 的實作細節,彼此完全獨立。

Page 18: Design pattern strategy pattern 策略模式

策略模式

18

優點: 提供一種替代繼承的方法,而且既保持了繼承的優點 ( 程式碼重用 )且比繼承更靈活 ( 演

算法獨立,可以任意擴展 ) 。避免在程式中使用多重條件分支語法 (if 、 switch) ,使系統更靈活,並易於擴展。符合物件導向設計原則:「高內聚、低耦合」。

缺點:因為每個具體策略都會產生一個新類別,所以會增加系統需要維護程式的數量。 用戶端仍然得負擔選擇實現策略的責任,當增加新的策略時還是得維護用戶端程式。

可使用抽象工廠模式(abstract factory) 解決這個問題。

Page 19: Design pattern strategy pattern 策略模式

參考資料

19

書籍:深入淺出設計模式 大話設計模式

網站:Design Pattern新解: http://goo.gl/XLtljStack Doc : http://goo.gl/bnsht

Page 20: Design pattern strategy pattern 策略模式

QA

20

Thank you!