(原創) 我的Design Pattern之旅[3]:使用template改進Strategy Pattern (OO) (Design Pattern) (C/C++) (template)...

在strategy pattern中,為了讓各strategy能方便存取原來物件的所有public member function,我們常用*this將整個物件傳給各strategy,這樣的設計並沒有什麼不好,但各strategy和原物件過於tight coupling,導致各strategy難以再和其他各物件搭配,本文使用template解決此問題。

(原創) 我的Design Pattern之旅:Strategy Pattern (初級) (Design Pattern) (C++) (OO C++) (Template C++)中,我們使用了strategy pattern讓Grapher能畫Triangle、Circle和Square

因為需求再次改變,:D,我們希望Grapher能將文字印在各Shape中,執行結果如下

None.gif Draw Hello Shape!! in Square
None.gifDraw Hello C++!! in Circle


為了達到此需求,我們可以將IShape interface改成

ExpandedBlockStart.gif ContractedBlock.gif class  IShape  dot.gif {
InBlock.gif
public:
InBlock.gif  
virtual void draw(const char *text) const = 0;
ExpandedBlockEnd.gif}
;


但若將來需求再改變,希望畫的不是文字,而是一張圖片,那interface又必須再變動,為了一勞永逸,我們會將整個物件傳給各strategy,IShape interface改成如下

ExpandedBlockStart.gif ContractedBlock.gif class  IShape  dot.gif {
InBlock.gif
public:
InBlock.gif  
virtual void draw(Grapher &grapher) const = 0;
ExpandedBlockEnd.gif}
;


Grapher::drawShpae()將*this傳給各strategy

ExpandedBlockStart.gif ContractedBlock.gif void  drawShape()  dot.gif {
InBlock.gif  
this->shape->draw(*this);
ExpandedBlockEnd.gif}


完整的程式碼如下


 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /* 
 2InBlock.gif(C) OOMusou 2007 http://oomusou.cnblogs.com
 3InBlock.gif
 4InBlock.gifFilename    : DP_StrategyPattern3_polymorphism_this.cpp
 5InBlock.gifCompiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 6InBlock.gifDescription : Demo how to use Strategy Pattern with *this
 7InBlock.gifRelease     : 04/04/2007 1.0
 8ExpandedBlockEnd.gif*/

 9 None.gif#include  < iostream >
