您的位置:首页 > 其它

【结构型】Bridge模式

2016-05-25 12:05 375 查看
桥接模式是为了将对象的抽象与实现分离,使得它们可以独立变化。简简单单的一句话,却已经是站在了更高抽象层面上来看待、设计、解决问题。平常我们多是对具体问题进行分析、抽象,然后就开始设计,这对多数情况下基本完全够用,毕竟实际项目中的功能模块都是找一“最优解的"实现来解决掉问题,把功能设计出来即可。这种情况下的结构关系图参考如下:

namespace bridge
{
class IImpl;
class ITarget
{
public:
virtual void doSomething() = 0;
// some code here........

protected:
IImpl* getImpl() { return _impl; }

private:
IImpl* _impl;

};//class ITarget

class ConcreteTarget : public ITarget
{
public:
virtual void doSomething() override {
auto pImpl = this->getImpl();
if (nullptr != pImpl) {
pImpl->realDoSomething();
}
// some other code here........
}

};//class ConcreteTarget

class IImpl
{
public:
virtual void realDoSomething() = 0;

};//class IImpl

class ConcreteImpl1 : public IImpl
{
public:
virtual void realDoSomething() override { /* some code here........ */ }

};//class ConcreteImpl1

class ConcreteImpl2 : public IImpl
{
public:
virtual void realDoSomething() override { /* some code here........ */ }

};//class ConcreteImpl2

}//namespace bridge


Bridge模式编码结构参考
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: