bridge pattern(桥接模式)


效果及实现要点:

1.Bridge模式使用“对象间的组合关系”解耦了抽象和实现之间固有的绑定关系,使得抽象和实现可以沿着各自的维度来变化。
2.所谓抽象和实现沿着各自维度的变化,即“子类化”它们,得到各个子类之后,便可以任意它们,从而获得不同版本上drawing。
3.Bridge模式有时候类似于多继承方案,但是多继承方案往往违背了类的单一职责原则(即一个类只有一个变化的原因),复用性比较差。Bridge模式是比多继承方案更好的解决方法。
4.Bridge模式的应用一般在“两个非常强的变化维度”,有时候即使有两个变化的维度,但是某个方向的变化维度并不剧烈——换言之两个变化不会导致纵横交错的结果,并不一定要使用Bridge模式。

适用性:
   在以下的情况下应当使用桥梁模式:
1.如果一个系统需要在构件的抽象化角色和具体化角色之间增加更多的灵活性,避免在两个层次之间建立静态的联系。
2.设计要求实现化角色的任何改变不应当影响客户端,或者说实现化角色的改变对客户端是完全透明的。
3.一个构件有多于一个的抽象化角色和实现化角色,系统需要它们之间进行动态耦合。
4.虽然在系统中使用继承是没有问题的,但是由于抽象化角色和具体化角色需要独立变化,设计要求需要独立管理这两者。
总结:

      Bridge模式是一个非常有用的模式,也非常复杂,它很好的符合了开放-封闭原则和优先使用对象,而不是继承这两个面向对象原则。


桥接模式与装饰的区别:
装饰模式:

      这两个模式在一定程度上都是为了减少子类的数目,避免出现复杂的继承关系。但是它们解决的方法却各有不同,装饰模式把子类中比基类中多出来的部分放到单独的类里面,以适应新功能增加的需要,当我们把描述新功能的类封装到基类的对象里面时,就得到了所需要的子类对象,这些描述新功能的类通过组合可以实现很多的功能组合 .
桥接模式:
          桥接模式则把原来的基类的实现化细节抽象出来,在构造到一个实现化的结构中,然后再把原来的基类改造成一个抽象化的等级结构,这样就可以实现系统在多个维度上的独立变化 。




Example 9-6 C++ Code Fragments: 
The Bridge Implemented
void main (String argv[]) {
Shape *s1;
Shape *s2;
Drawing *dp1, *dp2;
dp1= new V1Drawing;
s1=new Rectangle(dp,1,1,2,2);
dp2= new V2Drawing;
s2= new Circle(dp,2,2,4);
s1->draw(); 
s2->draw();
delete s1; delete s2; 
delete dp1; delete dp2;
}
// NOTE: Memory management not tested.
// Includes not shown.
class Shape {
public: draw()=0;
private: Drawing *_dp;
}
Shape::Shape (Drawing *dp) {
_dp= dp;
}
void Shape::drawLine(
double x1, double y1, 
double x2, double y2)
_dp->drawLine(x1,y1,x2,y2);
}
Rectangle::Rectangle (Drawing *dp, 
double x1, y1, x2, y2) :
Shape( dp) {
_x1= x1; _x2= x2; 
_y1= y1; _y2= y2;
}

void Rectangle::draw () {
drawLine(_x1,_y1,_x2,_y1);
drawLine(_x2,_y1,_x2,_y2);
drawLine(_x2,_y2,_x1,_y2);
drawLine(_x1,_y2,_x1,_y1);
}
class Circle { 
public: Circle (
Drawing *dp, 
double x, double y, double r);
};
Circle::Circle (
Drawing *dp, 
double x, double y, 
double r) : Shape(dp) {
_x= x; 
_y= y;
_r= r;
}
Circle::draw () {
drawCircle( _x, _y, _r);
}
class Drawing {
public: virtual void drawLine (
double x1, double y1,
double x2, double y2)=0;
};
class V1Drawing : 
public Drawing {
public: void drawLine (
double x1, double y1, 
double x2, double y2); 
void drawCircle(
double x, double y, double r);
};
void V1Drawing::drawLine ( 
double x1, double y1,
double x2, double y2) { 
DP1.draw_a_line(x1,y1,x2,y2); 
}

void V1Drawing::drawCircle (
double x1, double y, double r) {
DP1.draw_a_circle (x,y,r);
}
class V2Drawing : public 
Drawing {
public: 
void drawLine (
double x1, double y1, 
double x2, double y2); 
void drawCircle( 
double x, double y, double r);
};
void V2Drawing::drawLine (
double x1, double y1,
double x2, double y2) {
DP2.drawline(x1,x2,y1,y2);
}
void V2Drawing::drawCircle (
double x, double y, double r) {
DP2.drawcircle(x, y, r);
}
// We have been given the implementations for 
// DP1 and DP2
class DP1 {
public:
static void draw_a_line (
double x1, double y1,
double x2, double y2);
static void draw_a_circle (
double x, double y, double r);
};
class DP2 {
public:
static void drawline (
double x1, double x2,
double y1, double y2);
static void drawcircle (
double x, double y, double r);
};


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值