【设计模式】之十三Bridge

来源:互联网 发布:清理系统盘的软件 编辑:程序博客网 时间:2024/05/21 22:01

Type: Structural

Bridge: 将抽象与其实现解耦,这样两者可以独立地变化。

#include <iostream>using namespace std;class Implementor{public:    virtual void operationImpl() = 0;};class ConcreteImplementorA: public Implementor{public:    void operationImpl()    {        cout<<"ConcreteImplementorA operationImpl"<<endl;    };};class ConcreteImplementorB: public Implementor{public:    void operationImpl()    {        cout<<"ConcreteImplementorB operationImpl"<<endl;    };};class Abstraction{public:    void setImplementor(Implementor* pImplementor)    {        m_pImplementor = pImplementor;    };    void operation()    {        if(m_pImplementor != NULL)            m_pImplementor->operationImpl();    };private:    Implementor* m_pImplementor;};int main(){    ConcreteImplementorA *pImplA = new ConcreteImplementorA();    ConcreteImplementorB *pImplB = new ConcreteImplementorB();    Abstraction *pAbstraction = new Abstraction;    pAbstraction->setImplementor(pImplA);    pAbstraction->operation();    pAbstraction->setImplementor(pImplB);    pAbstraction->operation();    system("pause");    return 0;}

原创粉丝点击