c++涉及模式 橋接模式(bridge Pattern)

gaopengtttt發表於2017-04-17
c++涉及模式 橋接模式(bridge Pattern)
考慮這樣一個問題:
需要獲得一個圖形,這個圖形可以是圓形,可以是正方形,可以使長方形其顏色可以是藍色可以是紅色可以是綠色,如果這種情況下將設計寫死,那麼可以
看到有3*3=9 個類,但是圖形和顏色更多呢?那麼成為一個基本不能完成的任務,那麼在這種情況下我們就需一種叫做橋接的設計模式,它的原理同樣是
通過虛擬函式進行解耦合,實現方式 圖形抽象類通過一個輸入顏色抽象類的指標(依賴)來代表顏色,然後通過儲存在一個聚合的顏色抽象類指標成員中,這裡
通過這兩圖形抽象類和顏色抽象類進行解耦合,同時能夠實現任何顏色和任何圖形之間的組合,也是非常神奇的一種設計模式
下面是模式圖:



下面是上面問題的程式碼實現:
輸出為:

I'm bule rectangle

I'm red rectangle

I'm green square

I'm bule square


程式碼:

點選(此處)摺疊或開啟

  1. #include<iostream>
  2. using namespace std;

  3. //顏色虛介面
  4. class colour
  5. {
  6. public:
  7.     virtual void getcol() = 0;
  8.     virtual ~colour(){};
  9. };

  10. //形狀虛介面
  11. class graph
  12. {
  13. public:
  14.     virtual void setcol(colour* col) = 0; //依賴 橋接
  15.     virtual ~graph(){};
  16. protected:
  17.     colour* col; //聚合 橋接
  18. };

  19. //顏色具體實現
  20. class red:public colour
  21. {
  22. public:
  23.     virtual void getcol()
  24.     {
  25.         cout<<"I'm red ";
  26.     }
  27.     virtual ~red(){};
  28. };


  29. class bule:public colour
  30. {
  31. public:
  32.     virtual void getcol()
  33.     {
  34.         cout<<"I'm bule";
  35.     }
  36.     virtual ~bule(){};
  37. };

  38. class green:public colour
  39. {
  40. public:
  41.     virtual void getcol()
  42.     {
  43.         cout<<"I'm green ";
  44.     }
  45.     virtual ~green(){};
  46. };

  47. //形狀具體實現並且橋接到顏色

  48. class square:public graph
  49. {
  50. public:
  51.     square()
  52.     {
  53.        this->col = NULL ;
  54.     }

  55.     virtual void setcol(colour* col)
  56.     {
  57.         this->col = col;
  58.     }
  59.     void print()
  60.     {
  61.         this->col->getcol();
  62.         cout<<" square\n";
  63.     }
  64.     virtual ~square(){};
  65. };


  66. class triangle:public graph
  67. {
  68.  public:
  69.    triangle()
  70.    {
  71.       this->col = NULL ; ;
  72.    }
  73.     virtual void setcol(colour* col)
  74.     {
  75.         this->col = col;
  76.     }
  77.     void print()
  78.     {
  79.         this->col->getcol();
  80.         cout<<" triangle\n";
  81.     }
  82.     virtual ~triangle(){};
  83. };

  84. class rectangle:public graph
  85. {
  86.     public:
  87.     rectangle()
  88.     {
  89.        this->col = NULL ;
  90.     }
  91.     virtual void setcol(colour* col)
  92.     {
  93.         this->col = col;
  94.     }
  95.     void print()
  96.     {
  97.         this->col->getcol();
  98.         cout<<" rectangle\n";
  99.     }
  100.     virtual ~rectangle(){};
  101. };


  102. int main(void)
  103. {
  104.     bule tblue;
  105.     red tred;
  106.     green tgreen;

  107.     rectangle trectangle;
  108.     trectangle.setcol(&tblue); //任意組合
  109.     trectangle.print();
  110.     trectangle.setcol(&tred); //任意組合
  111.     trectangle.print();

  112.     square tsquare;
  113.     tsquare.setcol(&tgreen); //任意組合
  114.     tsquare.print();
  115.     tsquare.setcol(&tblue); //任意組合
  116.     tsquare.print();
  117. }
作者微信:

               

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-2137443/,如需轉載,請註明出處,否則將追究法律責任。

相關文章