設計模式設計模式
設計模式表明了最佳實踐,是軟件開發過程當中面臨通常問題的解決方案安全
設計模式是一套被反覆使用,通過分類,代碼設計的經驗ide
單例模式函數
單例模式也叫單件模式Singleton是一個很是用的設計模式,建一個線程安全且高效的Singleton是很是重要的spa
1.不考慮線程安全的一個單例模式線程
class Singleton { public: static Singleton* GetInstance() { if (_sInstance == NULL) { _sInstance = new Singleton(); } return _sInstance; } static void DelInstance() { if (_sInstance) { delete _sInstance; _sInstance = NULL; } } void Print() { cout << _data << endl; } private: //定義構造函數私有,限制只能在類內建立對象 Singleton() :_data(0) {} Singleton(const Singleton&); Singleton& operator=(const Singleton&); //指向實例的指針爲靜態私有,這樣定義靜態成員函數獲取對象實例 static Singleton* _sInstance; int _data; };
2.線程安全單例設計
懶漢模式指針
class Singleton { public: static Singleton* GetInstance() { if(_sInstance==NULL) { lock(); if (_sInstance == NULL) { _sInstance = new Singleton(); } unlock(); return _sInstance; } static void DelInstance() { if (_sInstance) { delete _sInstance; _sInstance = NULL; } } void Print() { cout << _data << endl; } private: //定義構造函數私有,限制只能在類內建立對象 Singleton() :_data(0) {} Singleton(const Singleton&); Singleton& operator=(const Singleton&); //指向實例的指針爲靜態私有,這樣定義靜態成員函數獲取對象實例 static Singleton* _sInstance; int _data; }; //靜態的數據成員必須初始化 Singleton* Singleton::_sInstance = NULL; void TestSingleton() { Singleton::GetInstance()->Print(); Singleton::DelInstance(); }
第二個版本orm
class Singleton { public: static Singleton* GetInstance() { if (_sInstance == NULL) { _sInstance = new Singleton(); } return _sInstance; } static void DelInstance() { std::lock_guard<std::mutex> lock(_mtx); if (_sInstance) { delete _sInstance; _sInstance = NULL; } } void Print() { cout << _data << endl; } private: //定義構造函數私有,限制只能在類內建立對象 Singleton() :_data(0) {} Singleton(const Singleton&); Singleton& operator=(const Singleton&); //指向實例的指針爲靜態私有,這樣定義靜態成員函數獲取對象實例 static Singleton* _sInstance; int _data; }; //靜態的數據成員必須初始化 Singleton* Singleton::_sInstance = NULL; void TestSingleton() { Singleton::GetInstance()->Print(); Singleton::DelInstance(); }
餓漢模式對象
方式一
class Singleton { public: // 獲取惟一對象實例的接口函數 static Singleton* GetInstance() { static Singleton tmp; return &tmp; } void Print() { cout<<_data<<endl; } private: // 構造函數定義爲私有,限制只能在類內建立對象 Singleton() :_data(0) {} Singleton(const Singleton&); Singleton& operator=(const Singleton&); // 單例類裏面的數據 int _data; }; void TestSingleton() { Singleton::GetInstance()->Print(); }
方式二
class Singleton { public: // 獲取惟一對象實例的接口函數 static Singleton* GetInstance() { assert(_sInstance); return _sInstance; } // 刪除實例對象 static void DelInstance() { if (_sInstance) { delete _sInstance; _sInstance = NULL; } } void Print() { cout << _data << endl; } private: // 構造函數定義爲私有,限制只能在類內建立對象 Singleton() :_data(0) {} Singleton(const Singleton&); Singleton& operator=(const Singleton&); // 指向實例的指針定義爲靜態私有,這樣定義靜態成員函數獲取對象實例 static Singleton* _sInstance; // 單例類裏面的數據 int _data; }; Singleton* Singleton::_sInstance = new Singleton; void TestSingleton() { Singleton::GetInstance()->Print(); Singleton::DelInstance(); }
在堆上建立對象
class AA { public: AA* GetObj() { return new AA; } private: AA() {} AA(const AA&); int a; };
在棧上建立對象
方式一
class AA { public: AA GetObj() { return AA(); } private: AA() {} AA(const AA&); int a; };
方式二
class AA { private: void*operator new(size_t); void operator delete(void*p); void*operator new[](size_t); void operator delete[](void* p); private: AA() {} AA(const AA&); int a; };