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

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/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7728585/viewspace-2137443/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值