PIMPL(private implementation或者pointer to implementation)

来源:互联网 发布:条码检查软件系统 编辑:程序博客网 时间:2024/05/29 18:24

一.不引入PIMPL技法

1.引入更多的头文件,降低编译速度 (main.cpp中引入y.h中带x.h,y.h中带x.h,两次引入x.h)

2.提高模块的耦合度(编译期,运行期)
  [如果X类大小改变,Y依赖于X实现,Y类需要重新编译
   没有办法用到多态]
3.降低了接口的稳定程度
   对于库的使用,方法不能改变
   对于库的编译,动态库的变更,客户程序不用重新编译

   [Y依赖于X实现,X改变,Y类需要重新编译]                           

//file y.h#include "x.h"class Y{public:void Fun();private:X x_;};//file y.cpp#include "y.h"void Y::Fun{ return x_.Fun(); }//file main.cpp #include "y.h"int main(void ) {Y y;y.Fun();}

二.PIMPL

1.PIMPL(private implementation或者pointer to implementation)也称为handle/body idiom
2.PIMPL背后的思想是把客户与所有关于类的私有部分的知识隔离开。避免其它类知道其内部结构
3.降低编译依赖,提高重新编译速度
4.接口和实现隔离
5.降低模块的耦合度
  编译期
  运行期
6.提高了接口的稳定程度
   对于库的使用,方法不能改变
   对于库的编译,动态库的变更,客户程序不用重新编译

//file y.hclass X;class Y{public:void Fun();private:X x_;};//file y.cpp#include "y.h"#include "x.h"void Y::Fun{ return x_->Fun(); }//file main.cpp #include "y.h"int main(void ) {Y y;y.Fun();}


阅读全文
1 0
原创粉丝点击