一直挺火的模式, 也称呼为桥接模式,记录一下:
Handle/body(桥接模式),handle 只有接口,body(handleImpl )是具体的实现。
handle 都不会变,要变的只有handleImpl ,被称为编译防火墙,这样有一层handle的接口,只需要改动impl,并且改动Impl的时候,不用额外编译handle.cpp。 另外一个好处主要是,用户不用包含那么多头文件,省的知道的接口太多,不好用。比如main只需要包含一个handle.hpp。
例如:
handle 类:
hpp:
class handleImpl //前置声明
class handle{
public:
notify();
private:
handleImpl* m_realImpl;
}
#include <handleImpl.hpp>
Handle::notify(){
m_realImpl->notify();
}
handleImpl 类:
hpp://包含其他的各种相关的实现类的头文件
#include<xx.h>
#include<yy.h>
class handleImpl{
public:
notify();
private:
xx m_xx;
yy m_yy;
}
#include<handleImpl.hpp>
handleImpl::notify(){
"1234"
m_xx.fun();
m_yy.fun();
}
main:
#include<handle.h>
int main(){
handle* myhandle = new handle();
myhandle->notify();
}
输出:
1234
xx
yy