设计模式(5) - Bridge桥接模式
2013-12-22 00:05
417 查看
1. 意图
将一个抽象类从它的具体实现中解耦出来,这样这两个类可以独立的变化,互不影响。2. UML类图
3. 代码实现
#include<iostream> using namespace std; //Implementor class DrawImplementor { public: virtual void drawSquare(double) = 0; virtual ~DrawImplementor() { } }; // ConcreteImplementor A class DrawWithBrush: public DrawImplementor { public: DrawWithBrush() { } virtual ~DrawWithBrush() { } void drawSquare(double side) { cout<<"DrawWithBrush->square with side="<<side<<endl; } }; // ConcreteImplementor B class DrawWithPencil: public DrawImplementor { public: DrawWithPencil() { } virtual ~DrawWithPencil() { } void drawSquare(double side) { cout<<"DrawWithPencil->square with side="<<side<<endl; } }; // Abstraction class Shape { public: virtual void draw() = 0; virtual void resize(double) = 0; virtual ~Shape() { } }; // Refined Abstraction class Square: public Shape { public: Square(double s, DrawImplementor& impl): side(s),implementor(impl) { } virtual ~Square() { } void draw() { implementor.drawSquare(side); } void resize(double pct) { side *= pct; } private: double side; DrawImplementor& implementor; }; int main(int argc, char *argv[]) { DrawWithBrush brush; DrawWithPencil pencil; Square sq1(1,brush); Square sq2(2,pencil); sq1.resize(10); sq1.draw(); sq2.resize(10); sq2.draw(); return 0; }运行结果:
DrawWithBrush->square with side=10
DrawWithPencil->square with side=20
相关文章推荐
- 设计模式(4) - Prototype原型模式
- TPTP 资料记录
- 小经典理解
- 内存管理
- 设计模式(3) - Builder建造者模式
- 《人件》读书笔记
- Android实现启用GPRS/3G网络
- .bss .data .text 区别
- 十五章
- java 类加载器
- 关于 resgen.exe已退出 代码为 2 的错误问题的解决办法。
- Shadow学习笔记(PSM,LiSPSM,TSM,PSSM,CSM)
- WCDMA基本概念
- php安装xdebug/php安装pear/phpunit详解步骤(图)
- 在线一元二次方程计算器实例(方程计算器在线计算)
- JS中的异常处理方法分享
- 开启bin-log日志mysql报错的解决方法
- mysql服务无法启动报错误1067解决方法(mysql启动错误1067 )
- Hadoop 中 MapReduce 新旧改变
- 使用Sass创建弹性网格