c++ using Handle Class Pattern to accomplish implementation hiding

Reference material:

  • Thinking In C++ 2nd eidition chapter 5 section "Handle classes"

If there's something need to be hidden from clients of the class (such as an encryption algorithm, etc), you can use this pattern to hide the detail of your implementation (however, it can not be used on class templetes, see http://www.cnblogs.com/qrlozte/p/4108807.html). This trick is like the one used in C programming language to define a struct pointer of the implementation (See "C Programming: 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 
 3 typedef Encryption::byte byte;
 4 
 5 byte getByteFromSocket() {
 6     byte b = 0;
 7     // assign 'b' with a byte from the socket..
 8     return b;
 9 }
10 
11 void sendEncrpytedByte(const byte& encrpted) {
12     // send encrypted message...
13 }
14 
15 int main()
16 {
17     Encryption obj;
18     byte encypted = obj.encrypt(getByteFromSocket());
19     sendEncrpytedByte(encypted);
20     return 0;
21 }

Encryption.h

 1 #ifndef ENCRYPTION_H
 2 #define ENCRYPTION_H
 3 
 4 class EncryptionImpl; // Encryption Implementation
 5 
 6 class Encryption // Handle class
 7 {
 8     public:
 9         typedef unsigned char byte;
10         Encryption();
11         byte encrypt(const byte &src);
12         ~Encryption();
13 
14     private:
15         /*
16             Implementation (detail) of the algorithm is wrapped
17             in an incomplete type here, internal data structures
18             or helper classes or other functions needed to be
19             protect cannot be seen or used by clients even in
20             this header
21         */
22         EncryptionImpl *impl;
23 };
24 
25 #endif // ENCRYPTION_H

Encryption.cpp

 1 #include "Encryption.h"
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 class EncryptionImpl {
 8     public:
 9         /*
10             Internal data structures:
11 
12             Normally, they can be public for ease
13             of use, because the data structure
14             is totally under your control.
15             If you do need encapsulation for
16             some reason, then use it.
17         */
18 };
19 
20 class HelperClass1 {
21     // ...
22     public:
23         void dosomething() { cout << "HelperClass1 object is doing something.." << endl; }
24 };
25 
26 
27 class HelperClass2 {
28     // ...
29     public:
30         void dosomething() { cout << "HelperClass2 object is doing something.." << endl; }
31 };
32 
33 void helperFunction1() {
34     //...
35     cout << "helperFunction1 is doing something.." << endl;
36 }
37 
38 void helperFunction2() {
39     //...
40     cout << "helperFunction2 is doing something.." << endl;
41 }
42 
43 /**
44     do any initialization as you need
45 */
46 Encryption::Encryption(): impl(new EncryptionImpl())
47 {
48     // do any initialization as you need
49 }
50 
51 /**
52     do any cleanup as you need
53 */
54 Encryption::~Encryption()
55 {
56     // do any cleanup as you need
57     if (impl != NULL) delete impl;
58 }
59 
60 Encryption::byte Encryption::encrypt(const byte& src)
61 {
62     byte encrypted = src;
63     // algorithm detail...
64     HelperClass1 obj1;
65     HelperClass2 obj2;
66     obj1.dosomething();
67     obj2.dosomething();
68     helperFunction1();
69     helperFunction2();
70     // etc...
71     return encrypted;
72 }
原文地址:https://www.cnblogs.com/qrlozte/p/4107277.html