关于学习 《深入应用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模式的更多相关文章

  1. (原创)c++11改进我们的模式之改进访问者模式

    本次讲c++11改进我们的模式之改进访问者模式 访问者模式是GOF23个设计模式中比较复杂的模式之一,但是它的功能也很强大,非常适合稳定的继承层次中对象的访问,可以在不修改被访问对象的情况下,动态添加 ...

  2. (原创)c++11改进我们的模式之改进命令模式

    模式虽然精妙,却难完美,比如观察者模式中观察者生命周期的问题:比如访问者模式中循环依赖的问题等等:其它很多模式也存在这样那样的一些不足之处,如使用场景受限.实现复杂.不够简洁.不够通用等.但我觉得不足 ...

  3. (原创)c++11改进我们的模式之改进单例模式

    我会写关于c++11的一个系列的文章,会讲到如何使用c++11改进我们的程序,本次讲如何改进我们的模式,会讲到如何改进单例模式.观察者模式.访问者模式.工厂模式.命令模式等模式.通过c++11的改进, ...

  4. (原创)c++11改进我们的模式之改进代理模式,实现通用的AOP框架

    c++11 boost技术交流群:296561497,欢迎大家来交流技术. 本次要讲的时候如何改进代理模式,具体来说是动态代理模式,动态代理模式一般实现AOP框架,不懂AOP的童鞋看这里.我前面的博文 ...

  5. 1.设计模式 - Singleton模式(单件模式)

    Singleton是一种创建型模式,指某个类采用Singleton模式,则在这个类被创建后,只可能产生一个实例供外部访问,并且提供一个全局的访问点,一般用于Activity的控制层全局对象和Singl ...

  6. (原创)c++11改进我们的模式之改进表驱动模式

    所谓表驱动法(Table-Driven Approach),简单讲是指用查表的方法获取值.表驱动是将一些通过较为复杂逻辑语句来得到数据信息的方式,通过查询表的方式来实现,将数据信息存放在表里.对于消除 ...

  7. (原创)c++11改进我们的模式之改进观察者模式

    和单例模式面临的是同样的问题,主题更新的接口难以统一,很难做出一个通用的观察者模式,还是用到可变模板参数解决这个问题,其次还用到了右值引用,避免多余的内存移动.c++11版本的观察者模式支持注册的观察 ...

  8. 转:Singleton模式

    C++完美实现Singleton模式  转自:http://www.cppblog.com/dyj057/archive/2005/09/20/346.html boost库的Singleton的实现 ...

  9. Java设计模式(2)单态模式(Singleton模式)

    定义:Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在. 在很多操作中,比如建立目录 数据库连接都需要这样的单线程操作. 还有,singleton能够被状态化 ...

随机推荐

  1. uva-784-水题-搜索

    题意:从*点开始,标记所有能走到的点,X代表墙,下划线原样输出 AC:40ms #include<stdio.h> #include<iostream> #include< ...

  2. Windows系统下Eclipse上搭建Python开发环境

    参考网站:   https://blog.csdn.net/zhangphil/article/details/78962159 1.先安装JDK 和python,参考网站:https://www.c ...

  3. hive 下篇

    由于spark on hive 问题,导致无法插入数据,暂时使用spark进行hive操作 向分区表插入数据 hive> show partitions customers;OKpartitio ...

  4. 26. 天马tomcat授权文件的影响因素

    问题:Tianma版本同一台机器的Server端序列号有时会显示空白 描述:同一台机器的tomcat,tianma版本序列号为空(如图) 解决:手动删除ABS_DOCUMENT\LiveBos目录下s ...

  5. JAVA NIO学习记录2-非阻塞式网络通信

    一.阻塞与非阻塞 传统的IO 流都是阻塞式的.也就是说,当一个线程调用read() 或write() 时,该线程被阻塞,直到有一些数据被读取或写入,该线程在此期间不能执行其他任务.因此,在完成网络通信 ...

  6. cmake编译参数

    进入源码目录后 cmake . -LH  --查看之前编译参数 ccmake .  使用cmake gui界面进行查看和编辑编译用到的参数 (推荐) 源码目录下 INSTALL-SOURCE 文件后面 ...

  7. XML文件的写,集合XML序列化(写)。XML文件的读,递归遍历

    XML文件:必须要有一个节点.检验xml文件,可以用浏览器打开,能打开表示对,否则错. 处理方法: DOM:XmlDocument文档对象模型 Sax(事件驱动,XmlReader) XmlSeria ...

  8. Ansiable Manage MySQL global variables

    mysql_variables - Manage MySQL global variables New in version 1.3. Synopsis Requirements (on host t ...

  9. MySQL+Navicat for MySQL安装

    一.安装MySQL 1.下载MySQL http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.10-winx64.zip 2.安装 2.1解压安装包 ...

  10. Redis的简介

    Redis 简介 Redis 是一个高性能的key-value数据库.支持复杂的数据结构,支持持久化,支持主从集群,支持高可用,支持较大的value存储... Redis是一个nosql,非关系型数据 ...