设计模式5--bridge(桥模式)

2018-02-14  本文已影响0人  _codelover

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
上一篇下一篇

猜你喜欢

热点阅读