bridge(桥模式)
bridge.png基类Implementor,定义一些操作接口
class Implementor
{
public:
virtual void operationImp()=0;
~Implementor(){}
};
基类Abstraction有指向Implementor的多态指针,以及自己的操作接口
class Abstraction
{
public:
virtual void option()=0;
~Abstraction(){}
Abstraction(Implementor* _imp):imp(_imp){}
protected:
Implementor* imp;
};
ConcreateImplementorA继承Implementor并实现接口
class ConcreateImplementorA : public Implementor
{
public:
virtual void operationImp()
{
cout<<"A option"<<endl;
}
};
ConcreateImplementorB继承Implementor并实现接口
class ConcreateImplementorB : public Implementor
{
public:
virtual void operationImp()
{
cout<<"B option"<<endl;
}
};
RefinedAbstraction继承Abstraction实现接口,RefinedAbstraction可以调用自己的操作以及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;
}
bridge.png
网友评论