這樣的設計看似完美,但IShape和Grapher相依程度太高,若將來有個Painter class,和Grapher完全不同,沒有任何繼承或多型的關系,但想重復使用IShape interface的strategy,這樣的設計就無法讓Painter使用了。若我們能讓IShape interface的draw()不再只限定Grapher型別,改用template,就能讓將來所有型別都能使用IShape interface。
template <typename T>
用泛型T取代了Grapher,任何型別都可傳進IShape::draw()。
class IShape {
public:
virtual void draw(T &grapher) const = 0;
};
我們
完整程式碼如下
1/**//*
2(C) OOMusou 2007 http://oomusou.cnblogs.com
3
4Filename : DP_StrategyPattern3_template_this.cpp
5Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
6Description : Demo how to use Strategy Pattern with *this by template
7Release : 04/03/2007 1.0
8*/
9#include <iOStream>
10#include <string>
11
12using namespace std;
13
14class Grapher;
15
16template <typename T>
17class IShape {
18public:
19 virtual void draw(T &grapher) const = 0;
20};
21
22class Grapher {
23private:
24 IShape<Grapher> *shape;
25 string text;
26
27public:
28 Grapher() {}
29 Grapher(IShape<Grapher> *shape, const char* text = "Hello Shape!!") : shape(shape), text(string(text)) {}
30
31public:
32 void drawShape() {
33 this->shape->draw(*this);
34 }
35
36 void setShape(IShape<Grapher> *shape, const char* text = "Hello Shape!!") {
37 this->text = text;
38 this->shape = shape;
39 }
40
41 string getText() const {
42 return this->text;
43 }
44};
45
46template <typename T>
47class Triangle : public IShape<T> {
48public:
49 void draw(T &grapher) const {
50 cout << "Draw " << grapher.getText() << " in Triangle" << endl;
51 }
52};
53
54template <typename T>
55class Circle : public IShape<T> {
56public:
57 void draw(T &grapher) const {
58 cout << "Draw " << grapher.getText() << " in Circle" << endl;
59 }
60};
61
62template <typename T>
63class Square : public IShape<T> {
64public:
65 void draw(T &grapher) const {
66 cout << "Draw " << grapher.getText() << " in Square" << endl;
67 }
68};
69
70
71int main() {
72 Grapher theGrapher(&Square<Grapher>());
73 theGrapher.drawShape();
74
75 theGrapher.setShape(&Circle<Grapher>(), "Hello C++!!");
76 theGrapher.drawShape();
77}
執行結果
Draw Hello Shape!! in Square
Draw Hello C++!! in Circle
Conclusion
泛型的應用相當廣,在此范例僅僅是泛型的小小應用,在OOP世界使用strategy pattern,常將*this傳給strategy,若搭配GP可讓strategy pattern的resuse程度更高。