大佬教程收集整理的这篇文章主要介绍了设计模式 - Bridge 桥模式,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
Bridge桥模式也属于”的单一职责“模式中的典型模式。
问题描述:
我们绘制图形时,图形可以有不同形状以及不同颜色,比如圆形可以是红的,绿的,方形可以是红的绿的,如果用代码来描绘这些类,会有如下:
1 class Shape{ 2 }; 3 class Rectangle : public Shape{ 4 }; 5 class Circle : public Shape{ 6 }; 7 class Color{ 8 }; 9 class Red : public Color{ 10 }; 11 class Blue : public Color{ 12 }; 13 class RedRectangle : public Red{ 14 }; 15 class BlueRectangle : public Blue{ 16 };
每增加一种图形或者颜色,新增的类就会成倍得增长。而且CRedRectangle继承于颜色,似乎也不太合理,CRedRectangle和CRed之间不是一种is-a的关系。下面通过桥模式改善它?
将抽象部分(业务功能)与实现部分(平台实现)分离,使它们都可以独立地变化。 ——《设计模式》GoF
简单的说就是抽象对外提供的接口,对外隐瞒实现部分,在抽象中引用实现部分,从而实现抽象对实现部分的调用,而抽象中引用的实现部分可以在今后的开发中,切换为别的实现部分。
明显方案二采用聚合的方式可以减少很多类的数量。
Abstraction类定义了抽象类的接口,并且维护一个指向Implementor实现类的指针;
RefineAbstraction类扩充了Abstraction类的接口;
Implementor类定义了实现类的接口,这个接口不一定要与Abstraction的接口完全一致;实际上,这两个接口可以完全不同;
ConcreteImplementor类实现了Implementor定义的接口。
1 #include <iostream> 2 3 using namespace std; 4 5 //具体实现的抽象 6 class Implementor { 7 public: 8 virtual void operatonImpl() = 0; 9 }; 10 11 //具体实现 12 class ConcreteImplementor : public Implementor { 13 public: 14 void oerationImpl() { cout << "OperationImpl" << endl; } 15 }; 16 17 class Abstruction { 18 public: 19 Abstruction(Implementor* pImpl) : m_pImpl(pImpl) {} 20 virtual void operation() = 0; 21 22 protected: 23 Implementor* m_pImpl; 24 }; 25 26 //重新定义抽象 27 class RedfinedAbstraction : public Abstruction { 28 public: 29 RedfineAbstraction(Implementor* pImpl) : Abstruction(pImpl) {} 30 void operation() { m_pImpl->operatonImpl(); } 31 }; 32 33 int main() { 34 Implementor* pImplObj = new ConcreteImplementor(); 35 Abstruction* pAbsObj = new RedfineAbstraction(pImplObj); 36 pAbsObj->operation(); 37 delete pImplObj; 38 pImplObj = nullptr; 39 delete pAbsObj; 40 pAbsObj = nullptr; 41 return 0; 42 }
使用桥模式重新实现形状与颜色的代码:
1 #include <iostream> 2 #include <String> 3 4 using namespace std; 5 6 class Color { 7 public: 8 virtual String name() = 0; 9 10 protected: 11 String mname; 12 }; 13 14 class Green : public Color { 15 public: 16 Green() { mname = "Green"; } 17 virtual ~Green() {} 18 virtual String name() { return mname; } 19 }; 20 21 class Red : public Color { 22 public: 23 Red() { mname = "Red"; } 24 virtual ~Red() {} 25 virtual String name() { return mname; } 26 }; 27 28 class Shape { 29 public: 30 Shape(Color* color) : mColor(color) {} 31 virtual void myShape() = 0; 32 33 protected: 34 Color* mColor; 35 }; 36 37 class Rectangle : public Shape { 38 public: 39 Rectangle(Color* color) : Shape(color) {} 40 virtual void myShape() { 41 cout << "Rectangle has a " << mColor->name() << " color\n"; 42 } 43 }; 44 45 class Circle : public Shape { 46 public: 47 Circle(Color* color) : Shape(color) {} 48 virtual void myShape() { 49 cout << "Circle has a " << mColor->name() << " color\n"; 50 } 51 }; 52 53 int main() { 54 Color* red = new Red(); 55 Color* green = new Green(); 56 57 Shape* rectangle = new Rectangle(red); 58 rectangle->@H_159_10@myShape(); 59 Shape* circle = new Circle(green); 60 circle->@H_159_10@myShape(); 61 62 return 0; 63 }
分离抽象接口及其实现的部分
参考:
以上是大佬教程为你收集整理的设计模式 - Bridge 桥模式全部内容,希望文章能够帮你解决设计模式 - Bridge 桥模式所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。