码迷,mamicode.com
首页 > 编程语言 > 详细

C++之PIMPL模式

时间:2018-06-18 22:15:09      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:title   信息隐藏   耦合   vat   imp   生成   .cpp   提高   list   

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

  

 

C++之PIMPL模式

标签:title   信息隐藏   耦合   vat   imp   生成   .cpp   提高   list   

原文地址:https://www.cnblogs.com/cthon/p/9196258.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!