Bridge桥接模式1 C++理解的第一个版本
#include<iostream>
using namespace std;
class Implementor
{
public:
virtual void OperationImp() = 0;
};
class ConcreteImplementorA: public Implementor
{
public:
void OperationImp()
{
cout << "ConcreteImplementorA.OperationImp" << endl;
}
};
class ConcreteImplementorB: public Implementor
{
public:
void OperationImp()
{
cout << "ConcreteImplementorB.OperationImp" << endl;
}
};

class Abstraction
{
protected:
Implementor *pImp;
public:
virtual void Operation()
{
if (pImp)
pImp->OperationImp();
}
void SetImplementor(Implementor *p)
{
pImp = p;
}
};
class RefinedAbstraction: public Abstraction
{
public:
void Operation()
{
Abstraction::Operation();//类似Delphi中的Inherited;
cout << "Do some other thing
" << endl;
}
};
int main(int argc, char* argv[])
{
cout << "hello, 这个一个桥接模式的演示:" << endl; 
//RefinedAbstraction* pR = new RefinedAbstraction();
Abstraction* pAbstraction = new RefinedAbstraction();
Implementor *pImplementor;
pImplementor = new ConcreteImplementorA();
pAbstraction->SetImplementor(pImplementor);
pAbstraction->Operation();
delete pImplementor;
pImplementor = new ConcreteImplementorB();
pAbstraction->SetImplementor(pImplementor);
pAbstraction->Operation();
delete pImplementor;
return 0;
}

浙公网安备 33010602011771号