C++之PIMPL模式

C++之PIMPL模式

1 PIMPL解释

PIMPL(Private Implementation 或 Pointer to Implementation)是通过一个私有的成员指针,将指针所指向的类的内部实现数据进行隐藏。

2 PIMPL优点

举例:

//x.h
class X
{
public:
    void Fun();
private:
    int i; //add int i;
};

//c.h
#include <x.h>
class C
{
public:
    void Fun();
private:
    X x; //与X的强耦合
};

PIMPL做法:
//c.h
class X; //代替#include <x.h>
class C
{
public:
    void Fun();
private:
    X *pImpl; //pimpl
};

1)降低模块的耦合。因为隐藏了类的实现,被隐藏的类相当于原类不可见,对隐藏的类进行修改,不需要重新编译原类。
2)降低编译依赖,提高编译速度。指针的大小为(32位)或8(64位),X发生变化,指针大小却不会改变,文件c.h也不需要重编译。
3)接口与实现分离,提高接口的稳定性。

1、通过指针封装,当定义“new C”或"C c1"时 ,编译器生成的代码中不会掺杂X的任何信息。
2、当使用C时,使用的是C的接口(C接口里面操作的类其实是pImpl成员指向的X对象),与X无关,X被通过指针封装彻底的与实现分离。
//c.cpp
C::C()pImpl(new X())
{
}

C::~C()
{
     delete pImpl;
     pImpl = NULL;
}

void C::Fun()
{
    pImpl->Fun();
}

//main
#include <c.h>
int main()
{
    C c1;
    c1.Fun();
    return 0;
}

实例代码:

nestcalss.h

#ifndef __LINE_H__
#define __LINE_H__
 
 
//设计模式: PIMPL
//1. 实现信息隐藏
//2. 减小编译依赖, 可以用最小的代价平滑的升级库文件,
//3. 接口与实现进行解耦
 
class Line
{
public:
    Line(int,int,int,int);
    ~Line();
  
    void printLine() const;
private:
    class LineImpl;
private:
    LineImpl * _pimpl;
};
 
 
#endif

nestclass.cc

#include "nestClass.h"
#include <math.h>
 
#include <iostream>
using std::cout;
using std::endl;
 
class Line::LineImpl
{
    class Point
    {
    public:
        Point(int ix = 0, int iy = 0)
        : _ix(ix)
        , _iy(iy)
        {
            cout << "Point(int=0, int=0)" << endl;
        }
 
        void print() const
        {
            cout << "(" << _ix
                 << "," << _iy
                 << ")";
        }
 
    private:
        int _ix;
        int _iy;
    };
public:
    LineImpl(int x1, int y1, int x2, int y2)
    : _p1(x1, y1)
    , _p2(x2, y2)
    {
        cout << "LineImpl(int,int,int,int)" << endl;
    }
 
    ~LineImpl() {   cout << "~LineImpl()" << endl;  }
 
    void printLine() const;
private:
    Point _p1;
    Point _p2;
};
 
void Line::LineImpl::printLine() const
{
    _p1.print();
    cout << " --> ";
    _p2.print();
    cout << endl;
}
 
Line::Line(int x1, int y1, int x2, int y2)
: _pimpl(new LineImpl(x1, y1, x2, y2))
{
    cout << "Line(int,int,int,int)" << endl;
}
 
Line::~Line()
{
    delete _pimpl;
    cout << "~Line()" << endl;
}
 
void Line::printLine() const
{
    _pimpl->printLine();
}

main.cc

#include "nestClass.h"
#include <iostream>
using std::cout;
using std::endl;
  
int main(void)
{
    Line line(1, 2, 3, 4);
    line.printLine();
 
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值