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

 #include "Encryption.h"

 typedef Encryption::byte byte;

 byte getByteFromSocket() {
byte b = ;
// assign 'b' with a byte from the socket..
return b;
} void sendEncrpytedByte(const byte& encrpted) {
// send encrypted message...
} int main()
{
Encryption obj;
byte encypted = obj.encrypt(getByteFromSocket());
sendEncrpytedByte(encypted);
return ;
}

Encryption.h

 #ifndef ENCRYPTION_H
#define ENCRYPTION_H class EncryptionImpl; // Encryption Implementation class Encryption // Handle class
{
public:
typedef unsigned char byte;
Encryption();
byte encrypt(const byte &src);
~Encryption(); private:
/*
Implementation (detail) of the algorithm is wrapped
in an 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
*/
EncryptionImpl *impl;
}; #endif // ENCRYPTION_H

Encryption.cpp

 #include "Encryption.h"

 #include <iostream>

 using namespace std;

 class EncryptionImpl {
public:
/*
Internal data structures: Normally, they can be public for ease
of use, because the data structure
is totally under your control.
If you do need encapsulation for
some reason, then use it.
*/
}; class HelperClass1 {
// ...
public:
void dosomething() { cout << "HelperClass1 object is doing something.." << endl; }
}; class HelperClass2 {
// ...
public:
void dosomething() { cout << "HelperClass2 object is doing something.." << endl; }
}; void helperFunction1() {
//...
cout << "helperFunction1 is doing something.." << endl;
} void helperFunction2() {
//...
cout << "helperFunction2 is doing something.." << endl;
} /**
do any initialization as you need
*/
Encryption::Encryption(): impl(new EncryptionImpl())
{
// do any initialization as you need
} /**
do any cleanup as you need
*/
Encryption::~Encryption()
{
// do any cleanup as you need
if (impl != NULL) delete impl;
} Encryption::byte Encryption::encrypt(const byte& src)
{
byte encrypted = src;
// algorithm detail...
HelperClass1 obj1;
HelperClass2 obj2;
obj1.dosomething();
obj2.dosomething();
helperFunction1();
helperFunction2();
// etc...
return encrypted;
}

c++ using Handle Class Pattern to accomplish implementation hiding的更多相关文章

  1. Handle/Body pattern(Wrapper pattern)

    Handle Body Pattern 一些设计模式,通过一系列非直接的间接的方式(这种间接的方式,可称其为 handle(把手)),完成接口与实现(实现可称为 body(主体))的分离 Handle ...

  2. Command and Query Responsibility Segregation (CQRS) Pattern 命令和查询职责分离(CQRS)模式

    Segregate operations that read data from operations that update data by using separate interfaces. T ...

  3. The .NET weak event pattern in C#

    Introduction As you may know event handlers are a common source of memory leaks caused by the persis ...

  4. c++ why can't class template hide its implementation in cpp file?

    类似的问题还有: why can't class template use Handle Class Pattern to hide its implementation? || why there ...

  5. Implementation with Java

    Implementation with Java From:http://jcsc.sourceforge.net In general, follow the Sun coding conventi ...

  6. 转:Redis作者谈Redis应用场景

    毫无疑问,Redis开创了一种新的数据存储思路,使用Redis,我们不用在面对功能单调的数据库时,把精力放在如何把大象放进冰箱这样的问题上,而是利用Redis灵活多变的数据结构和数据操作,为不同的大象 ...

  7. C++ Core Guidelines

    C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...

  8. Core J2EE Patterns - Service Locator--oracle官网

    原文地址:http://www.oracle.com/technetwork/java/servicelocator-137181.html Context Service lookup and cr ...

  9. Multiple address space mapping technique for shared memory wherein a processor operates a fault handling routine upon a translator miss

    Virtual addresses from multiple address spaces are translated to real addresses in main memory by ge ...

随机推荐

  1. Android Studio 生成aar包,并非debug包,而是release包

    1.编写Module,作为library 下面是需要发布的aar包,上面的是随意的project 2.app依赖myLibrary 2.1 设置Project Structure 2.2 app依赖M ...

  2. 根文件系统及Busybox简介

    转:http://blog.csdn.net/wqc02/article/details/8930184 1.根文件系统简介...2 2.Busybox简介...2 2.1Busybox简介...2 ...

  3. mormot支持websocket

    mormot支持websocket 根据定义,HTTP连接是无状态的和单向的,也就是说,客户机向服务器发送一个请求,该服务器通过一个应答回复.没有客户端的预先请求,就没有办法让服务器发送消息给客户机. ...

  4. 北京极科极客科技有限公司 http://www.hiwifi.com/

    北京极科极客科技有限公司  http://www.hiwifi.com/ 产品:hiwifi   199元.

  5. [Git] git merge和rebase的区别

    git merge 会生成一个新得合并节点,而rebase不会 比如: D---E test / A---B---C---F master 使用merge合并, 为分支合并自动识别出最佳的同源合并点: ...

  6. javascript 递归函数调用(recursive funciton call)

    所谓的递归函数调用,就是自己调用自己的函数. var timerHandler = null; function a(){ console.log(123); timerHandler = setTi ...

  7. go get 下载的包放在哪里呢?

    有些问题,我以前都是似懂非懂,没有去弄个究竟!!!!! 这个习惯非常不好,搞得有些东西看似懂了,又不能百分之百说自己懂了,可能下次就弄不出来了,这样是不可取的. 不能有这种做事的风格. ------- ...

  8. 爪哇国新游记之二十二----排序判断重复时间复杂度为2n的位图法

    import java.util.ArrayList; import java.util.List; /** * 位图法 * 用于整型数组判重复,得到无重复列表 * */ public class B ...

  9. javascript - 你不容错过的es6模板写法

    /** * ``即重音符(128键盘左上角ESC下面那个键盘) * 隶属:模板字符串 */ let unit = '4'; let keywords = 'uc'; // step1:模板变量 ${v ...

  10. MySQL错误Another MySQL daemon already running with the same unix socket.v

    etc/init.d/mysqld start 结果显示 Another MySQL daemon already running with the same unix socket.显示另一个MyS ...