10 None.gif#include  < string >
11 None.gif
12 None.gif using   namespace  std;
13 None.gif
14 None.gif class  Grapher;
15 None.gif
16 ExpandedBlockStart.gifContractedBlock.gif class  IShape  dot.gif {
17InBlock.gifpublic:
18InBlock.gif  virtual void draw(Grapher &grapher) const = 0;
19ExpandedBlockEnd.gif}
;
20 None.gif
21 ExpandedBlockStart.gifContractedBlock.gif class  Grapher   dot.gif {
22InBlock.gifprivate:
23InBlock.gif  IShape *shape;
24InBlock.gif  string text;
25InBlock.gif
26InBlock.gifpublic:
27ExpandedSubBlockStart.gifContractedSubBlock.gif  Grapher() dot.gif{}
28ExpandedSubBlockStart.gifContractedSubBlock.gif  Grapher(IShape *shape, const char *text = "Hello Shape!!") : shape(shape), text(string(text)) dot.gif{}
29InBlock.gif  
30InBlock.gifpublic:
31ExpandedSubBlockStart.gifContractedSubBlock.gif  void drawShape() dot.gif{
32InBlock.gif    this->shape->draw(*this);
33ExpandedSubBlockEnd.gif  }

34InBlock.gif  
35ExpandedSubBlockStart.gifContractedSubBlock.gif  void setShape(IShape *shape, const char* text = "Hello Shape!!"dot.gif{
36InBlock.gif    this->text = text;
37InBlock.gif    this->shape = shape;
38ExpandedSubBlockEnd.gif  }
 
39InBlock.gif  
40ExpandedSubBlockStart.gifContractedSubBlock.gif  string getText() const dot.gif{
41InBlock.gif    return this->text;
42ExpandedSubBlockEnd.gif  }

43ExpandedBlockEnd.gif}
;
44 None.gif
45 None.gif
46 ExpandedBlockStart.gifContractedBlock.gif class  Triangle :  public  IShape  dot.gif {
47InBlock.gifpublic:
48ExpandedSubBlockStart.gifContractedSubBlock.gif  void draw(Grapher &grapher) const dot.gif{
49InBlock.gif    cout << "Draw " << grapher.getText() << " in Triangle" << endl;
50ExpandedSubBlockEnd.gif  }

51ExpandedBlockEnd.gif}
;
52 None.gif
53 ExpandedBlockStart.gifContractedBlock.gif class  Circle :  public  IShape  dot.gif {
54InBlock.gifpublic:
55ExpandedSubBlockStart.gifContractedSubBlock.gif  void draw(Grapher &grapher) const dot.gif{
56InBlock.gif    cout << "Draw " << grapher.getText() << " in Circle" << endl;
57ExpandedSubBlockEnd.gif  }

58ExpandedBlockEnd.gif}
;
59 None.gif
60 ExpandedBlockStart.gifContractedBlock.gif class  Square :  public  IShape  dot.gif {
61InBlock.gifpublic:
62ExpandedSubBlockStart.gifContractedSubBlock.gif  void draw(Grapher &grapher) const dot.gif{
63InBlock.gif    cout << "Draw " << grapher.getText() << " in Square" << endl;
64ExpandedSubBlockEnd.gif  }

65ExpandedBlockEnd.gif}
;
66 None.gif
67 None.gif
68 ExpandedBlockStart.gifContractedBlock.gif int  main()  dot.gif {
69InBlock.gif  Grapher theGrapher(&Square());
70InBlock.gif  theGrapher.drawShape();
71InBlock.gif  
72InBlock.gif  theGrapher.setShape(&Circle(), "Hello C++!!");
73InBlock.gif  theGrapher.drawShape();
74ExpandedBlockEnd.gif}


執行結果

None.gif Draw Hello Shape!! in Square
None.gifDraw Hello C++!! in Circle


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

None.gif template  < typename T >
ExpandedBlockStart.gifContractedBlock.gif
class  IShape  dot.gif {
InBlock.gif
public:
InBlock.gif  
virtual void draw(T &grapher) const = 0;
ExpandedBlockEnd.gif}
;

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

完整程式碼如下

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /* 
 2InBlock.gif(C) OOMusou 2007 http://oomusou.cnblogs.com
 3InBlock.gif
 4InBlock.gifFilename    : DP_StrategyPattern3_template_this.cpp
 5InBlock.gifCompiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 6InBlock.gifDescription : Demo how to use Strategy Pattern with *this by template
 7InBlock.gifRelease     : 04/03/2007 1.0
 8ExpandedBlockEnd.gif*/

 9 None.gif#include  < iostream >
10 None.gif#include  < string >
11 None.gif
12 None.gif using   namespace  std;
13 None.gif
14 None.gif class  Grapher;
15 None.gif
16 None.giftemplate  < typename T >
17 ExpandedBlockStart.gifContractedBlock.gif class  IShape  dot.gif {
18InBlock.gifpublic:
19InBlock.gif  virtual void draw(T &grapher) const = 0;
20ExpandedBlockEnd.gif}
;
21 None.gif
22 ExpandedBlockStart.gifContractedBlock.gif class  Grapher  dot.gif {
23InBlock.gifprivate:
24InBlock.gif  IShape<Grapher> *shape;
25InBlock.gif  string text;
26InBlock.gif    
27InBlock.gifpublic:
28ExpandedSubBlockStart.gifContractedSubBlock.gif  Grapher() dot.gif{}
29ExpandedSubBlockStart.gifContractedSubBlock.gif  Grapher(IShape<Grapher> *shape, const char* text = "Hello Shape!!") : shape(shape), text(string(text)) dot.gif{}
30InBlock.gif  
31InBlock.gifpublic:
32ExpandedSubBlockStart.gifContractedSubBlock.gif  void drawShape() dot.gif{
33InBlock.gif    this->shape->draw(*this);
34ExpandedSubBlockEnd.gif  }

35InBlock.gif  
36ExpandedSubBlockStart.gifContractedSubBlock.gif  void setShape(IShape<Grapher> *shape, const char* text = "Hello Shape!!"dot.gif{
37InBlock.gif    this->text = text;
38InBlock.gif    this->shape = shape;
39ExpandedSubBlockEnd.gif  }

40InBlock.gif  
41ExpandedSubBlockStart.gifContractedSubBlock.gif  string getText() const dot.gif{
42InBlock.gif    return this->text;
43ExpandedSubBlockEnd.gif  }

44ExpandedBlockEnd.gif}
;
45 None.gif
46 None.giftemplate  < typename T >
47 ExpandedBlockStart.gifContractedBlock.gif class  Triangle :  public  IShape < T >   dot.gif {
48InBlock.gifpublic:
49ExpandedSubBlockStart.gifContractedSubBlock.gif  void draw(T &grapher) const dot.gif{
50InBlock.gif    cout << "Draw " << grapher.getText() << " in Triangle" << endl;
51ExpandedSubBlockEnd.gif  }

52ExpandedBlockEnd.gif}
;
53 None.gif
54 None.giftemplate  < typename T >
55 ExpandedBlockStart.gifContractedBlock.gif class  Circle :  public  IShape < T >   dot.gif {
56InBlock.gifpublic:
57ExpandedSubBlockStart.gifContractedSubBlock.gif  void draw(T &grapher) const dot.gif{
58InBlock.gif    cout << "Draw " << grapher.getText() << " in Circle" << endl;
59ExpandedSubBlockEnd.gif  }

60ExpandedBlockEnd.gif}
;
61 None.gif
62 None.giftemplate  < typename T >
63 ExpandedBlockStart.gifContractedBlock.gif class  Square :  public  IShape < T >   dot.gif {
64InBlock.gifpublic:
65ExpandedSubBlockStart.gifContractedSubBlock.gif  void draw(T &grapher) const dot.gif{
66InBlock.gif    cout << "Draw " << grapher.getText() << " in Square" << endl;
67ExpandedSubBlockEnd.gif  }

68ExpandedBlockEnd.gif}
;
69 None.gif
70 None.gif
71 ExpandedBlockStart.gifContractedBlock.gif int  main()  dot.gif {
72InBlock.gif  Grapher theGrapher(&Square<Grapher>());
73InBlock.gif  theGrapher.drawShape();
74InBlock.gif  
75InBlock.gif  theGrapher.setShape(&Circle<Grapher>(), "Hello C++!!");
76InBlock.gif  theGrapher.drawShape();
77ExpandedBlockEnd.gif}


執行結果

None.gif Draw Hello Shape!! in Square
None.gifDraw Hello C++!! in Circle


Conclusion
泛型的應用相當廣,在此範例僅僅是泛型的小小應用,在OOP世界使用strategy pattern,常將*this傳給strategy,若搭配GP可讓strategy pattern的resuse程度更高。

See Also
(原創) 我的Design Pattern之旅[1]:Strategy Pattern (初級) (Design Pattern) (C++) (OO C++) (Template C++)
(原創) 我的Design Pattern之旅[4]:使用Generic改進Strategy Pattern (高級) (Design Pattern) (C#) (Generic)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值