设计模式--Pimpl(pointer to implementation 指向实现的指针)
来源:互联网 发布:淘宝小号怎样申请 编辑:程序博客网 时间:2024/06/05 20:58
0x01 缘由
以前学习了一些设计模式,但是实践中使用得较少。用得比较多的是单列模式、代理模式等,这些模式我在其他博文中也有讲解。为什么这些模式没有使用,难道是真的用不到? 我应该不是。现在重新学习设计模式:设计模式是针对软件设计问题的通用解决方案。从基本的原理、优缺点、什么场景下使用等方面考虑和思考。
设计模式是前辈们提炼和总结的一些设计思路,所以掌握它有利无弊。
0x02 介绍
使用Pimpl 惯用法将实现细节从公有头文件中分离出来。当你想给客户设计一个良好的API接口时,你会将大量的实现隐藏,提供给客户的是非常明了的API。
0x03 优缺点
优点:
信息隐藏。看下面的例子就可以发现。
降低耦合。头文件的耦合,导致头文件要包含较多文件;
加速编译。
更好的二进制兼容性。
惰性分配。
缺点:
难调试、难阅读;
增加对象的创建和分配的开销;
0x04 实现
头文件:
#ifndef AUTOTIMER_H_#define AUTOTIMER_H_#include <string.h>class AutoTimer{ public: explicit AutoTimer(const std::string name); ~AutoTimer(); private: class Imple; Imple * mimpl;};#endif /* AUTOTIMER_H_ */
实现:
#include "autotimer.h"#include <iostream>#if _WIN32#include <windows.h>#else#include <sys/time.h>#endifclass AutoTimer::Imple{ public: double GetElapsed() const { #ifdef _WIN32 return (GetTickCount() - mSmStartTime) / 1e3; #else struct timeval mEndTime; gettimeofday(&mEndTime, NULL); double t1 = mStartTime.tv_usec/1e6 + mStartTime.tv_sec ; double t2 = mEndTime.tv_usec/1e6 + mEndTime.tv_sec ; return t2 - t1; #endif } std::string mName; #ifdef _WIN32 DWORD mStartTime; #else struct timeval mStartTime; #endif};AutoTimer::AutoTimer(const std::string name):mImpl(new AutoTimer::Imple){ mimpl->mName = name; #ifdef _WIN32 mimpl->mStartTime = GetTickCount(); #else gettimeofday(&mimpl->mStartTime, NULL); #endif}AutoTimer::~AutoTimer(){ std::cout << mimpl->mName << ": took" << mimpl->GetElapsed() << "secs" << std::endl; delete mimpl; mimpl = NULL;}
0x05 使用场景
给第三方提高API接口。内部各负责模块间提供API接口。
0x06 总结
学习到了一种设计对外API的思路。
阅读全文
0 0
- 设计模式--Pimpl(pointer to implementation 指向实现的指针)
- PIMPL实现String(pointer to implementation)
- PIMPL(private implementation或者pointer to implementation)
- 减少编译时间:pointer to implementation(Impl设计模式)
- 常量指针(const pointer)和指向常量的指针(pointer to const)
- 设计模式(五)桥接模式Bridge == Pointer To Implementation
- 实现私有化(Pimpl) --- QT常见的设计模式
- 084.Pointer Pointer 指向指针的指针
- 084.Pointer Pointer 指向指针的指针
- C++指针(Pointer)的理解(指向指针的指针、指向引用的指针)
- 指向函数的指针 ------ 函数指针(function pointer)
- c++11 条款22:当使用Pimpl(指向实现的指针)时,在实现文件里定义特定的成员函数
- const Pointers and Pointer to const Objects 常量指针和指向常量对象的指针
- C++实现设计模式: PIMPL IDIOM
- 071.Pointer Array 指向数组的指针
- 079.Pointer Function 指向函数的指针
- 079.Pointer Function 指向函数的指针
- 071.Pointer Array 指向数组的指针
- python笔记 11-01-2017 kmeans +random
- js实现图片上传
- js url传值为中文的时候,页面接收乱码
- JavaScript
- 第7章 成员资格、授权(Authorize、ASP.NET Identity、OAuth和OpenID的外部登录)和安全性
- 设计模式--Pimpl(pointer to implementation 指向实现的指针)
- spring表单标签提交返回400错误问题
- Java.util.Math类的API记录
- 获取url参数
- SpringBoot 中文手册 --Part IV --32 消息
- 20171031|课堂作业02 方法参数相关博文
- java.lang.NoSuchMethodError 错误的原因及解决办法
- Android不加载获取网络、本地图片的尺寸
- MTK 充电逻辑总结