程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 我的Design Pattern之旅[3]:使用template改進Strategy Pattern(OO)(3)

我的Design Pattern之旅[3]:使用template改進Strategy Pattern(OO)(3)

編輯:關於C語言

這樣的設計看似完美,但IShape和Grapher相依程度太高,若將來有個Painter class,和Grapher完全不同,沒有任何繼承或多型的關系,但想重復使用IShape interface的strategy,這樣的設計就無法讓Painter使用了。若我們能讓IShape interface的draw()不再只限定Grapher型別,改用template,就能讓將來所有型別都能使用IShape interface。

template <typename T>
class IShape {
public:
 virtual void draw(T &grapher) const = 0;
};

我們
用泛型T取代了Grapher,任何型別都可傳進IShape::draw()。

完整程式碼如下

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程度更高。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved