1. 程式人生 > >C++設計模式-單例模式

C++設計模式-單例模式

con git god www light nullptr return post gpo

版權聲明:若無來源註明,Techie亮博客文章均為原創。 轉載請以鏈接形式標明本文標題和地址:
本文標題:C++設計模式-單例模式 本文地址:http://techieliang.com/2017/12/772/ 文章目錄
  • 1. 介紹
  • 2. 範例
  •  2.1. 單線程
  •  2.2. 多線程

1. 介紹

幾個重點:

  • 構造函數為private
  • 提供一個獲取單例對象指針的函數
  • 一個靜態指針成員存儲單例對象

註意:

  • 獲取單例對象也可以獲取對象引用,但要註意拷貝構造函數和賦值運算符
  • 如果有多線程訪問單例,需要註意線程同步

2. 範例

源碼GitHub:CppDesignPattern

2.1. 單線程

  1. #ifndef SIGLETON_H
  2. #define SIGLETON_H
  3. /**
  4. * @brief 非線程安全單例,無多線程時使用
  5. */
  6. class Singleton {
  7. public:
  8. /**
  9. * @brief 單例模式,獲取實例化對象
  10. * @param 無
  11. * @return 單例對象
  12. */
  13. static Singleton *GetInstance();
  14. /**
  15. * @brief 單例模式,主動銷毀實例化對象
  16. * @param 無
  17. * @return 無
  18. */
  19. static void DestoryInstance();
  20. private:
  21. /**
  22. * @brief 構造函數
  23. */
  24. Singleton();
  25. /**
  26. * @brief 單例模式在程序結束時自動刪除單例的方法
  27. */
  28. class SingletonDel {
  29. public:
  30. ~SingletonDel() {
  31. if (m_instance != NULL) {
  32. delete m_instance;
  33. m_instance = NULL;
  34. }
  35. }
  36. };
  37. static SingletonDel m_singleton_del;///程序結束時銷毀
  38. static Singleton *m_instance; //單例對象指針
  39. };
  40. #endif // SIGLETON_H
  41. //cpp
  42. #include "sigleton.h"
  43. Singleton* Singleton::m_instance = nullptr;
  44. Singleton *Singleton::GetInstance() {
  45. if (m_instance == nullptr) {
  46. m_instance = new Singleton();
  47. }
  48. return m_instance;
  49. }
  50. void Singleton::DestoryInstance() {
  51. if (m_instance != nullptr) {
  52. delete m_instance;
  53. m_instance = nullptr;
  54. }
  55. }
  56. Singleton::Singleton() {
  57. }

2.2. 多線程

  1. #ifndef THREAD_SAFE_SINGLETON_H
  2. #define THREAD_SAFE_SINGLETON_H
  3. /**
  4. * @brief 線程安全單例,多線程時使用
  5. */
  6. class ThreadSafeSingleton {
  7. public:
  8. /**
  9. * @brief 單例模式,獲取實例化對象
  10. * @param 無
  11. * @return 單例對象
  12. */
  13. static ThreadSafeSingleton* GetInstance();
  14. /**
  15. * @brief 單例模式,主動銷毀實例化對象
  16. * @param 無
  17. * @return 無
  18. */
  19. static void DestoryInstance();
  20. private:
  21. /**
  22. * @brief 構造函數
  23. */
  24. ThreadSafeSingleton();
  25. /**
  26. * @brief 單例模式在程序結束時自動刪除單例的方法
  27. */
  28. class SingletonDel {
  29. public:
  30. ~SingletonDel() {
  31. if (m_instance != NULL) {
  32. delete m_instance;
  33. m_instance = NULL;
  34. }
  35. }
  36. };
  37. static ThreadSafeSingleton m_singleton_del;///程序結束時銷毀
  38. static ThreadSafeSingleton *m_instance; //單例對象指針
  39. };
  40. #endif // THREAD_SAFE_SINGLETON_H
  41. #include "thread_safe_singleton.h"
  42. #include <QMutex>
  43. #include <QMutexLocker>
  44. namespace thread_safe_singleton_private {
  45. static QMutex mutex;
  46. }
  47. ThreadSafeSingleton* ThreadSafeSingleton::m_instance = nullptr;
  48. ThreadSafeSingleton *ThreadSafeSingleton::GetInstance() {
  49. if (m_instance == nullptr) {
  50. QMutexLocker lock(thread_safe_singleton_private::mutex);
  51. if (m_instance == nullptr) {
  52. m_instance = new ThreadSafeSingleton();
  53. }
  54. }
  55. return m_instance;
  56. }
  57. void ThreadSafeSingleton::DestoryInstance() {
  58. if (m_instance != nullptr) {
  59. delete m_instance;
  60. m_instance = nullptr;
  61. }
  62. }
  63. ThreadSafeSingleton::ThreadSafeSingleton() {
  64. }

上面的範例提供了主動銷毀單例的方法同時提供了自動銷毀的方法。

若主動銷毀需註意其他存儲了單例指針的對象

相關鏈接:C++設計模式

轉載請以鏈接形式標明本文標題和地址:Techie亮博客 » C++設計模式-單例模式

C++設計模式-單例模式