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]--RadioGroup+RadioButton实现底部导航栏

    RadioGroup+RadioButton组合方式打造简单实用的底部导航栏 代码块: <?xml version="1.0" encoding="utf-8&qu ...

  2. RxJava 2.x 理解-2

    操作符总结: http://reactivex.io/documentation/operators.html https://github.com/ReactiveX/RxJava/wiki Ope ...

  3. node.js学习一——什么是node.js

    定义:node.js是运行在服务器端的运用了谷歌v8引擎的javascript运行平台 特点:1. 异步式I/O(非阻塞式I/O) 2. 事件驱动 什么是异步式I/O(非阻塞式I/O)? 要了解什么是 ...

  4. MORMOT数据库连接池

    MORMOT数据库连接池 MORMOT封装了一堆的PROPS控件,用于连接各种数据库. MORMOT的封装是武装到了牙齿的,这堆PROPS控件居然数据库连接池也封装好了.这就为我们省了不少事,笔者非常 ...

  5. [Android 4.4.4] 泛泰A850 三版通刷 Mokee4.4.4 KTU84P 20140626 RC2.2 by syhost

    RC2.1版地址: http://blog.csdn.net/syhost/article/details/34051923 2014.06.26 RC2.2 更新内容: 1 修复相机录像无声的bug ...

  6. 【C++ OpenGL ES 2.0编程笔记】8: 使用VBO和IBO绘制立方体 【转】

    http://blog.csdn.net/kesalin/article/details/8351935 前言 本文介绍了OpenGL ES 2.0 中的顶点缓冲对象(VBO: Vertex Buff ...

  7. Spark(九) -- SparkSQL API编程

    本文测试的Spark版本是1.3.1 Text文本文件测试 一个简单的person.txt文件内容为: JChubby,13 Looky,14 LL,15 分别是Name和Age 在Idea中新建Ob ...

  8. Mac上安装使用Nginx

    1.brew search nginx 2.brew install nginx 启动nginx ,sudo nginx ;访问localhost:8080 发现已出现nginx的欢迎页面了. 备注: ...

  9. C# 解决窗体假死的状态

    异步调用是CLR为开发者提供的一种重要的编程手段,它也是构建高性能.可伸缩应用程序的关键.在多核CPU越来越普及的今天,异步编程允许使用非常少的线程执行很多操作.我们通常使用异步完成许多计算型.IO型 ...

  10. pickle和cPickle:Python对象的序列化(上)

    https://segmentfault.com/a/1190000002493548 pickle模块实现了一种算法,将任意一个Python对象转化成一系列字节(byets).此过程也调用了seri ...