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;
}
