设为首页 加入收藏

TOP

设计模式之桥接模式C++简单实现
2014-11-23 20:10:36 来源: 作者: 【 】 浏览:16
Tags:设计模式 桥接 模式 简单 实现

桥模式,其作用就是让抽象与实现相分离,让两者都能够各自变化

简单实现代码如下:

#include
using namespace std;
class Implementor
{
public:
	virtual void Operation()=0;
};
class ConcreteImplementorA:public Implementor
{
public:
	virtual void Operation()
	{
		cout<<"具体实现A的方法执行"<implementor=implementor;
	}
	virtual void Operation()=0;
	virtual ~Abstraction(){}
};
class RefinedAbstraction:public Abstraction
{
public:
	virtual void Operation()
	{
		implementor->Operation();
	}
	virtual ~RefinedAbstraction()
	{
		if(implementor)
			delete implementor;
	}
};
int main()
{
	Abstraction * ab=new RefinedAbstraction();
	ab->SetImplementor(new ConcreteImplementorA());
	ab->Operation();
	delete ab;
	ab=new RefinedAbstraction();
	ab->SetImplementor(new ConcreteImplementorB());
	ab->Operation();
	delete ab;
	return 0;
}

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇枚举+贪心 hdu-4334-Trouble 下一篇HDU 4616 Game 树形DP

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)
·MySQL 数据类型:从 (2025-12-26 18:20:03)
·Linux Shell脚本教程 (2025-12-26 17:51:10)
·Qt教程,Qt5编程入门 (2025-12-26 17:51:07)