C++设计模式之七:Bridge(桥接器)

一、意图:

将抽象部分与实现部分分离,增加灵活性;

二、类图:


C++设计模式之七:Bridge(桥接器)_第1张图片

三、组成元素:

Abstraction:抽象类接口;

RefinedAbstraction:扩展Abstraction的接口;

Implementor:实现类接口;

ConcreteImplementor:具体实现类;

四、代码实现;

#include "iostream"

using namespace std;

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

class ConcreteImplementorA:public Implementor 
{
public:
	void Operation()
	{
		cout<<"ConcreteImplementorA!"<<endl;
	}
	
};

class Abstraction
{
private:
	Implementor* p_Imp;
public:
	Abstraction(Implementor* imp)
	{
		p_Imp=imp;
	}
	void Operation()
	{
		p_Imp->Operation();
	}
};
class RefinedAbstraction:public Abstraction
{
public:
	void Operation()
	{
		
	}
};

class ConcreteImplementorB:public Implementor 
{
public:
	void Operation()
	{
		cout<<"ConcreteImplementorB!"<<endl;
	}
	
};

void main()
{
	Implementor* ima=new ConcreteImplementorA();
	Implementor* imb=new ConcreteImplementorB();

	Abstraction* abs1=new Abstraction(ima);
	Abstraction* abs2=new Abstraction(imb);

	abs1->Operation();
	abs2->Operation();
}


你可能感兴趣的:(C++设计模式之七:Bridge(桥接器))