1. 概念
pimpl(Private Implementation 或 Pointer to Implementation)是通过一个私有的成员指针,将指针所指向的类的内部实现数据进行隐藏。
2. 优点
1)降低模块的耦合;
2)降低编译依赖,提高编译速度;
3)接口与实现分离,提高接口的稳定性;
3. 实现原则
1. 暴露的接口里面不要有虚函数,要显式声明构造函数、析构函数,并且不能inline。
sizeof(Graphics) == sizeeof(Graphics::Impl*)
class Graphics
{
public:
Graphics();
~Graphics();
void drawLine(int x0, int y0, int x1, int y1);
void drawArc(int x, int y, int r);
private:
class Impl; //头文件只放声明
std::shared_ptr<Impl> impl = nullptr;
}
2. 在库的实现中把调用转发(forward)给实现(Graphics::Impl),这部分代码位于.so/.dll中,随库的升级一起变化。
#include <Graphics.h>
Graphics::Graphics()
{
impl = std::make_shared<Impl>();
}
Graphics::~Graphics()
{
}
void Graphics::drawLine(int x0, int y0, int x1, int y1)
{
if(nullptr != impl->get())
{
impl->get()->drawLine(x0, y0, x1, y1);
}
}
void Graphics::drawArc(int x, int y, int r)
{
if(nullptr != impl.get())
{
impl.get()->drawArc(x, y, r);
}
}
3. 如果要加入新的功能,不必通过继承来扩展,可以原地修改,且很容易保持二进制兼容性。
先动头文件:
class Graphics
{
public:
Graphics();
~Graphics();
void drawLine(int x0, int y0, int x1, int y1);
void drawLine(double x0, double y0, double x1, double y1); //add
void drawArc(int x, int y, int r);
void drawArc(double x, double y, double r); //add
private:
class Impl;
std::shared_ptr<Impl> impl = nullptr;
}
在实现的文件例增加forward,这么做不会破坏二进制兼容性,因为增加non-virtual函数不影响现有的可执行文件。
#include <Graphics.h>
Graphics::Graphics()
{
impl = std::make_shared<Impl>();
}
Graphics::~Graphics()
{
}
void Graphics::drawLine(int x0, int y0, int x1, int y1)
{
if(nullptr != impl->get())
{
impl->get()->drawLine(x0, y0, x1, y1);
}
}
void Graphics::drawLine(double x0, double y0, double x1, double y1)
{
if(nullptr != impl->get())
{
impl->get()->drawLine(x0, y0, x1, y1);
}
}
void Graphics::drawArc(int x, int y, int r)
{
if(nullptr != impl.get())
{
impl.get()->drawArc(x, y, r);
}
}
void Graphics::drawArc(double x, double y, double r)
{
if(nullptr != impl.get())
{
impl.get()->drawArc(x, y, r);
}
}
采用pimpl多了一道explicit forward的手续,带来的好处是可扩展性和二进制兼容性。起到了防火墙的作用。
|