C++设计模式编程中使用Bridge桥接模式的完全攻略
#ifndef _ABSTRACTION_H_
#define _ABSTRACTION_H_
class AbstractionImplement;
class Abstraction
{
public:
virtual void Operation()=0;//定义接口,表示该类所支持的操作
virtual ~Abstraction();
protected:
Abstraction();
};
class RefinedAbstractionA:public Abstraction
{
public:
RefinedAbstractionA(AbstractionImplement* imp);//构造函数
virtual void Operation();//实现接口
virtual ~RefinedAbstractionA();//析构函数
private:
AbstractionImplement* _imp;//私有成员
};
class RefinedAbstractionB:public Abstraction
{
public:
RefinedAbstractionB(AbstractionImplement* imp);//构造函数
virtual void Operation();//实现接口
virtual ~RefinedAbstractionB();//析构函数
private:
AbstractionImplement* _imp;//私有成员
};
#endif
Abstraction.cpp
#include "Abstraction.h"
#include "AbstractionImplement.h"
#include
using namespace std;
Abstraction::Abstraction()
{}
Abstraction::~Abstraction()
{}
RefinedAbstractionA::RefinedAbstractionA(AbstractionImplement* imp)
{
this->_imp = imp;
}
RefinedAbstractionA::~RefinedAbstractionA()
{
delete this->_imp;
this->_imp = NULL;
}
void RefinedAbstractionA::Operation()
{
cout << "RefinedAbstractionA::Operation" << endl;
this->_imp->Operation();
}
RefinedAbstractionB::RefinedAbstractionB(AbstractionImplement* imp)
{
this->_imp = imp;
}
RefinedAbstractionB::~RefinedAbstractionB()
{
delete this->_imp;
this->_imp = NULL;
}
void RefinedAbstractionB::Operation()
{
cout << "RefinedAbstractionB::Operation" << endl;
this->_imp->Operation();
}
AbstractImplement.h
#ifndef _ABSTRACTIONIMPLEMENT_H_
#define _ABSTRACTIONIMPLEMENT_H_
//抽象基类,定义了实现的接口
class AbstractionImplement
{
public:
virtual void Operation()=0;//定义操作接口
virtual ~AbstractionImplement();
protected:
AbstractionImplement();
};
// 继承自AbstractionImplement,是AbstractionImplement的不同实现之一
class ConcreteAbstractionImplementA:public AbstractionImplement
{
public:
ConcreteAbstractionImplementA();
void Operation();//实现操作
~ConcreteAbstractionImplementA();
protected:
};
// 继承自AbstractionImplement,是AbstractionImplement的不同实现之一
class ConcreteAbstractionImplementB:public AbstractionImplement
{
public:
ConcreteAbstractionImplementB();
void Operation();//实现操作
~ConcreteAbstractionImplementB();
protected:
};
#endif
AbstractImplement.cpp
#include "AbstractionImplement.h"
#include
using namespace std;
AbstractionImplement::AbstractionImplement()
{}
AbstractionImplement::~AbstractionImplement()
{}
ConcreteAbstractionImplementA::ConcreteAbstractionImplementA()
{}
ConcreteAbstractionImplementA::~ConcreteAbstractionImplementA()
{}
void ConcreteAbstractionImplementA::Operation()
{
cout << "ConcreteAbstractionImplementA Operation" << endl;
}
ConcreteAbstractionImplementB::ConcreteAbstractionImplementB()
{}
ConcreteAbstractionImplementB::~ConcreteAbstractionImplementB()
{}
void ConcreteAbstractionImplementB::Operation()
{
cout << "ConcreteAbstractionImplementB Operation" << endl;
}
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播