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
#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的更多相关文章
- Handle/Body pattern(Wrapper pattern)
Handle Body Pattern 一些设计模式,通过一系列非直接的间接的方式(这种间接的方式,可称其为 handle(把手)),完成接口与实现(实现可称为 body(主体))的分离 Handle ...
- Command and Query Responsibility Segregation (CQRS) Pattern 命令和查询职责分离(CQRS)模式
Segregate operations that read data from operations that update data by using separate interfaces. T ...
- 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 ...
- 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 ...
- Implementation with Java
Implementation with Java From:http://jcsc.sourceforge.net In general, follow the Sun coding conventi ...
- 转:Redis作者谈Redis应用场景
毫无疑问,Redis开创了一种新的数据存储思路,使用Redis,我们不用在面对功能单调的数据库时,把精力放在如何把大象放进冰箱这样的问题上,而是利用Redis灵活多变的数据结构和数据操作,为不同的大象 ...
- C++ Core Guidelines
C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...
- Core J2EE Patterns - Service Locator--oracle官网
原文地址:http://www.oracle.com/technetwork/java/servicelocator-137181.html Context Service lookup and cr ...
- 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 ...
随机推荐
- 计算数字出现的次数 Exercise07_03
import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:计算数字出现的次数 * */ public class Exercise0 ...
- Problem B: 调用函数,输出100到200之间的所有素数
#include <stdio.h> int isPrime(unsigned int n)//定义素数函数 { int i; || n == ) ; ; i * i <= n; i ...
- HashMap源码-Basic hash bin node
/** * Basic hash bin node, used for most entries. (See below for * TreeNode subclass, and in LinkedH ...
- Linux下使用Shell过滤重复文本(转)
ffffffffffffffffff ffffffffffffffffff eeeeeeeeeeeeeeeeeeee fffffffffffffffffff eeeeeeeeeeeeeeeeeeee ...
- android多线程-AsyncTask之工作原理深入解析(上)
关联文章: Android 多线程之HandlerThread 完全详解 Android 多线程之IntentService 完全详解 android多线程-AsyncTask之工作原理深入解析(上) ...
- 重新学习vue基础
1.创建vue实例 var vm = new Vue({ el: '#example', //选择元素 data: {a:1} //基本数据 }) 2.模板语法 (一)基本语法 <span> ...
- Tomcat环境下配置数据源
两种方式,图形化和字符型配置,图形化需要部署一个应用,字符型配置如下: 需要文件 mysql-connector-java-5.1.16-bin.jar Oracle需要classes12.jar文件 ...
- linux设备驱动:中断的实现
一.什么是中断 中断分两种: 1)中断,又叫外部中断或异步中断,它的产生是由于外设向处理器发出中断请求.其中外部中断也有两种,这是由配置寄存器设定的:普通中断请求(IRQ)和快速中断请求(FIQ).一 ...
- 【重点突破】—— Vue2.0 transition 动画Demo实践填坑
前言:vue1.0版本和2.0版本的过渡系统改变是很大的,具体请详看文档介绍.本文转载自郭锦荣的博客,一共列举了四种transition的使用实践,分别是css过渡.css动画.javascript钩 ...
- orchard project 本地化
http://orchardproject.net/localization 本地化 果园的本地化管理是托管在一个外部服务( Crowdin), 的项目.公众和贡献是受欢迎的! 如何做出贡献 注册上 ...