C++ handle classes

C++中,使用handle classes(句柄类)的目的,是为了降低文件之间的编译依存关系。

摘自网上的一段话:

       需要句柄类的背景: 

1)在对安全要求很高的领域,即使核心实现已经封闭在库中不可见,但头文件中变量定义仍可能曝露一些内部信息

2)在设计初期、实现部分会经常变动,甚至头文件中变量定义也需要经常变动,因此在重编译的时候头文件也需要编译,有时候导致编译时间过长。 句柄类就是为了解决这类问题

比如,我有一个类Person,在很多地方都用到了这个类(#incude Person.h),在程序不断完善的过程中,难免会对该类进行重新设计或者一些修改。当Person.h被很多文件包含时,即使我们只是对Person类做了很小的改动,那么所有包含该头文件的文件,都需要重新编译,这会花费大量的无谓的时间,我想这是每一个编程人员都不希望看到的,为了解决此类问题的发生,我们便想到了句柄类。

     设计思想:

            我们把Person类设计成一个 handle class (句柄类),而重新定义一个新类 PersonImpl,作为具体的实现类,当然原本Person类中的成员变量,和成员函数的具体实现都放在PersonImpl类中。在Person类中定义一个指向PersonImpl类的指针,用于调用具体的实现。这样一来,当实现修改了,我们只需要重新编译Person文件即可。

具体代码如下:

[cpp]  view plain copy
  1. //Person.h  
  2. //the handle class  
  3. //定义  
  4. #include <string>  
  5. #include <memory>  
  6. class PersonImpl;  
  7. class Person  
  8. {  
  9. public:  
  10.     Person(const std::string& name,const int &id,const std::string& addr);  
  11.     std::string rename() const;  
  12.     int reid() const;  
  13.     std::string readdr() const;  
  14. private:  
  15.     std::tr1::shared_ptr<PersonImpl> pImpl; //指针,指向具体的完成实现工作的类 PersonImpl  
  16.       
  17. };  
  18.   
  19. //Person.cpp  
  20. //the handle class  
  21. //实现  
  22. #include "Person.h"  
  23. #include "PersonImpl.h" //必须这样做,否则无法调用其成员函数,  
  24.                         //注意,PersonImpl有着和句柄类Person完全相同的成员函数,2者接口完全相同  
  25.                            
  26. Person::Person(const std::string& name,const int &id,const std::string& addr)  
  27.     :pImpl(new PersonImpl(name,id,addr))  
  28. {  
  29. }  
  30. std::string Person::rename() const  
  31. {  
  32.     return pImpl->rename(); //通过指针调用实现类里的成员函数,完成具体动作  
  33. }  
  34. int Person::reid() const  
  35. {  
  36.     return pImpl->reid();  
  37. }  
  38. std::string Person::readdr() const  
  39. {  
  40.     return pImpl->readdr();  
  41. }  
  42. //PersonImpl.h  
  43. //the implement class  
  44. //定义  
  45. #include <string>  
  46. #include <memory>  
  47. class PersonImpl  
  48. {  
  49. public:  
  50.     PersonImpl(const std::string& name,const int &id,const std::string& addr);  
  51.     std::string rename() const;  
  52.     int reid() const;  
  53.     std::string readdr() const;  
  54. private:  
  55.     std::string theName;  
  56.     int theId;  
  57.     std::string theAddress;  
  58.       
  59. };  
  60.   
  61. //PersonImpl.cpp  
  62. //the implement class  
  63. //实现  
  64. #include "PersonImpl.h"  
  65. PersonImpl::PersonImpl(const std::string& name,const int &id,const std::string& addr)  
  66.     :theName(name),theId(id),theAddress(addr)  
  67. {  
  68. }  
  69. std::string PersonImpl::rename() const  
  70. {  
  71.     return theName;  
  72. }  
  73. int PersonImpl::reid() const  
  74. {  
  75.     return theId;  
  76. }  
  77. std::string PersonImpl::readdr() const  
  78. {  
  79.     return theAddress;  
  80. }  
  81. //Main.cpp  
  82. #include <iostream>  
  83. #include "Person.h"  
  84. //主程序,只需要包含Person类,因为Person类的接口永远不会改变,如果实现文件PersonImpl中的接口或者成员改变了  
  85. //只需要重新编译Person.h,而不需要重新编译包含person.h的文件了  
  86. int main()  
  87. {  
  88.     Person man1("Tom",10,"England");  
  89.     std::cout<<"the name:"<<man1.rename()<<std::endl;  
  90.     std::cout<<"the id:"<<man1.reid()<<std::endl;  
  91.     std::cout<<"the Address:"<<man1.readdr()<<std::endl;  
  92.     system("pause");  
  93.     return 0;  
  94. }  
 

转自:

C++中,使用handle classes(句柄类)的目的,是为了降低文件之间的编译依存关系。


摘自网上的一段话:

       需要句柄类的背景: 

1)在对安全要求很高的领域,即使核心实现已经封闭在库中不可见,但头文件中变量定义仍可能曝露一些内部信息

