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. hadoop FileSplit

    /** A section of an input file. Returned by {@link * InputFormat#getSplits(JobContext)} and passed t ...

  2. linux之touch命令修改文件的时间戳

    功能:对已经存在文件的时间进行修改,存取时间(access time).修改时间(modification time).对不存在的文件,进行创建新的空白文件. 语法:touch   [选项]  文件 ...

  3. winform 窗体实现增删改查(CRUD)窗体基类模式

    参考博客下方:http://www.cnblogs.com/wuhuacong/archive/2010/05/31/1748579.html 对于一般常用到的编辑数据.新增数据窗体,分开了两个不同的 ...

  4. 【Echarts】百度Echarts的使用入门+两个简单的小例子+心得

    Echarts对于展示结果,有一个很好的表达方式. 1.首先,在官网将js下载到本地,引用到页面上 这里是在开发环境,所以下载最后源代码这个 managerResult.jsp <%@ page ...

  5. What's New In DevTools (Chrome 59)来看看最新Chrome 59的开发者工具又有哪些新功能

    原文:https://developers.google.com/web/updates/2017/04/devtools-release-notes#command-menu 参考:https:// ...

  6. iptables配置

    iptables -F iptables -P INPUT DROP iptables -P FORWARD DROP iptables -A INPUT -m state --state ESTAB ...

  7. 表格中的IE BUG

    在表格应用了跨列单元格后,在IE6/7下当跨列单元格中的元素长度超过其跨列单元格中第一个单元格的宽度时会产生换行,如下所示: 解决方法: 1. 设置 table 的 'table-layout' 特性 ...

  8. Yii2系列教程四:实现用户注册,验证,登录

    上一篇写了一点点Yii2的数据库相关知识和强大的Gii,这一篇就如上一篇的最后所说的一样:在Yii2中实现用户的注册和登录. 你可以直接到Github下载源码,以便可以跟上进度,你也可以重头开始,一步 ...

  9. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何获取标准驱动器扭矩值获取电流值

    双击某个驱动器(以松下伺服驱动器为例),在Process Data中,注意默认显示了PDO mapping1的数据(Error code, status word等)   注意左侧,2和3分别表示了与 ...

  10. Memcachedclientutils类

    0.个人标签 我的CSDN博客地址: http://blog.csdn.net/caicongyang 1.依赖的jar包 commons-pool-1.5.6.jar slf4j-api-1.6.1 ...