关于学习 《深入应用c++11》的代码笔记:

c++11之前是这么实现的

  1. template<typename T>
  2. class Singleton{
  3. public:
  4. static T* Instance(){
  5. if (m_pInstance == nullptr)
  6. m_pInstance = new T();
  7. return m_pInstance;
  8. }
  9.  
  10. template<typename T0>
  11. static T* Instance(T0 arg0){
  12. if (m_pInstance == nullptr)
  13. m_pInstance = new T(arg0);
  14. return m_pInstance;
  15. }
  16.  
  17. template<typename T0,typename T1>
  18. static T* Instance(T0 arg0, T1 arg1){
  19. if (m_pInstance == nullptr)
  20. m_pInstance = new T(arg0, arg1);
  21. return m_pInstance;
  22. }
  23.  
  24. template<typename T0, typename T1,typename T2>
  25. static T* Instance(T0 arg0, T1 arg1,T2 arg2){
  26. if (m_pInstance == nullptr)
  27. m_pInstance = new T(arg0, arg1,arg2);
  28. return m_pInstance;
  29. }
  30.  
  31. template<typename T0, typename T1, typename T2,typename T3>
  32. static T* Instance(T0 arg0, T1 arg1, T2 arg2,T3 arg3){
  33. if (m_pInstance == nullptr)
  34. m_pInstance = new T(arg0, arg1, arg2,arg3);
  35. return m_pInstance;
  36. }
  37.  
  38. template<typename T0, typename T1, typename T2, typename T3,typename T4>
  39. static T* Instance(T0 arg0, T1 arg1, T2 arg2, T3 arg3,T4 arg4){
  40. if (m_pInstance == nullptr)
  41. m_pInstance = new T(arg0, arg1, arg2, arg3,arg4);
  42. return m_pInstance;
  43. }
  44.  
  45. template<typename T0, typename T1, typename T2, typename T3, typename T4,typename T5>
  46. static T* Instance(T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4,T5 arg5){
  47. if (m_pInstance == nullptr)
  48. m_pInstance = new T(arg0, arg1, arg2, arg3, arg4,arg5);
  49. return m_pInstance;
  50. }
  51.  
  52. static T* GetInstance()
  53. {
  54. if (m_pInstance == nullptr)
  55. throw std::logic_error("the instance is not init,please init the instance first");
  56.  
  57. return m_pInstance;
  58. }
  59.  
  60. static void DestroyInstance(){
  61. delete m_pInstance;
  62. m_pInstance = nullptr;
  63. }
  64.  
  65. private:
  66. Singleton(void);
  67. virtual ~Singleton(void);
  68. Singleton(const Singleton&);
  69. Singleton& operator = (const Singleton);
  70.  
  71. static T* m_pInstance;
  72. };
  73.  
  74. template<class T> T* Singleton<T>::m_pInstance = nullptr;
  75.  
  76. //============================================
  77. struct A{
  78. A(){}
  79. };
  80.  
  81. struct B{
  82. B(int x){}
  83. };
  84.  
  85. struct C{
  86. C(int x, double y){}
  87. };
  88.  
  89. int _tmain(int argc, _TCHAR* argv[])
  90. {
  91. Singleton<A>::Instance();
  92. Singleton<A>::Instance();
  93. Singleton<B>::Instance(1);
  94. Singleton<C>::Instance(1,3.14);
  95.  
  96. Singleton<A>::DestroyInstance();
  97. Singleton<B>::DestroyInstance();
  98. Singleton<C>::DestroyInstance();
  99.  
  100. return 0;
  101. }

  c++11之后可以简略一点,使用了可变模板参数

  1. template<typename T>
  2. class Singleton{
  3. public:
  4. template <typename... Args>
  5. static T* Instance(Args&&... args){
  6. if (m_pInstance == nullptr)
  7. m_pInstance = new T(std::forward<Args>(args)...);
  8. return m_pInstance;
  9. }
  10.  
  11. static T* GetInstance(){
  12. if (m_pInstance == nullptr)
  13. throw std::logic_error("the instance is not init,please initialize the instance first");
  14. return m_pInstance;
  15. }
  16.  
  17. static void DestroyInstance()
  18. {
  19. delete m_pInstance;
  20. m_pInstance = nullptr;
  21. }
  22.  
  23. private:
  24. Singleton(void);
  25. virtual ~Singleton(void);
  26. Singleton(const Singleton&);
  27. Singleton& operator=(const Singleton&);
  28. private:
  29. static T* m_pInstance;
  30. };
  31.  
  32. template<class T>T* Singleton<T>::m_pInstance = nullptr;
  33.  
  34. #include <iostream>
  35. #include <string>
  36.  
  37. using namespace std;
  38.  
  39. struct A{
  40. A(const string&){ cout << "lvalue" << endl; }
  41. A(string&&x){ cout << "rvalue" << endl; }
  42. };
  43.  
  44. struct B{
  45. B(const string&){ cout << "lvalue" << endl; }
  46. B(string&& x){ cout << "rvalue" << endl; }
  47. };
  48.  
  49. struct C{
  50. C(int x, double y){}
  51. void Fun(){ cout << "Test" << endl; }
  52. };
  53.  
  54. int _tmain(int argc, _TCHAR* argv[])
  55. {
  56. string str = "bb";
  57. Singleton<A>::Instance(str);
  58. Singleton<B>::Instance(std::move(str));
  59. Singleton<C>::Instance(1,3.14);
  60. Singleton<C>::GetInstance()->Fun();
  61.  
  62. Singleton<A>::DestroyInstance();
  63. Singleton<B>::DestroyInstance();
  64. Singleton<C>::DestroyInstance();
  65.  
  66. return 0;
  67. }

  

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. C#中的Attribute详解(下)

    原文地址:https://blog.csdn.net/xiaouncle/article/details/70229119 C#中的Attribute详解(下) 一.Attribute本质 从上篇里我 ...

  2. python数据结构之链表(一)

    数据结构是计算机科学必须掌握的一门学问,之前很多的教材都是用C语言实现链表,因为c有指针,可以很方便的控制内存,很方便就实现链表,其他的语言,则没那么方便,有很多都是用模拟链表,不过这次,我不是用模拟 ...

  3. VCSA 6.5 升级 VCSA 6.7

    VCSA 6.7已于4月17日正式发布,文件名为VMware-VCSA-all-6.7.0-8217866.iso,国内百度网盘已有下载链接,请自行搜索. 下载后解压,运行\VMware-VCSA-a ...

  4. LeaderF常用用法

    常用: 搜索当前目录下的文件 :LeaderfFile <leader>f 搜索当前的Buffer :LeaderfBuffer <leader>b 搜索最近使用过的文件( s ...

  5. 红帽yum源安装报错initscripts-9.49.41-1.el7.x86_64 conflicts redhat-release &lt; 7.5-0.11" ?

    https://access.redhat.com/solutions/3425111 环境 Red Hat Enterprise Linux 7 问题 yum fails to apply upda ...

  6. 关于消息队列的使用----ActiveMQ,RabbitMQ,ZeroMQ,Kafka,MetaMQ,RocketMQ

    一.消息队列概述消息队列中间件是分布式系统中重要的组件,主要解决应用解耦,异步消息,流量削锋等问题,实现高性能,高可用,可伸缩和最终一致性架构.目前使用较多的消息队列有ActiveMQ,RabbitM ...

  7. 表格(table)

    Title 主机名 端口 操作 1111 10023 查看详情 修改 表头1 表头1 表头1 表头1 1 1 1 1 1 1 1 1 1 <!DOCTYPE html><html l ...

  8. 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 ...

  9. Gulp的安装与配置

    http://blog.csdn.net/itlsx/article/details/49981459

  10. kafka相关资料

    先来说一下Kafka与RabbitMQ的对比: RabbitMQ,遵循AMQP协议,由内在高并发的erlanng语言开发,用在实时的对可靠性要求比较高的消息传递上. kafka是Linkedin于20 ...