2)在设计初期、实现部分会经常变动,甚至头文件中变量定义也需要经常变动,因此在重编译的时候头文件也需要编译,有时候导致编译时间过长。 句柄类就是为了解决这类问题


比如,我有一个类Person,在很多地方都用到了这个类(#incude Person.h),在程序不断完善的过程中,难免会对该类进行重新设计或者一些修改。当Person.h被很多文件包含时,即使我们只是对Person类做了很小的改动,那么所有包含该头文件的文件,都需要重新编译,这会花费大量的无谓的时间,我想这是每一个编程人员都不希望看到的,为了解决此类问题的发生,我们便想到了句柄类。

     设计思想:

            我们把Person类设计成一个 handle class (句柄类),而重新定义一个新类 PersonImpl,作为具体的实现类,当然原本Person类中的成员变量,和成员函数的具体实现都放在PersonImpl类中。在Person类中定义一个指向PersonImpl类的指针,用于调用具体的实现。这样一来,当实现修改了,我们只需要重新编译Person文件即可。

 

 


 

 

具体代码如下:

 

[cpp]  view plain copy
  1. //Person.h  
  2. //the handle class  
  3. //定义  
  4. #include <string>  
  5. #include <memory>  
  6. class PersonImpl;  
  7. class Person  
  8. {  
  9. public:  
  10.     Person(const std::string& name,const int &id,const std::string& addr);  
  11.     std::string rename() const;  
  12.     int reid() const;  
  13.     std::string readdr() const;  
  14. private:  
  15.     std::tr1::shared_ptr<PersonImpl> pImpl; //指针,指向具体的完成实现工作的类 PersonImpl  
  16.       
  17. };  
  18.   
  19. //Person.cpp  
  20. //the handle class  
  21. //实现  
  22. #include "Person.h"  
  23. #include "PersonImpl.h" //必须这样做,否则无法调用其成员函数,  
  24.                         //注意,PersonImpl有着和句柄类Person完全相同的成员函数,2者接口完全相同  
  25.                            
  26. Person::Person(const std::string& name,const int &id,const std::string& addr)  
  27.     :pImpl(new PersonImpl(name,id,addr))  
  28. {  
  29. }  
  30. std::string Person::rename() const  
  31. {  
  32.     return pImpl->rename(); //通过指针调用实现类里的成员函数,完成具体动作  
  33. }  
  34. int Person::reid() const  
  35. {  
  36.     return pImpl->reid();  
  37. }  
  38. std::string Person::readdr() const  
  39. {  
  40.     return pImpl->readdr();  
  41. }  
  42. //PersonImpl.h  
  43. //the implement class  
  44. //定义  
  45. #include <string>  
  46. #include <memory>  
  47. class PersonImpl  
  48. {  
  49. public:  
  50.     PersonImpl(const std::string& name,const int &id,const std::string& addr);  
  51.     std::string rename() const;  
  52.     int reid() const;  
  53.     std::string readdr() const;  
  54. private:  
  55.     std::string theName;  
  56.     int theId;  
  57.     std::string theAddress;  
  58.       
  59. };  
  60.   
  61. //PersonImpl.cpp  
  62. //the implement class  
  63. //实现  
  64. #include "PersonImpl.h"  
  65. PersonImpl::PersonImpl(const std::string& name,const int &id,const std::string& addr)  
  66.     :theName(name),theId(id),theAddress(addr)  
  67. {  
  68. }  
  69. std::string PersonImpl::rename() const  
  70. {  
  71.     return theName;  
  72. }  
  73. int PersonImpl::reid() const  
  74. {  
  75.     return theId;  
  76. }  
  77. std::string PersonImpl::readdr() const  
  78. {  
  79.     return theAddress;  
  80. }  
  81. //Main.cpp  
  82. #include <iostream>  
  83. #include "Person.h"  
  84. //主程序,只需要包含Person类,因为Person类的接口永远不会改变,如果实现文件PersonImpl中的接口或者成员改变了  
  85. //只需要重新编译Person.h,而不需要重新编译包含person.h的文件了  
  86. int main()  
  87. {  
  88.     Person man1("Tom",10,"England");  
  89.     std::cout<<"the name:"<<man1.rename()<<std::endl;  
  90.     std::cout<<"the id:"<<man1.reid()<<std::endl;  
  91.     std::cout<<"the Address:"<<man1.readdr()<<std::endl;  
  92.     system("pause");  
  93.     return 0;  
  94. }  
 


http://blog.csdn.net/zicheng_lin/article/details/6317271

C++中,使用handle classes(句柄类)的目的,是为了降低文件之间的编译依存关系。


摘自网上的一段话:

       需要句柄类的背景: 

1)在对安全要求很高的领域,即使核心实现已经封闭在库中不可见,但头文件中变量定义仍可能曝露一些内部信息

