桥接模式

基本代码

#include <iostream>

using namespace std;

class Implementor
{
public:
    virtual void Operation() = 0;
};

class ConcreteImplementorA :public Implementor
{
    void Operation()
    {
        cout << "具体实现A的方法执行" << endl;
    }
};

class ConcreteImplementorB :public Implementor
{
    void Operation()
    {
        cout << "具体实现B的方法执行" << endl;
    }
};

class Abstraction
{
protected:
    Implementor* implementor;

public:
    void SetImplementor(Implementor* implementor_t)
    {
        implementor = implementor_t;
    }

    virtual void Operation()
    {
        implementor->Operation();
    }
};

class RefinedAbstraction :public Abstraction
{
    void Operation()
    {
        implementor->Operation();
    }
};



int main()
{
    Abstraction* ab = new RefinedAbstraction();

    ab->SetImplementor(new ConcreteImplementorA());
    ab->Operation();

    ab->SetImplementor(new ConcreteImplementorB());
    ab->Operation();


    system("pause");
    return 0;
}

手机设计结构图演变

12-17 18:03
查看更多