问题:
面向对象的需要解决的问题为:松耦合和高内聚;追求的目标是尽可能的提高系统模块内部的内聚,尽可能降低模块间的耦合;但这也是在面向对象设计过程中最为难把握的部分,大家肯定在OO的过程中遇到这样的问题:
1)客户给出了需求,于是使用一个类来实现(A);
2)客户需求变化,有两个算法实现功能,于是改变设计,我们通过一个抽象的基类,再定义两个具体的类来实现不同的算法(A1和A2);
3)客户又告诉我们说对于不同的操作系统,于是再抽象一个层次,作为一个抽象基类A0,在分别分为为每个操作系统派生具体类(A00和A01,其中A0表示原来的基类)实现不同操作系统上的客户需求,这样就差生了4个类;
4)可能用户的需求有所改变,比如有了一种新的算法。。。
5)这样带来了类的迅速膨胀;
Bridge模式用来解决这类问题,在Bridge模式中可以看到,系统被分为2个相对独立的部分,左边是抽象部分,右边是实现部分,这两个部分可以互相独立地进行修改:例如上面问题中的客户需求变化,当用户需求中需要从Abstract派生一个具体子类的时候,并不需要像上面通过继承方式,实现时候需要添加子类A1和A2;当需要添加算法时也只用改变右边实现(添加一个具体的实现类),而左边不用再变化,也不用添加具体子类。
讨论:Bridge是设计模式中比较复杂和难理解的模式之一,也是OO开发和设计中经常会用到的模式之一;使用组合(委托)的方式将抽象和实现彻底的解耦,这样的好处是抽象和实现可以分别独立的变化,系统的耦合性也得到了良好的降低;而实际上使用Bridge模式和使用带来问题方式的解决方案的根本区别在于是通过继承还是通过组合的方式实现一个功能需求,因此在面向对象分析和设计中的一个原则是Favor Composition Over Inheritance。
在VC6.0下的编码实现,大家可以看一下:
#ifndef _ABSTRACTION_H
#define _ABSTRACTION_H
#include "AbstractionImp.h"
class Abstraction
{
public:
Abstraction();
virtual ~Abstraction();
virtual void Operation() = 0;
};
class RefinedAbstraction : public Abstraction
{
public:
RefinedAbstraction(AbstractionImp *imp);
virtual ~RefinedAbstraction();
void Operation();
private:
AbstractionImp *pImp;
};
#endif
#include "Abstraction.h"
#include "Public.h"
Abstraction::Abstraction()
{
}
Abstraction::~Abstraction()
{
}
RefinedAbstraction::RefinedAbstraction(AbstractionImp *imp)
{
pImp = imp;
}
RefinedAbstraction::~RefinedAbstraction()
{
}
void RefinedAbstraction::Operation()
{
pImp->Operation();
}
#ifndef _ABSTRACTIONIMP_H
#define _ABSTRACTIONIMP_H
class AbstractionImp
{
public:
AbstractionImp();
virtual ~AbstractionImp();
virtual void Operation() = 0;
private:
};
class ContreteAbstractionImpA : public AbstractionImp
{
public:
ContreteAbstractionImpA();
virtual ~ContreteAbstractionImpA();
void Operation();
};
class ContreteAbstractionImpB : public AbstractionImp
{
public:
ContreteAbstractionImpB();
virtual ~ContreteAbstractionImpB();
void Operation();
};
#endif
#include "AbstractionImp.h"
#include "Public.h"
AbstractionImp::AbstractionImp()
{
cout<<"the AbstractionImp create!"<<endl;
}
AbstractionImp::~AbstractionImp()
{
}
ContreteAbstractionImpA::ContreteAbstractionImpA()
{
cout<<"the ContreteAbstractionImpA create!"<<endl;
}
ContreteAbstractionImpA::~ContreteAbstractionImpA()
{
}
void ContreteAbstractionImpA::Operation()
{
cout<<"Concrete ContreteAbstractionImpA ... "<<endl;
}
ContreteAbstractionImpB::ContreteAbstractionImpB()
{
cout<<"the ContreteAbstractionImpB create!"<<endl;
}
ContreteAbstractionImpB::~ContreteAbstractionImpB()
{
}
void ContreteAbstractionImpB::Operation()
{
cout<<"Contrete ContreteAbstractionImpB..."<<endl;
}
#ifndef _PUBLIC_H
#define _PUBLIC_H
#include <iostream>
#include "AbstractionImp.h"
#include "Abstraction.h"
using namespace std;
#endif
#include "Public.h"
int main()
{
AbstractionImp *imp = new ContreteAbstractionImpA();
imp->Operation();
Abstraction *pAbs = new RefinedAbstraction(imp);
pAbs->Operation();
imp = new ContreteAbstractionImpB();
imp->Operation();
pAbs = new RefinedAbstraction(imp);
pAbs->Operation();
return 0;
}
可以参考代码学习,这样能够体会的比较深刻。