This document introduces the strategy design pattern. It discusses using inheritance to add a fly function to ducks, but this leads to issues when rubber ducks cannot fly. The strategy pattern encapsulates varying behaviors like flying into separate classes/interfaces. This avoids issues with inheritance by composing behaviors instead. The key principles are to identify varying aspects and separate them, program to interfaces/supertypes, and favor composition over inheritance.
The document provides background information on the origins of design patterns. It discusses how Christopher Alexander introduced patterns for building architecture. In 1987, Kent Beck and Ward Cunningham began applying patterns to programming and presented their results at OOPSLA. Design patterns provide general reusable solutions to commonly occurring problems in software design. They describe relationships and interactions between classes or objects without specifying final classes or objects.
This document introduces the strategy design pattern. It discusses using inheritance to add a fly function to ducks, but this leads to issues when rubber ducks cannot fly. The strategy pattern encapsulates varying behaviors like flying into separate classes/interfaces. This avoids issues with inheritance by composing behaviors instead. The key principles are to identify varying aspects and separate them, program to interfaces/supertypes, and favor composition over inheritance.
The document provides background information on the origins of design patterns. It discusses how Christopher Alexander introduced patterns for building architecture. In 1987, Kent Beck and Ward Cunningham began applying patterns to programming and presented their results at OOPSLA. Design patterns provide general reusable solutions to commonly occurring problems in software design. They describe relationships and interactions between classes or objects without specifying final classes or objects.
4. 設計模式簡介
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.
Pattern
KK [ˋpætɚn]
5. 設計模式簡介
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.
11. 幾個物件導向設計的原則
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)的特性
15. 策略模式
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.
(策略模式定義了一系列的演算法,並將每一個演算法封裝起來,且使它們還可以相互替換。
策略模式讓演算法獨立於使用它的客戶而獨立變化。)