bridge(橋模式)
基類Implementor,定義一些操作接口
class Implementor
{
public:
virtual void operationImp()=0;
~Implementor(){}
};
基類Abstraction有指向Implementor的多態(tài)指針日月,以及自己的操作接口
class Abstraction
{
public:
virtual void option()=0;
~Abstraction(){}
Abstraction(Implementor* _imp):imp(_imp){}
protected:
Implementor* imp;
};
ConcreateImplementorA繼承Implementor并實(shí)現(xiàn)接口
class ConcreateImplementorA : public Implementor
{
public:
virtual void operationImp()
{
cout<<"A option"<<endl;
}
};
ConcreateImplementorB繼承Implementor并實(shí)現(xiàn)接口
class ConcreateImplementorB : public Implementor
{
public:
virtual void operationImp()
{
cout<<"B option"<<endl;
}
};
RefinedAbstraction繼承Abstraction實(shí)現(xiàn)接口,RefinedAbstraction可以調(diào)用自己的操作以及Abstraction的操作
class RefinedAbstraction : public Abstraction{
public:
RefinedAbstraction(Implementor* _imp):Abstraction(_imp){}
virtual void option()
{
imp->operationImp();
cout<<"RefinedAbstraction option"<<endl;
}
};
#include <iostream>
using namespace std;
int main(int argc, char const *argv[]) {
ConcreateImplementorA* cona = new ConcreateImplementorA();
RefinedAbstraction* rea = new RefinedAbstraction(cona);
rea->option();
return 0;
}