C++設計模式-單例模式
阿新 • • 發佈:2017-12-22
con git god www light nullptr return post gpo 版權聲明:若無來源註明,Techie亮博客文章均為原創。 轉載請以鏈接形式標明本文標題和地址:
本文標題:C++設計模式-單例模式 本文地址:http://techieliang.com/2017/12/772/ 文章目錄
本文標題:C++設計模式-單例模式 本文地址:http://techieliang.com/2017/12/772/ 文章目錄
- 1. 介紹
- 2. 範例
- 2.1. 單線程
- 2.2. 多線程
1. 介紹
幾個重點:
- 構造函數為private
- 提供一個獲取單例對象指針的函數
- 一個靜態指針成員存儲單例對象
註意:
- 獲取單例對象也可以獲取對象引用,但要註意拷貝構造函數和賦值運算符
- 如果有多線程訪問單例,需要註意線程同步
2. 範例
源碼GitHub:CppDesignPattern
2.1. 單線程
- #ifndef SIGLETON_H
- #define SIGLETON_H
- /**
- * @brief 非線程安全單例,無多線程時使用
- */
- class Singleton {
- public:
- /**
- * @brief 單例模式,獲取實例化對象
- * @param 無
- * @return 單例對象
- */
- static Singleton *GetInstance();
- /**
- * @brief 單例模式,主動銷毀實例化對象
- * @param 無
- * @return 無
- */
- static void DestoryInstance();
- private:
- /**
- * @brief 構造函數
- */
- Singleton();
- /**
- * @brief 單例模式在程序結束時自動刪除單例的方法
- */
- class SingletonDel {
- public:
- ~SingletonDel() {
- if (m_instance != NULL) {
- delete m_instance;
- m_instance = NULL;
- }
- }
- };
- static SingletonDel m_singleton_del;///程序結束時銷毀
- static Singleton *m_instance; //單例對象指針
- };
- #endif // SIGLETON_H
- //cpp
- #include "sigleton.h"
- Singleton* Singleton::m_instance = nullptr;
- Singleton *Singleton::GetInstance() {
- if (m_instance == nullptr) {
- m_instance = new Singleton();
- }
- return m_instance;
- }
- void Singleton::DestoryInstance() {
- if (m_instance != nullptr) {
- delete m_instance;
- m_instance = nullptr;
- }
- }
- Singleton::Singleton() {
- }
2.2. 多線程
- #ifndef THREAD_SAFE_SINGLETON_H
- #define THREAD_SAFE_SINGLETON_H
- /**
- * @brief 線程安全單例,多線程時使用
- */
- class ThreadSafeSingleton {
- public:
- /**
- * @brief 單例模式,獲取實例化對象
- * @param 無
- * @return 單例對象
- */
- static ThreadSafeSingleton* GetInstance();
- /**
- * @brief 單例模式,主動銷毀實例化對象
- * @param 無
- * @return 無
- */
- static void DestoryInstance();
- private:
- /**
- * @brief 構造函數
- */
- ThreadSafeSingleton();
- /**
- * @brief 單例模式在程序結束時自動刪除單例的方法
- */
- class SingletonDel {
- public:
- ~SingletonDel() {
- if (m_instance != NULL) {
- delete m_instance;
- m_instance = NULL;
- }
- }
- };
- static ThreadSafeSingleton m_singleton_del;///程序結束時銷毀
- static ThreadSafeSingleton *m_instance; //單例對象指針
- };
- #endif // THREAD_SAFE_SINGLETON_H
- #include "thread_safe_singleton.h"
- #include <QMutex>
- #include <QMutexLocker>
- namespace thread_safe_singleton_private {
- static QMutex mutex;
- }
- ThreadSafeSingleton* ThreadSafeSingleton::m_instance = nullptr;
- ThreadSafeSingleton *ThreadSafeSingleton::GetInstance() {
- if (m_instance == nullptr) {
- QMutexLocker lock(thread_safe_singleton_private::mutex);
- if (m_instance == nullptr) {
- m_instance = new ThreadSafeSingleton();
- }
- }
- return m_instance;
- }
- void ThreadSafeSingleton::DestoryInstance() {
- if (m_instance != nullptr) {
- delete m_instance;
- m_instance = nullptr;
- }
- }
- ThreadSafeSingleton::ThreadSafeSingleton() {
- }
上面的範例提供了主動銷毀單例的方法同時提供了自動銷毀的方法。
若主動銷毀需註意其他存儲了單例指針的對象
相關鏈接:C++設計模式
轉載請以鏈接形式標明本文標題和地址:Techie亮博客 » C++設計模式-單例模式C++設計模式-單例模式