單例模式(Singleton)[分類:建立型] [目的:爲程序的某個類的訪問,提供惟一對象實例]html
這估計是設計模式裏面最簡單的一個類了,下面咱們一塊兒看看,單例它是什麼?以及如何實現一個單例git
1 class Singleton 2 { 3 public: 4 static Singleton& GetInstance() 5 { 6 static Singleton instance; 7 return instance; 8 } 9 private: 10 Singleton(); 11 12 };
1 Singleton instance1=Singleton::GetInstance(); 2 Singleton instance2=Singleton::GetInstance(); 3 qDebug()<<"instance1 address:"<<&instance1; 4 qDebug()<<"instance2 address:"<<&instance2;
1 class SingletonModify 2 { 3 public: 4 static SingletonModify& GetInstance() 5 { 6 static SingletonModify instance; 7 return instance; 8 } 9 void sayHello(); 10 private: 11 SingletonModify(); 12 SingletonModify(SingletonModify const &);//to use this modify wrong demo 13 SingletonModify& operator =(SingletonModify const &);//to use this modify wrong demo 14 };
SingletonModify::GetInstance().sayHello();//after modify code,
1 class SingletonSimplify 2 { 3 public: 4 SingletonSimplify(); 5 static SingletonSimplify* GetInstanceptr()//simplify method 6 { 7 static SingletonSimplify instance; 8 return &instance; 9 } 10 };
1 SingletonSimplify* instanceSmp1=SingletonSimplify::GetInstanceptr();//another simplify singleton method 2 SingletonSimplify* instanceSmp2=SingletonSimplify::GetInstanceptr(); 3 qDebug()<<"instanceSmp1 address:"<<instanceSmp1; 4 qDebug()<<"instanceSmp2 address:"<<instanceSmp2;
1 class SingletonLazyMode 2 { 3 public: 4 static SingletonLazyMode* GetInstance(); 5 static void InstanceDispose(); 6 void sayHi(); 7 private: 8 SingletonLazyMode(); 9 static SingletonLazyMode* mSingletonInstance; 10 int num=10; 11 };
1 SingletonLazyMode *SingletonLazyMode::mSingletonInstance=NULL; 2 SingletonLazyMode *SingletonLazyMode::GetInstance() 3 { 4 if(mSingletonInstance==NULL) 5 { 6 mSingletonInstance=new SingletonLazyMode(); 7 mSingletonInstance->num=20; 8 } 9 return mSingletonInstance; 10 } 11 void SingletonLazyMode::InstanceDispose() 12 { 13 if(mSingletonInstance!=NULL) 14 { 15 delete mSingletonInstance; 16 mSingletonInstance=NULL; 17 } 18 } 19 void SingletonLazyMode::sayHi() 20 { 21 qDebug()<<"hi lazy man! Number:"<<num; 22 } 23 SingletonLazyMode::SingletonLazyMode() 24 { 25 }
1 class SingletonEagerMode 2 { 3 public: 4 static SingletonEagerMode* GetInstance(); 5 static void InstanceDispose(); 6 private: 7 SingletonEagerMode(); 8 static SingletonEagerMode* mEagerInstance; 9 };
1 SingletonEagerMode *SingletonEagerMode::mEagerInstance=new SingletonEagerMode(); 2 SingletonEagerMode *SingletonEagerMode::GetInstance() 3 { 4 return mEagerInstance; 5 } 6 void SingletonEagerMode::InstanceDispose() 7 { 8 if(mEagerInstance!=NULL) 9 { 10 delete mEagerInstance; 11 mEagerInstance=NULL; 12 } 13 } 14 SingletonEagerMode::SingletonEagerMode() 15 { 16 17 }
1 SingletonLazyMode* lazyinstance1=SingletonLazyMode::GetInstance();//lazy mode 懶漢模式 2 SingletonLazyMode* lazyinstance2=SingletonLazyMode::GetInstance(); 3 lazyinstance1->sayHi(); 4 lazyinstance2->sayHi(); 5 qDebug()<<"lazyinstance1 address:"<<lazyinstance1; 6 qDebug()<<"lazyinstance2 address:"<<lazyinstance2; 7 SingletonLazyMode::InstanceDispose(); 8 qDebug()<<"lazyinstance1 address:"<<lazyinstance1; 9 qDebug()<<"lazyinstance2 address:"<<lazyinstance2; 10 lazyinstance1->sayHi(); 11 lazyinstance2->sayHi(); 12 SingletonEagerMode* eagerinstance1=SingletonEagerMode::GetInstance();//eager mode 餓漢模式 13 SingletonEagerMode* eagerinstance2=SingletonEagerMode::GetInstance(); 14 qDebug()<<"eagerinstance1 address:"<<eagerinstance1; 15 qDebug()<<"eagerinstance2 address:"<<eagerinstance2;
參考了一篇博客也沒看出什麼問題:https://www.cnblogs.com/chengjundu/p/11283123.html設計模式
若是有朋友知道望不吝賜教!!!!感謝。安全
最後看看咱們線程安全的懶漢實現方式函數
.h文件性能
1 class SingletonThreadSafety 2 { 3 public: 4 static SingletonThreadSafety* GetInstance(); 5 static void InstanceDispose(); 6 private: 7 SingletonThreadSafety(); 8 static SingletonThreadSafety* mSaftyInstance; 9 static QMutex mMutex; 10 };
1 SingletonThreadSafety *SingletonThreadSafety::mSaftyInstance=NULL; 2 QMutex SingletonThreadSafety::mMutex; 3 SingletonThreadSafety *SingletonThreadSafety::GetInstance() 4 { 5 if(mSaftyInstance==NULL) 6 { 7 QMutexLocker locker(&mMutex); 8 if(mSaftyInstance==NULL) 9 { 10 mSaftyInstance=new SingletonThreadSafety(); 11 } 12 } 13 return mSaftyInstance; 14 } 15 void SingletonThreadSafety::InstanceDispose() 16 { 17 if(mSaftyInstance!=NULL) 18 { 19 delete mSaftyInstance; 20 mSaftyInstance=NULL; 21 } 22 } 23 SingletonThreadSafety::SingletonThreadSafety() 24 { 25 26 }
這就完美解決掉了線程安全問題,可是在獲取實例對象的時候須要對Qmutex進行判斷,這會損失一點點性能。測試
以上單例全部內容,若有錯誤請指出!!!this
參考<<大話設計模式>>一書spa
附源代碼:線程
https://gitee.com/xiaochunlu/designer-pattern/tree/master/Singleton_Pattern