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. (转)MoMo的Unity3D研究院之Assetbundle的原理(六十一)

    http://www.xuanyusong.com/archives/2373 Assetbundle 是Unity Pro提供提供的功能,它可以把多个游戏对象或者资源二进制文件封装到Assetbun ...

  2. Linux虚拟机小问题解决方法系列

    1)使用虚拟机的过程中,会碰到虚拟机占用的空间越来越大的情况,即使删除了虚拟机里的文件,磁盘空间还是似乎还是没有释放,使用“vmware-vdiskmanager”工具解决.解决方法在这里:参考.合并 ...

  3. 关于Hadoop_env.sh中的HADOOP_CLASSPATH

    之前博客里介绍了如何自定义DoubleArrayWritable,并将该类型的value写入SequenceFile文件中,为了能够使用命令查看这个文件中的内容(果然坑都是一步一步给自己挖的)参考了网 ...

  4. Why DNS Based Global Server Load Balancing (GSLB) Doesn’t Work

    Why DNS Based Global Server Load Balancing (GSLB) Doesn't Work    

  5. 使用.Net中的WeakDictionary — ConditionalWeakTable

    有的时候,我们需要给某些数据添加一些附加信息,一种常用的做法是使用一个Dictionary在填充这些附加信息如: var data = new Data();    var tag = new Tag ...

  6. P2P通信标准协议(二)之TURN

    上一篇P2P通信标准协议(一)介绍了在NAT上进行端口绑定的通用规则,应用程序可以根据这个协议来设计网络以外的通信. 但是,STUN/RFC5389协议里能处理的也只有市面上大多数的Cone NAT( ...

  7. MathType插入空格

    公式太长,换行后加一些空格,继续录. 将鼠标定位到需要插入空格的位置,此时如果直接按空格键,你会发现并不能插入空格.正确的输入方法有两种: 方法一,在菜单栏中[样式]菜单下选择[文本],随后按空格键即 ...

  8. Matlab与神经网络入门

    第一节.神经网络基本原理  1. 人工神经元( Artificial Neuron )模型  人工神经元是神经网络的基本元素,其原理可以用下图表示: 图1. 人工神经元模型 图中x1~xn是从其他神经 ...

  9. Word如何设置单元格垂直居中

    那两个上面是水平居中,下面是垂直居中.

  10. 引用日志log4net.dll的web.config配置

    <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigu ...