C++实现单例的5种方法总结 转载 https://blog.csdn.net/zztan/article/details/54691809

转载 https://blog.csdn.net/zztan/article/details/54691809

本文结合一些已有的C++ 单例模式 设计方法,总结出了5种实现方式,并指出其中的使用特点和注意事项;

参考引用的文章链接:

  ++ 几种单例模式的写法

https://segmentfault.com/q/1010000004157283


一般情况下,为了实现单例我们都会想到使用 static 成员,下面#1是最基本的方式;

#1 静态指针成员:

 
  1. class Singleton{    
  2.     private:    
  3.         Singleton(){};    
  4.         virtual ~Singleton(){};    
  5.     public:    
  6.         Singleton *Instance();    
  7.     protect:    
  8.         static Singleton *_instance;     
  9. };    
  10. //Singleton.h    
  11.     
  12.     
  13. Singleton *Singleton::instance()    
  14. {    
  15.     
  16.     if(NULL == _instance){    
  17.         _instance = new Singleton();    
  18.     }    
  19.     return _instance;    
  20. }    
  21. //Singleton.cpp    

构造时机: 运行时生成;

对象位置:   堆 

资源释放: new的单例对象,没有时机去释放;

线程安全: 否;  ---在单例构造过程可能重复,造成内存泄露;


#2 静态指针成员(改进型):

在#1的基础上解决存在的两个问题;

如果单例对象的构造实在运行时之前(也就是程序静态变量初始化时完成)就可以避免线程安全的问题;

  1. class Singleton{    
  2.     public:    
  3.         static Singleton *instance();    
  4.     private:    
  5.         Singleton();    
  6.         virtual ~Singleton();    
  7.         Singleton(const Singleton&){};    
  8.         Singleton& operator=(const Singleton&){};    
  9.     private:    
  10.         class CGarbo{    
  11.             public:    
  12.                 ~CGarbo()    
  13.                 {      
  14.                     if(Singleton::m_pInstance){    
  15.                         delete m_pInstance;    
  16.                     }      
  17.                 }      
  18.         };     
  19.     private:    
  20.         static Singleton *m_pInstance;    
  21.         static CGarbo Garbo;    
  22. };    
  23. //Singleton.h    
  24.     
  25.     
  26. Singleton::CGarbo Singleton::Garbo;    
  27. Singleton* Singleton::m_pInstance = new Singleton();    
  28.     
  29. Singleton::Singleton()    
  30. {    
  31.     printf("contructure funcation\n");    
  32. }    
  33.     
  34. Singleton::~Singleton()    
  35. {    
  36.     printf("deconstructure funcation\n");    
  37. }    
  38.     
  39. Singleton* Singleton::instance()    
  40. {    
  41.     return m_pInstance;    
  42. }    
  43. //Singleton.cpp  

构造时机: 初始化时生成;

对象位置:   堆 

资源释放: 通过一个成员的析构函数来释放单例对象,;  非常巧妙, 但是m_pInstance指针和 Garbo两个成员的析构是否有先后顺序,如果指针先被释放(指针变量变成NULL?)那么单例对象还是没有机会被释放;  ---找时间确认一下,然后更新一下这个结果;

线程安全: 是;  ---初始化过程,没有线程竞争;


对比以上两种方式,可以看出静态初始化的时候构造单例对象,能够比较好的解决线程安全的问题; 但是资源释放的需要通过曲线救国的方式来解决;

那么能不能把单例对象也生成在静态区呢?这样释放的问题就可以由操作系统自动完成;

#3 静态成员对象

  1. class Singleton{    
  2.     public:    
  3.         static Singleton *instance();    
  4.     private:    
  5.         Singleton();    
  6.         virtual ~Singleton();    
  7.         Singleton(const Singleton&){};    
  8.         Singleton& operator=(const Singleton&){};    
  9.     private:    
  10.         static Singleton m_instance;    
  11. };    
  12. //Singleton.h    
  13.     
  14. Singleton Singleton::m_instance;    
  15.     
  16. Singleton::Singleton()    
  17. {    
  18.     printf("contructure funcation\n");    
  19. }    
  20.     
  21. Singleton::~Singleton()    
  22. {    
  23.     printf("deconstructure funcation\n");    
  24. }    
  25.     
  26. Singleton *Singleton::instance()    
  27. {    
  28.     return &m_instance;    
  29. }    
  30. //Singleton.cpp    

构造时机: 初始化时生成;

对象位置:  静态区

资源释放: 在程序结束时 自动释放静态区的 成员变量

线程安全: 是;

注意: 静态成员的初始化需要放到类外完成;  那么是否可以把静态对象在成员函数instance()内部生成呢?

#4 静态局部对象

  1. class Singleton{    
  2.        public:    
  3.            static Singleton *instance();    
  4.        private:    
  5.            Singleton();    
  6.            virtual ~Singleton();    
  7.            Singleton(const Singleton&){};    
  8.            Singleton& operator=(const Singleton&){};    
  9.   
  10.    };    
  11.    //Singleton.h     
  12.        
  13.    Singleton::Singleton()    
  14.    {    
  15.        printf("contructure funcation\n");    
  16.    }    
  17.        
  18.    Singleton::~Singleton()    
  19.    {    
  20.        printf("deconstructure funcation\n");    
  21.    }    
  22.        
  23.    Singleton *Singleton::instance()    
  24.    {    
  25.        static Singleton ins;  
  26.        return &ins;    
  27.   
  28.    }    
  29.   
  30. //Singleton.cpp  


构造时机: 运行时生成; ---参考链接2 ;  --对于C++的局部类对象,是在函数第一次调用时生成;

对象位置:  静态区

资源释放: 在程序结束时 自动释放静态区的 成员变量
线程安全: 是(C++11);  ---参考链接2,, C++11标准针对局部静态对象的构造能保证线程安全(待查);

综合以上4中方式,单例对象的释放都是在程序结束时释放,

如果要求能够提供接口随时释放对象,那么就必须构造在堆上,然后提供显式的destroy接口;

#5 静态指针成员(动态释放)

  1. #ifndef __MUTEX_H__    
  2. #define __MUTEX_h__    
  3. #include <pthread.h>    
  4.     
  5. class Mutex{      
  6. private:    
  7.         pthread_mutex_t m_mutex;    
  8.     public:    
  9.         Mutex();    
  10.         virtual ~Mutex();    
  11.     private:    
  12.         Mutex(const Mutex&){};    
  13.         Mutex& operator=(const Mutex&){};    
  14.     public:    
  15.         int lock();    
  16.         int unlock();    
  17.         int trylock();    
  18.     
  19. };    
  20. #endif //__MUTEX_H__    
  21. //Mutex.h    
  22.     
  23.     
  24. #include "Mutex.h"    
  25.     
  26. Mutex::Mutex()    
  27. {    
  28.     pthread_mutex_init(&m_mutex, NULL);    
  29. }    
  30.     
  31. Mutex::~Mutex()    
  32. {    
  33.     pthread_mutex_destroy(&m_mutex);    
  34. }    
  35.     
  36. int Mutex::lock()    
  37. {    
  38.     return  pthread_mutex_lock(&m_mutex);    
  39. }    
  40.     
  41. int Mutex::unlock()    
  42. {    
  43.     return pthread_mutex_unlock(&m_mutex);    
  44. }    
  45.     
  46. int Mutex::trylock()    
  47. {    
  48.     return pthread_mutex_trylock(&m_mutex);    
  49. }    
  50. //Mutex.cpp    
  51.     
  52. //单例类    
  53. #ifndef __SINGLETON_H__    
  54. #defile __SINGLETON_H__    
  55. #include "Mutex.h"    
  56. class Singleton{    
  57.     
  58. public:    
  59.     static Singleton *instance();    
  60.     static void destroy();    
  61.     
  62. private:    
  63.     Singleton();    
  64.     virtual ~Singleton();    
  65.      Singleton(const Singleton&){};    
  66.      Singleton& operator=(const Singleton&){};    
  67.         
  68. private:    
  69.     static Singleton *m_pInstance;    
  70.     static Mutex m_Mutex;    
  71. };    
  72. #endif //__SINGLETON_H__    
  73. //Singleton.h    
  74.     
  75.     
  76. Singleton* Singleton::m_pInstance = NULL;    
  77. Mutex Singleton::m_Mutex;    
  78.     
  79. Singleton::Singleton()    
  80. {    
  81. //do something    
  82. }    
  83.     
  84. Singleton::~Singleton()    
  85. {    
  86. //do something    
  87. }    
  88.     
  89. Singleton* Singleton::instance()    
  90. {    
  91.      if(NULL == m_pInstance){    
  92.           m_Mutex.lock();    
  93.           if(NULL == m_pInstance){    
  94.                m_pInstance = new Singleton();    
  95.           }    
  96.           m_Mutex.unlock();    
  97.      }    
  98.      return m_pInstance;    
  99. }    
  100.     
  101. void Singleton::destroy()    
  102. {    
  103.      if(m_pInstance){    
  104.           m_Mutex.lock();    
  105.           if(m_pInstance){    
  106.                delete m_pInstance;    
  107.                m_pInstance = NULL;    
  108.           }    
  109.           m_Mutex.unlock();    
  110.      }    
  111. }    
  112. //Singleton.cpp    

当然 也可以吧 静态成员指针放到接口函数里面作为局部静态变量 和局部静态对象;

猜你喜欢

转载自blog.csdn.net/qq_34358193/article/details/80662713