c++——pimpl技法

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的手续,带来的好处是可扩展性和二进制兼容性。起到了防火墙的作用。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值