(原創) 我的Design Pattern之旅[1]:Strategy Pattern (OO) (Design Pattern) (C/C++) (template) (.NET) (C#)
Abstract
Head First Design Patterns是用strategy pattern當作第一個範例,而陳俊杉教授也是用strategy當作授課的第一個pattern,可見strategy的確適合初學者學第一個學習的pattern。
Intent
定義一整族演算法,將每一個演算法封裝起來,可互換使用,更可以在不影響外界的情況下各別抽換所引用的演算法。
其UML表示法
GoF說strategy也稱為policy,我個人喜歡稱它為plugin,因為可以動態的換演算法,如同在eclipse上可以動態的換plugin一樣。
原本在單一class中有一個單一method很單純,如圖Grapher class只有drawShape()這個method,只能畫方形。
但後來『需求改變』,希望Grapher也能畫三角形和圓形,而且日後還可能增加功能,如畫橢圓形,菱形...,當然可以在Grapher陸續加上drawTriangle(),drawCircle(),drawEllipse(),但如此就違反OCP,Grapher須不斷的修改,根據DP第三守則"Identify the aspects of your application that vary and separate them from what you stays the same",將『會變』的部份另外包成class,但這些class必須要和原來的class溝通,所以必須訂出『標準』彼此才能溝通,IShape就是彼此溝通的標準,Triangle,Circle,Square則必須實做IShape這個interface,這就是strategy pattern。
我們看看這個架構,若日後還有新的shape加入,Grapher,IShape,Triangle,Circle,Square皆不用修改,符合OCP的closed for modification原則,若要加入新的class,只需實做IShape即可,符合OCP的open for extension原則,所以是非常好維護的架構,事實上,.NET Framework和STL都用了很多strategy pattern。
簡言之,strategy pattern就是將會變動的member function用class包起來,變成object『掛』在原本的class上。
ISO C++ by Interface
2(C) OOMusou 2007 http://oomusou.cnblogs.com
3
4Filename : DP_StrategyPattern_Classic.cpp
5Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
6Description : Demo how to use Strategy Pattern
7Release : 03/26/2007 1.0
8 07/10/2007 2.0
9*/
10#include <iostream>
11using namespace std;
12
13class IDrawStrategy {
14public:
15 virtual void draw() const = 0;
16};
17
18class Grapher {
19public:
20 Grapher(IDrawStrategy* drawStrategy = 0) : _drawStrategy(drawStrategy) {}
21
22public:
23 void drawShape() const;
24 void setShape(IDrawStrategy* drawStrategy);
25
26protected:
27 IDrawStrategy* _drawStrategy;
28};
29
30void Grapher::drawShape() const {
31 if (_drawStrategy)
32 _drawStrategy->draw();
33}
34
35void Grapher::setShape(IDrawStrategy* drawStrategy) {
36 _drawStrategy = drawStrategy;
37}
38
39class Triangle : public IDrawStrategy {
40public:
41 void draw() const;
42};
43
44void Triangle::draw() const {
45 cout << "Draw Triangle" << endl;
46}
47
48class Circle : public IDrawStrategy {
49public:
50 void draw() const;
51};
52
53void Circle::draw() const {
54 cout << "Draw Circle" << endl;
55}
56
57class Square : public IDrawStrategy {
58public:
59 void draw() const;
60};
61
62void Square::draw() const {
63 cout << "Draw Square" << endl;
64}
65
66int main() {
67 Grapher grapher(&Square());
68 grapher.drawShape();
69
70 grapher.setShape(&Circle());
71 grapher.drawShape();
72}
執行結果
Draw Circle
67行和70行可以看到strategy pattern的美,可以動態的換演算法,如同plugin一樣,且若將來擴充其他shape,只需加上新的class實做IDrawStrategy,其他程式都不用再改,符合OCP原則。
C# by Interface
2(C) OOMusou 2007 http://oomusou.cnblogs.com
3
4Filename : DP_StrategyPattern_Classic.cs
5Compiler : Visual Studio 2005 / C# 2.0
6Description : Demo how to implement Strategy Pattern by C#
7Release : 07/08/2007 1.0
8*/
9using System;
10
11interface IDrawStrategy {
12 void draw();
13}
14
15class Grapher {
16 private IDrawStrategy _drawStrategy = null;
17
18 public Grapher() {}
19 public Grapher(IDrawStrategy drawStrategy) {
20 _drawStrategy = drawStrategy;
21 }
22
23 public void drawShape() {
24 if (_drawStrategy != null)
25 _drawStrategy.draw();
26 }
27
28 public void setShape(IDrawStrategy drawStrategy) {
29 _drawStrategy = drawStrategy;
30 }
31}
32
33class Triangle : IDrawStrategy {
34 public void draw() {
35 Console.WriteLine("Draw Triangle");
36 }
37}
38
39class Circle : IDrawStrategy {
40 public void draw() {
41 Console.WriteLine("Draw Circle");
42 }
43}
44
45class Square : IDrawStrategy {
46 public void draw() {
47 Console.WriteLine("Draw Square");
48 }
49}
50
51class Program {
52 public static void Main() {
53 Grapher grapher = new Grapher(new Square());
54 grapher.drawShape();
55
56 grapher.setShape(new Circle());
57 grapher.drawShape();
58 }
59}
執行結果
Draw Circle
使用interface是最正規的OOP寫法,另外Effective C++的item 35也使用了function pointer來實做strategy pattern,function pointer是C/C++的獨門寫法。
ISO C++ by Function Pointer
2(C) OOMusou 2007 http://oomusou.cnblogs.com
3
4Filename : DP_StrategyPattern_FunctionPointer.cpp
5Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
6Description : Demo how to use Strategy Pattern by function pointer
7Release : 03/31/2007 1.0
8 : 07/08/2007 2.0
9 : 07/10/2007 3.0
10*/
11#include <iostream>
12
13using namespace std;
14
15class Grapher {
16public:
17 typedef void (*pfDraw)();
18 Grapher(pfDraw draw = 0) : _draw(draw) {}
19
20public:
21 void drawShape() const;
22 void setShape(pfDraw draw);
23
24protected:
25 pfDraw _draw;
26};
27
28void Grapher::drawShape() const {
29 if (_draw)
30 _draw();
31}
32
33void Grapher::setShape(pfDraw draw) {
34 _draw = draw;
35}
36
37class Triangle {
38public:
39 static void draw();
40};
41
42void Triangle::draw() {
43 cout << "Draw Triangle" << endl;
44}
45
46class Circle {
47public:
48 static void draw();
49};
50
51void Circle::draw() {
52 cout << "Draw Circle" << endl;
53}
54
55class Square {
56public:
57 static void draw();
58};
59
60void Square::draw() {
61 cout << "Draw Square" << endl;
62}
63
64int main() {
65 Grapher grapher(Square::draw);
66 grapher.drawShape();
67
68 grapher.setShape(Circle::draw);
69 grapher.drawShape();
70}
執行結果
Draw Circle
說穿了,本來只是本來由interface定義function的signature,現在改由16行的
定義pfDraw這個function pointer型別,所有要傳進的的function必須符合這個function pointer型別才可。
既然ISO C++可以用function pointer實現strategy pattern,就讓我想到C#的delegate了。delegate是C#對function pointer和observer pattern的實現,理應可用delegate來實現strategy pattern。
C# by Delegate
2(C) OOMusou 2007 http://oomusou.cnblogs.com
3
4Filename : DP_StrategyPattern_Delegate.cs
5Compiler : Visual Studio 2005 / C# 2.0
6Description : Demo how to implement Strategy Pattern by C# delegate
7Release : 07/08/2007 1.0
8*/
9using System;
10
11class Grapher {
12 private DrawDelegate _drawDelegate = null;
13
14 public delegate void DrawDelegate();
15
16 public Grapher() {}
17 public Grapher(DrawDelegate drawDelegate) {
18 _drawDelegate = drawDelegate;
19 }
20
21 public void drawShape() {
22 if (_drawDelegate != null)
23 _drawDelegate();
24 }
25
26 public void setShape(DrawDelegate drawDelegate) {
27 _drawDelegate = drawDelegate;
28 }
29}
30
31class Triangle {
32 public static void draw() {
33 Console.WriteLine("Draw Triangle");
34 }
35}
36
37class Circle {
38 public static void draw() {
39 Console.WriteLine("Draw Circle");
40 }
41}
42
43class Square {
44 public static void draw() {
45 Console.WriteLine("Draw Square");
46 }
47}
48
49class Program {
50 public static void Main() {
51 Grapher grapher = new Grapher(Square.draw);
52 grapher.drawShape();
53
54 grapher.setShape(Circle.draw);
55 grapher.drawShape();
56 }
57}
執行結果
Draw Circle
除此之外,GoF的Design Pattern也展示了使用template實做Strategy Pattern。
ISO C++ by Template
2(C) OOMusou 2007 http://oomusou.cnblogs.com
3
4Filename : DP_StrategyPattern_template.cpp
5Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
6Description : Demo how to use Strategy Pattern by template
7Release : 03/31/2007 1.0
8*/
9#include <iostream>
10
11using namespace std;
12
13template <typename T>
14class Grapher {
15private:
16 T _drawTemplate;
17
18public:
19 void drawShape() const;
20};
21
22template<typename T>
23void Grapher<T>::drawShape() const {
24 _drawTemplate.draw();
25}
26
27class Triangle {
28public:
29 void draw() const;
30};
31
32void Triangle::draw() const {
33 cout << "Draw Triangle" << endl;
34}
35
36class Circle {
37public:
38 void draw() const;
39};
40
41void Circle::draw() const {
42 cout << "Draw Circle" << endl;
43}
44
45class Square {
46public:
47 void draw() const;
48};
49
50void Square::draw() const {
51 cout << "Draw Square" << endl;
52}
53
54int main() {
55 Grapher<Square> grapher;
56 grapher.drawShape();
57
58 Grapher<Circle> grapher2;
59 grapher2.drawShape();
60}
執行結果
Draw Circle
同樣是實現多型,Design Pattern的用的是OOP的interface + dynamic binding技術,這是在run-time下完成,優點是在run-time動態改變,缺點是速度較慢;GP用template技術,這是在compile-time下完成,優點是速度較快,缺點是無法在run-time動態改變,由於template方式不需interface,所以整個程式看不到interface,也由於無法run-time改變,所以沒有setShape(),而16行的
也只是object而非pointer,因為不需run-time的多型,也非function pointer。
46行
也只能在直接指定strategy,無法動態再改變。
C# 2.0也提供泛型了,所以C#也可以用Generics實現Strategy Pattern。
C# by Generics
2(C) OOMusou 2007 http://oomusou.cnblogs.com
3
4Filename : DP_StrategyPattern_Generics.cs
5Compiler : Visual Studio 2005 / C# 2.0
6Description : Demo how to implement Strategy Pattern by C# Generics
7Release : 07/08/2007 1.0
8*/
9using System;
10
11interface IDrawStrategy {
12 void draw();
13}
14
15class Grapher<T> where T : class, IDrawStrategy, new() {
16 private T _drawStrategy = default(T);
17
18 public Grapher() {
19 _drawStrategy = new T();
20 }
21
22 public void drawShape() {
23 if (_drawStrategy != null)
24 _drawStrategy.draw();
25 }
26
27 public void setShape(T drawStrategy) {
28 _drawStrategy = drawStrategy;
29 }
30}
31
32class Triangle : IDrawStrategy {
33 public void draw() {
34 Console.WriteLine("Draw Triangle");
35 }
36}
37
38class Circle : IDrawStrategy {
39 public void draw() {
40 Console.WriteLine("Draw Circle");
41 }
42}
43
44class Square : IDrawStrategy {
45 public void draw() {
46 Console.WriteLine("Draw Square");
47 }
48}
49
50class Program {
51 public static void Main() {
52 Grapher<Square> grapher = new Grapher<Square>();
53 grapher.drawShape();
54
55 Grapher<Circle> grapher2 = new Grapher<Circle>();
56 grapher2.drawShape();
57 }
58}
執行結果
Draw Circle
Remark
strategy和template method目的相同,皆對『新需求』的不同演算法提供『擴充』的機制,但手法卻不同,strategy採用object的方式,利用delegation改變演算法,而template method則採用class的繼承方式來改變演算法,也因為strategy採用object方式,所以有run-time改變的可能,但template method採class手法,所以無法run-time改變。
GoF的原文如下
Known Use
1.eclipse的plugin,可以在不修改eclipse原始碼下,外掛plugin變更eclipse所提供的功能。
See Also
(原創) 我的Design Pattern之旅[2]:Template Method Pattern (OO) (Design Pattern) (C/C++)
(原創) 我的Design Pattern之旅[3]:使用template改進Strategy Pattern (OO) (Design Pattern) (C/C++) (template)
Reference
GoF,Design Patterns,Addison Weseley Longman,1995
A. Shalloway,J. R. Trott,Design Patterns Explained 2/e,Addison Wesley, 2005
Eric Freeman,Elisabeth Freeman,Head First Design Pattern,O'Reilly,2004
Robert C. Martin,Agile Software Development,Pearson Prentice Hall, 2002
Scott Meyers,Effective C++ 3/e Item 35,Addison Wesley, 2005