2)在设计初期、实现部分会经常变动,甚至头文件中变量定义也需要经常变动,因此在重编译的时候头文件也需要编译,有时候导致编译时间过长。 句柄类就是为了解决这类问题


比如,我有一个类Person,在很多地方都用到了这个类(#incude Person.h),在程序不断完善的过程中,难免会对该类进行重新设计或者一些修改。当Person.h被很多文件包含时,即使我们只是对Person类做了很小的改动,那么所有包含该头文件的文件,都需要重新编译,这会花费大量的无谓的时间,我想这是每一个编程人员都不希望看到的,为了解决此类问题的发生,我们便想到了句柄类。

     设计思想:

            我们把Person类设计成一个 handle class (句柄类),而重新定义一个新类 PersonImpl,作为具体的实现类,当然原本Person类中的成员变量,和成员函数的具体实现都放在PersonImpl类中。在Person类中定义一个指向PersonImpl类的指针,用于调用具体的实现。这样一来,当实现修改了,我们只需要重新编译Person文件即可。

 

 


 

 

具体代码如下:

 

[cpp]  view plain copy
  1. //Person.h  
  2. //the handle class  
  3. //定义  
  4. #include <string>  
  5. #include <memory>  
  6. class PersonImpl;  
  7. class Person  
  8. {  
  9. public:  
  10.     Person(const std::string& name,const int &id,const std::string& addr);  
  11.     std::string rename() const;  
  12.     int reid() const;  
  13.     std::string readdr() const;  
  14. private:  
  15.     std::tr1::shared_ptr<PersonImpl> pImpl; //指针,指向具体的完成实现工作的类 PersonImpl  
  16.       
  17. };  
  18.   
  19. //Person.cpp  
  20. //the handle class  
  21. //实现  
  22. #include "Person.h"  
  23. #include "PersonImpl.h" //必须这样做,否则无法调用其成员函数,  
  24.                         //注意,PersonImpl有着和句柄类Person完全相同的成员函数,2者接口完全相同  
  25.                            
  26. Person::Person(const std::string& name,const int &id,const std::string& addr)  
  27.     :pImpl(new PersonImpl(name,id,addr))  
  28. {  
  29. }  
  30. std::string Person::rename() const  
  31. {  
  32.     return pImpl->rename(); //通过指针调用实现类里的成员函数,完成具体动作  
  33. }  
  34. int Person::reid() const  
  35. {  
  36.     return pImpl->reid();  
  37. }  
  38. std::string Person::readdr() const  
  39. {  
  40.     return pImpl->readdr();  
  41. }  
  42. //PersonImpl.h  
  43. //the implement class  
  44. //定义  
  45. #include <string>  
  46. #include <memory>  
  47. class PersonImpl  
  48. {  
  49. public:  
  50.     PersonImpl(const std::string& name,const int &id,const std::string& addr);  
  51.     std::string rename() const;  
  52.     int reid() const;  
  53.     std::string readdr() const;  
  54. private:  
  55.     std::string theName;  
  56.     int theId;  
  57.     std::string theAddress;  
  58.       
  59. };  
  60.   
  61. //PersonImpl.cpp  
  62. //the implement class  
  63. //实现  
  64. #include "PersonImpl.h"  
  65. PersonImpl::PersonImpl(const std::string& name,const int &id,const std::string& addr)  
  66.     :theName(name),theId(id),theAddress(addr)  
  67. {  
  68. }  
  69. std::string PersonImpl::rename() const  
  70. {  
  71.     return theName;  
  72. }  
  73. int PersonImpl::reid() const  
  74. {  
  75.     return theId;  
  76. }  
  77. std::string PersonImpl::readdr() const  
  78. {  
  79.     return theAddress;  
  80. }  
  81. //Main.cpp  
  82. #include <iostream>  
  83. #include "Person.h"  
  84. //主程序,只需要包含Person类,因为Person类的接口永远不会改变,如果实现文件PersonImpl中的接口或者成员改变了  
  85. //只需要重新编译Person.h,而不需要重新编译包含person.h的文件了  
  86. int main()  
  87. {  
  88.     Person man1("Tom",10,"England");  
  89.     std::cout<<"the name:"<<man1.rename()<<std::endl;  
  90.     std::cout<<"the id:"<<man1.reid()<<std::endl;  
  91.     std::cout<<"the Address:"<<man1.readdr()<<std::endl;  
  92.     system("pause");  
  93.     return 0;  
  94. }  
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值