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 ...
随机推荐
- Java高级架构师(一)第17节:X-gen生成所需的DAO部分模板
- ctags 小记
转:http://www.cnblogs.com/napoleon_liu/archive/2011/01/23/1942738.html 简介 ctags − Generate tag files ...
- 【spring boot】spring boot中使用定时任务配置
spring boot中使用定时任务配置 =============================================================================== ...
- HashMap深度解析(一)
HashMap可以说是Java中最常用的集合类框架之一,是Java语言中非常典型的数据结构,我们总会在不经意间用到它,很大程度上方便了我们日常 开发.在很多Java的笔试题中也会被问到,最常见的,“H ...
- PostgreSQL配置文件--WAL
3 WAL WRITE AHEAD LOG 3.1 Settings 3.1.1 fsync 字符串 默认: fsync = on 开启后强制把数据同步更新到磁盘,可以保证数据库将在OS或者硬件崩溃的 ...
- Simple JavaScript Inheritance(John Resig)
I’ve been doing a lot of work, lately, with JavaScript inheritance – namely for my work-in-progress ...
- CSS3 transform变换
CSS3 transform变换 1.translate(x,y) 设置盒子位移2.scale(x,y) 设置盒子缩放3.rotate(deg) 设置盒子旋转4.skew(x-angle,y-angl ...
- http://blog.sina.com.cn/s/blog_62e1faba010147k4.html
http://blog.sina.com.cn/s/blog_62e1faba010147k4.html
- 算法导论-散列表(Hash Table)-大量数据快速查找算法
目录 引言 直接寻址 散列寻址 散列函数 除法散列 乘法散列 全域散列 完全散列 碰撞处理方法 链表法 开放寻址法 线性探查 二次探查 双重散列 随机散列 再散列问题 完整源码(C++) 参考资料 内 ...
- [C++]豆知识(1条)
术语: ctor:constructor,构造函数 dtor:destructor,析构函数 构造函数/析构函数 如果基类要利用多态,则dtor需要声明为virtual,这样在销毁对象时才可以正确调用 ...