[root@ ~/learn_code/design_pattern/18_bridge]$ cat Abstraction.h
#ifndef _ABSTRACTION_H_
#define _ABSTRACTION_H_ class AbstractionImplement; class Abstraction
{
public:
virtual void Operation()=;//定义接口,表示该类所支持的操作
virtual ~Abstraction();
protected:
Abstraction();
}; class RefinedAbstractionA:public Abstraction
{
public:
RefinedAbstractionA(AbstractionImplement* imp);//构造函数
virtual void Operation();//实现接口
virtual ~RefinedAbstractionA();//析构函数
private:
AbstractionImplement* _imp;//私有成员
}; class RefinedAbstractionB:public Abstraction
{
public:
RefinedAbstractionB(AbstractionImplement* imp);//构造函数
virtual void Operation();//实现接口
virtual ~RefinedAbstractionB();//析构函数
private:
AbstractionImplement* _imp;//私有成员
};
#endif [root@ ~/learn_code/design_pattern/18_bridge]$ cat Abstraction.cpp
#include "Abstraction.h"
#include "AbstractionImplement.h"
#include <iostream> using namespace std; Abstraction::Abstraction()
{} Abstraction::~Abstraction()
{} RefinedAbstractionA::RefinedAbstractionA(AbstractionImplement* imp)
{
this->_imp = imp;
} RefinedAbstractionA::~RefinedAbstractionA()
{
delete this->_imp;
this->_imp = NULL;
} void RefinedAbstractionA::Operation()
{
cout << "RefinedAbstractionA::Operation" << endl;
this->_imp->Operation();
} RefinedAbstractionB::RefinedAbstractionB(AbstractionImplement* imp)
{
this->_imp = imp;
} RefinedAbstractionB::~RefinedAbstractionB()
{
delete this->_imp;
this->_imp = NULL;
} void RefinedAbstractionB::Operation()
{
cout << "RefinedAbstractionB::Operation" << endl;
this->_imp->Operation();
} [root@ ~/learn_code/design_pattern/18_bridge]$ cat AbstractionImplement.h
#ifndef _ABSTRACTIONIMPLEMENT_H_
#define _ABSTRACTIONIMPLEMENT_H_ //抽象基类,定义了实现的接口
class AbstractionImplement
{
public:
virtual void Operation()=;//定义操作接口
virtual ~AbstractionImplement();
protected:
AbstractionImplement();
}; // 继承自AbstractionImplement,是AbstractionImplement的不同实现之一
class ConcreteAbstractionImplementA:public AbstractionImplement
{
public:
ConcreteAbstractionImplementA();
void Operation();//实现操作
~ConcreteAbstractionImplementA();
protected:
}; // 继承自AbstractionImplement,是AbstractionImplement的不同实现之一
class ConcreteAbstractionImplementB:public AbstractionImplement
{
public:
ConcreteAbstractionImplementB();
void Operation();//实现操作
~ConcreteAbstractionImplementB();
protected:
};
#endif [root@ ~/learn_code/design_pattern/18_bridge]$ cat AbstractionImplement.cpp
#include "AbstractionImplement.h"
#include <iostream> using namespace std; AbstractionImplement::AbstractionImplement()
{} AbstractionImplement::~AbstractionImplement()
{} ConcreteAbstractionImplementA::ConcreteAbstractionImplementA()
{} ConcreteAbstractionImplementA::~ConcreteAbstractionImplementA()
{} void ConcreteAbstractionImplementA::Operation()
{
cout << "ConcreteAbstractionImplementA Operation" << endl;
} ConcreteAbstractionImplementB::ConcreteAbstractionImplementB()
{} ConcreteAbstractionImplementB::~ConcreteAbstractionImplementB()
{} void ConcreteAbstractionImplementB::Operation()
{
cout << "ConcreteAbstractionImplementB Operation" << endl;
} [root@ ~/learn_code/design_pattern/18_bridge]$ cat main.cpp
#include "Abstraction.h"
#include "AbstractionImplement.h"
#include <iostream> using namespace std; int main()
{
/* 将抽象部分与它的实现部分分离,使得它们可以独立地变化 1、抽象Abstraction与实现AbstractionImplement分离; 2、抽象部分Abstraction可以变化,如new RefinedAbstractionA(imp)、new RefinedAbstractionB(imp2); 3、实现部分AbstractionImplement也可以变化,如new ConcreteAbstractionImplementA()、new ConcreteAbstractionImplementB(); */ AbstractionImplement* imp = new ConcreteAbstractionImplementA(); //实现部分ConcreteAbstractionImplementA
Abstraction* abs = new RefinedAbstractionA(imp); //抽象部分RefinedAbstractionA
abs->Operation(); cout << "-----------------------------------------" << endl; AbstractionImplement* imp1 = new ConcreteAbstractionImplementB(); //实现部分ConcreteAbstractionImplementB
Abstraction* abs1 = new RefinedAbstractionA(imp1); //抽象部分RefinedAbstractionA
abs1->Operation(); cout << "-----------------------------------------" << endl; AbstractionImplement* imp2 = new ConcreteAbstractionImplementA(); //实现部分ConcreteAbstractionImplementA
Abstraction* abs2 = new RefinedAbstractionB(imp2); //抽象部分RefinedAbstractionB
abs2->Operation(); cout << "-----------------------------------------" << endl; AbstractionImplement* imp3 = new ConcreteAbstractionImplementB(); //实现部分ConcreteAbstractionImplementB
Abstraction* abs3 = new RefinedAbstractionB(imp3); //抽象部分RefinedAbstractionB
abs3->Operation(); cout << endl;
return ;
}
参考:
http://www.cnblogs.com/jiese/p/3164940.html
http://design-patterns.readthedocs.io/zh_CN/latest/structural_patterns/bridge.html