c++11 改进设计模式 Singleton模式
关于学习 《深入应用c++11》的代码笔记:
c++11之前是这么实现的
- template<typename T>
- class Singleton{
- public:
- static T* Instance(){
- if (m_pInstance == nullptr)
- m_pInstance = new T();
- return m_pInstance;
- }
- template<typename T0>
- static T* Instance(T0 arg0){
- if (m_pInstance == nullptr)
- m_pInstance = new T(arg0);
- return m_pInstance;
- }
- template<typename T0,typename T1>
- static T* Instance(T0 arg0, T1 arg1){
- if (m_pInstance == nullptr)
- m_pInstance = new T(arg0, arg1);
- return m_pInstance;
- }
- template<typename T0, typename T1,typename T2>
- static T* Instance(T0 arg0, T1 arg1,T2 arg2){
- if (m_pInstance == nullptr)
- m_pInstance = new T(arg0, arg1,arg2);
- return m_pInstance;
- }
- template<typename T0, typename T1, typename T2,typename T3>
- static T* Instance(T0 arg0, T1 arg1, T2 arg2,T3 arg3){
- if (m_pInstance == nullptr)
- m_pInstance = new T(arg0, arg1, arg2,arg3);
- return m_pInstance;
- }
- template<typename T0, typename T1, typename T2, typename T3,typename T4>
- static T* Instance(T0 arg0, T1 arg1, T2 arg2, T3 arg3,T4 arg4){
- if (m_pInstance == nullptr)
- m_pInstance = new T(arg0, arg1, arg2, arg3,arg4);
- return m_pInstance;
- }
- template<typename T0, typename T1, typename T2, typename T3, typename T4,typename T5>
- static T* Instance(T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4,T5 arg5){
- if (m_pInstance == nullptr)
- m_pInstance = new T(arg0, arg1, arg2, arg3, arg4,arg5);
- return m_pInstance;
- }
- static T* GetInstance()
- {
- if (m_pInstance == nullptr)
- throw std::logic_error("the instance is not init,please init the instance first");
- return m_pInstance;
- }
- static void DestroyInstance(){
- delete m_pInstance;
- m_pInstance = nullptr;
- }
- private:
- Singleton(void);
- virtual ~Singleton(void);
- Singleton(const Singleton&);
- Singleton& operator = (const Singleton);
- static T* m_pInstance;
- };
- template<class T> T* Singleton<T>::m_pInstance = nullptr;
- //============================================
- struct A{
- A(){}
- };
- struct B{
- B(int x){}
- };
- struct C{
- C(int x, double y){}
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- Singleton<A>::Instance();
- Singleton<A>::Instance();
- Singleton<B>::Instance(1);
- Singleton<C>::Instance(1,3.14);
- Singleton<A>::DestroyInstance();
- Singleton<B>::DestroyInstance();
- Singleton<C>::DestroyInstance();
- return 0;
- }
c++11之后可以简略一点,使用了可变模板参数
- template<typename T>
- class Singleton{
- public:
- template <typename... Args>
- static T* Instance(Args&&... args){
- if (m_pInstance == nullptr)
- m_pInstance = new T(std::forward<Args>(args)...);
- return m_pInstance;
- }
- static T* GetInstance(){
- if (m_pInstance == nullptr)
- throw std::logic_error("the instance is not init,please initialize the instance first");
- return m_pInstance;
- }
- static void DestroyInstance()
- {
- delete m_pInstance;
- m_pInstance = nullptr;
- }
- private:
- Singleton(void);
- virtual ~Singleton(void);
- Singleton(const Singleton&);
- Singleton& operator=(const Singleton&);
- private:
- static T* m_pInstance;
- };
- template<class T>T* Singleton<T>::m_pInstance = nullptr;
- #include <iostream>
- #include <string>
- using namespace std;
- struct A{
- A(const string&){ cout << "lvalue" << endl; }
- A(string&&x){ cout << "rvalue" << endl; }
- };
- struct B{
- B(const string&){ cout << "lvalue" << endl; }
- B(string&& x){ cout << "rvalue" << endl; }
- };
- struct C{
- C(int x, double y){}
- void Fun(){ cout << "Test" << endl; }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- string str = "bb";
- Singleton<A>::Instance(str);
- Singleton<B>::Instance(std::move(str));
- Singleton<C>::Instance(1,3.14);
- Singleton<C>::GetInstance()->Fun();
- Singleton<A>::DestroyInstance();
- Singleton<B>::DestroyInstance();
- Singleton<C>::DestroyInstance();
- return 0;
- }
c++11 改进设计模式 Singleton模式的更多相关文章
- (原创)c++11改进我们的模式之改进访问者模式
本次讲c++11改进我们的模式之改进访问者模式 访问者模式是GOF23个设计模式中比较复杂的模式之一,但是它的功能也很强大,非常适合稳定的继承层次中对象的访问,可以在不修改被访问对象的情况下,动态添加 ...
- (原创)c++11改进我们的模式之改进命令模式
模式虽然精妙,却难完美,比如观察者模式中观察者生命周期的问题:比如访问者模式中循环依赖的问题等等:其它很多模式也存在这样那样的一些不足之处,如使用场景受限.实现复杂.不够简洁.不够通用等.但我觉得不足 ...
- (原创)c++11改进我们的模式之改进单例模式
我会写关于c++11的一个系列的文章,会讲到如何使用c++11改进我们的程序,本次讲如何改进我们的模式,会讲到如何改进单例模式.观察者模式.访问者模式.工厂模式.命令模式等模式.通过c++11的改进, ...
- (原创)c++11改进我们的模式之改进代理模式,实现通用的AOP框架
c++11 boost技术交流群:296561497,欢迎大家来交流技术. 本次要讲的时候如何改进代理模式,具体来说是动态代理模式,动态代理模式一般实现AOP框架,不懂AOP的童鞋看这里.我前面的博文 ...
- 1.设计模式 - Singleton模式(单件模式)
Singleton是一种创建型模式,指某个类采用Singleton模式,则在这个类被创建后,只可能产生一个实例供外部访问,并且提供一个全局的访问点,一般用于Activity的控制层全局对象和Singl ...
- (原创)c++11改进我们的模式之改进表驱动模式
所谓表驱动法(Table-Driven Approach),简单讲是指用查表的方法获取值.表驱动是将一些通过较为复杂逻辑语句来得到数据信息的方式,通过查询表的方式来实现,将数据信息存放在表里.对于消除 ...
- (原创)c++11改进我们的模式之改进观察者模式
和单例模式面临的是同样的问题,主题更新的接口难以统一,很难做出一个通用的观察者模式,还是用到可变模板参数解决这个问题,其次还用到了右值引用,避免多余的内存移动.c++11版本的观察者模式支持注册的观察 ...
- 转:Singleton模式
C++完美实现Singleton模式 转自:http://www.cppblog.com/dyj057/archive/2005/09/20/346.html boost库的Singleton的实现 ...
- Java设计模式(2)单态模式(Singleton模式)
定义:Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在. 在很多操作中,比如建立目录 数据库连接都需要这样的单线程操作. 还有,singleton能够被状态化 ...
随机推荐
- C#中的Attribute详解(下)
原文地址:https://blog.csdn.net/xiaouncle/article/details/70229119 C#中的Attribute详解(下) 一.Attribute本质 从上篇里我 ...
- python数据结构之链表(一)
数据结构是计算机科学必须掌握的一门学问,之前很多的教材都是用C语言实现链表,因为c有指针,可以很方便的控制内存,很方便就实现链表,其他的语言,则没那么方便,有很多都是用模拟链表,不过这次,我不是用模拟 ...
- VCSA 6.5 升级 VCSA 6.7
VCSA 6.7已于4月17日正式发布,文件名为VMware-VCSA-all-6.7.0-8217866.iso,国内百度网盘已有下载链接,请自行搜索. 下载后解压,运行\VMware-VCSA-a ...
- LeaderF常用用法
常用: 搜索当前目录下的文件 :LeaderfFile <leader>f 搜索当前的Buffer :LeaderfBuffer <leader>b 搜索最近使用过的文件( s ...
- 红帽yum源安装报错initscripts-9.49.41-1.el7.x86_64 conflicts redhat-release < 7.5-0.11" ?
https://access.redhat.com/solutions/3425111 环境 Red Hat Enterprise Linux 7 问题 yum fails to apply upda ...
- 关于消息队列的使用----ActiveMQ,RabbitMQ,ZeroMQ,Kafka,MetaMQ,RocketMQ
一.消息队列概述消息队列中间件是分布式系统中重要的组件,主要解决应用解耦,异步消息,流量削锋等问题,实现高性能,高可用,可伸缩和最终一致性架构.目前使用较多的消息队列有ActiveMQ,RabbitM ...
- 表格(table)
Title 主机名 端口 操作 1111 10023 查看详情 修改 表头1 表头1 表头1 表头1 1 1 1 1 1 1 1 1 1 <!DOCTYPE html><html l ...
- as3 三行三列 布满9个为一个界面
var n:int=int(iconIndex/3); e.x =(int(n/3)*3+iconIndex%3)*557; e.y = int(iconIndex / 3) % 3 * 260; i ...
- Gulp的安装与配置
http://blog.csdn.net/itlsx/article/details/49981459
- kafka相关资料
先来说一下Kafka与RabbitMQ的对比: RabbitMQ,遵循AMQP协议,由内在高并发的erlanng语言开发,用在实时的对可靠性要求比较高的消息传递上. kafka是Linkedin于20 ...