Reference Material:
- Thinking in C + + 2nd eidition Chapter 5 section "Handle Classes"
If there ' s something need to is hidden from clients of the class (such as a encryption algorithm, etc), you can use this Pattern to hide the detail of your implementation (however, it can is used on class templetes, see Http://www.cnblogs. com/qrlozte/p/4108807.html). This trick was like the one used in C programming language to define a struct pointer of the implementation (see "C Program Ming:a modern approach, Second Edition. Section 19.4 ").
As an example, see Code Snippets ' main.cpp ', ' Encryption.h ', ' Encryption.cpp '
Main.cpp
1#include"Encryption.h"2 3typedef encryption::byte byte;4 5 byteGetbytefromsocket () {6 byteb =0;7 //Assign ' B ' with a-byte from the socket.8 returnb;9 }Ten One voidSendencrpytedbyte (Const byte&encrpted) { A //Send encrypted message ... - } - the intMain () - { - encryption obj; - byteencypted =Obj.encrypt (Getbytefromsocket ()); + Sendencrpytedbyte (encypted); - return 0; +}
Encryption.h
1 #ifndef Encryption_h2 #defineEncryption_h3 4 classEncryptionimpl;//Encryption Implementation5 6 classEncryption//Handle class7 {8 Public:9typedef unsignedChar byte;Ten encryption (); One byteEncryptConst byte&src); A~encryption (); - - Private: the /* - implementation (detail) of the algorithm is wrapped - in a incomplete type here, internal data structures - or helper classes or other functions needed to be + protect cannot be seen or used by clients even in - This header + */ AEncryptionimpl *Impl; at }; - - #endif //Encryption_h
Encryption.cpp
1#include"Encryption.h"2 3#include <iostream>4 5 using namespacestd;6 7 classEncryptionimpl {8 Public:9 /*Ten Internal Data Structures: One A normally, they can be publicly for ease - of use, because the data structure - is totally under your control. the If You do need encapsulation for - some reason, then use it. - */ - }; + - classHelperClass1 { + // ... A Public: at voidDoSomething () {cout <<"HelperClass1 object is doing something."<<Endl;} - }; - - - classHelperClass2 { - // ... in Public: - voidDoSomething () {cout <<"HelperClass2 object is doing something."<<Endl;} to }; + - voidHelperFunction1 () { the //... *cout <<"HelperFunction1 is doing something."<<Endl; $ }Panax Notoginseng - voidHelperFunction2 () { the //... +cout <<"HelperFunction2 is doing something."<<Endl; A } the + /** - Do any initialization as need $ */ $Encryption::encryption (): Impl (NewEncryptionimpl ()) - { - //Do any initialization as need the } - Wuyi /** the Do any cleanup as need - */ Wuencryption::~encryption () - { About //Do any cleanup as need $ if(Impl! =NULL) Delete impl; - } - -Encryption::byteEncryption::encrypt (Const byte&src) A { + byteencrypted =src; the //Algorithm Detail ... - HelperClass1 obj1; $ HelperClass2 obj2; the obj1.dosomething (); the obj2.dosomething (); the HelperFunction1 (); the HelperFunction2 (); - // etc... in returnencrypted; the}
C + + using Handle Class Pattern to accomplish implementation hiding