设计模式——桥接模式(C++实现)

桥接模式

         将抽象部分与它的实现部分分离,使它们都可以独立的变化。属于结构型模式。通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦。

适用场景

       1、你不希望在抽象和它的实现部分之间有一个固定的绑定关系。 例如这种情况可能是因为,在程序运行时刻实现部分应可以被选择或者切换。
        2、类的抽象以及它的实现都应该可以通过生成子类的方法加以扩充。这时 Bridge模式使你可以对不同的抽象接口和实现部分进行组合,并分别对它们进行扩充。
        3、对一个抽象的实现部分的修改应对客户不产生影响,即客户的代码不必重新编译。
        4、(C++)你想对客户完全隐藏抽象的实现部分。在 C++中,类的表示在类接口中是可见的。
        5、 你想在多个对象间共享实现(可能使用引用计数),但同时要求客户并不知道这一点。
 

采用菜鸟教程的例子,这里用C++实现

#include<iostream>

using namespace std;

//创建桥接实现接口drawapi。
class drawapi
{
public:
	drawapi() {};
	virtual ~drawapi() {};
	virtual void drawcircle(int radius,int x,int y)=0;
	
};

//创建实现了DrawAPI接口的实体桥接实现类redcircle。
class redcircle:drawapi
{
public:
	redcircle() {};
	virtual ~redcircle() {};
	
	void drawcircle(int radius, int x, int y) { cout << "color: red " << "sadius: " << radius << " x: " << x << " y: " << y << endl; };
};

//创建实现了DrawAPI接口的实体桥接实现类greencircle。
class greencircle :drawapi
{
public:
	greencircle() {};
	virtual ~greencircle() {};

	void drawcircle(int radius, int x, int y) { cout << "color: green " << "sadius: " << radius << " x: " << x << " y: " << y << endl; };
};

//使用DrawAPI接口创建抽象类Shape。
class shape
{
public:
	shape() {};
	virtual ~shape() { delete mdrawapi; mdrawapi = NULL; };
	virtual void draw()=0;

protected:
	drawapi *mdrawapi;
};

//创建实现了Shape接口的实体类circle。
class circle :shape
{
public:
	circle(int tx, int ty, int tradius, drawapi *tdrawapi) { mdrawapi = tdrawapi; x = tx; y = ty; radius = tradius; };
	~circle() {};

	void draw() { mdrawapi->drawcircle(radius,x,y); };
private:
	int x;
	int y;
	int radius;
};

int main()
{
	shape *t1 = (shape*)new circle(100, 100, 10, (drawapi*)new redcircle());
	shape *t2 = (shape*)new circle(35, 74, 10, (drawapi*)new greencircle());

	t1->draw();
	t2->draw();

	delete t1;
	t1 = NULL;

	delete t2;
	t2 = NULL;

	return 0;
}

在visual studio 2015上运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
桥接模式是一种结构型设计模式,它将抽象和实现分离,使它们可以独立地变化。桥接模式的核心思想是将一个大类或一组类分解成抽象和实现两个独立的维度,使它们可以独立地变化和扩展,同时通过桥接来将它们连接起来。 在C++中,桥接模式通常通过虚函数实现。抽象部分通过基类定义接口,而实现部分通过派生类实现具体的功能。通过将抽象部分的指针作为参数传递给实现部分的函数,就可以实现两个部分的连接。 下面是一个简单的桥接模式的C++示例: ```c++ class Implementor { public: virtual void operation() = 0; virtual ~Implementor() {} }; class ConcreteImplementorA : public Implementor { public: void operation() override { // 具体的实现A } }; class ConcreteImplementorB : public Implementor { public: void operation() override { // 具体的实现B } }; class Abstraction { public: Abstraction(Implementor* implementor) : m_implementor(implementor) {} virtual void operation() = 0; virtual ~Abstraction() {} protected: Implementor* m_implementor; }; class RefinedAbstraction : public Abstraction { public: RefinedAbstraction(Implementor* implementor) : Abstraction(implementor) {} void operation() override { m_implementor->operation(); // 其他操作 } }; int main() { Implementor* implementorA = new ConcreteImplementorA(); Implementor* implementorB = new ConcreteImplementorB(); Abstraction* abstractionA = new RefinedAbstraction(implementorA); Abstraction* abstractionB = new RefinedAbstraction(implementorB); abstractionA->operation(); abstractionB->operation(); delete abstractionA; delete abstractionB; delete implementorA; delete implementorB; return 0; } ``` 在上面的示例中,Implementor是实现部分的抽象基类,ConcreteImplementorA和ConcreteImplementorB是具体的实现类。Abstraction是抽象部分的基类,RefinedAbstraction是抽象部分的具体实现类。在main函数中,我们创建了不同的Implementor和Abstraction对象,并通过它们来完成不同的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值