C 中的Singleton 類的實現

2021-04-23 05:31:37 字數 3013 閱讀 1607

ansi c++ 中的 singleton 實現說難不難,說容易也不容易,很多人寫 ansi c++ 的 singleton class 都有錯誤。這篇文章討論怎樣在 ansi c++ 中寫 singleton class, 希望對大家有幫助。

《設計模式》中把 singleton 寫成返回指標:

class singleton;

相應的實現 cpp 檔案是

singleton* singleton::_instance;

singleton* singleton::instance();

return _instance;

}  將建構函式設計成 protected 的目的是防止在 class 外面 new ,有人可能會設計成 private ,如果考慮到有可能會繼承這個類的話,還是將建構函式設計成 protected 比較好,還需要加乙個 virtual 析構函式。為了防止別人複製 singleton 物件:

singleton* psingleton = singleton::instance();

singleton s1 = *psingleton;

singleton s2 = *psingleton; 

需要將拷貝構造(copy constructor)函式變成 private。

但是這裡存在的問題是,什麼時候刪除 singleton 物件?按照 c++ 的乙個基本原則,物件在**建立就在**銷毀,這裡還應該放乙個 destroy 方法來刪除 singleton 物件。如果忘了刪除就比較麻煩。instance 函式還存在多執行緒同時訪問的加鎖問題。如果把 instance 函式開始和結尾放上加鎖和解鎖,整個函式效能會下降很多。這不是乙個好的設計。

有乙個小小的改動,可以避免忘了刪除 singleton 物件帶來記憶體洩露的問題。那就是用 std:auto_ptr 來包含 singleton 物件,定義乙個class static member auto_ptr 物件,在析構的靜態 auto_ptr 變數的時候時候自動刪除 singleton 物件。為了不讓使用者 delete singleton 物件,需要將析構函式由 public 變成 protected。以下是標頭檔案 singletonautoptr.h :

#include 

using

namespace std;

class csingletonautoptr ;

對應的 singletonautoptr.cpp 如下:

#include "singletonautoptr.h"

#include 

//initial static member vars here 

csingletonautoptr* csingletonautoptr::m_instance = null;

auto_ptrcsingletonautoptr::m_auto_ptr;

/ // construction/destruction

/ csingletonautoptr::csingletonautoptr()

csingletonautoptr::~csingletonautoptr()

csingletonautoptr* csingletonautoptr::getinstance()

void csingletonautoptr::test()

呼叫方法:

csingletonautoptr* psingleton = csingletonautoptr::getinstance();

psingleton->test();

寫乙個 c++ 中的 singleton 需要這麼費勁,大大出乎我們的意料。有很多人從未用過 auto_ptr,而且 std:auto_ptr 本身就並不完美,它是基於物件所有權機制的,相比之下,apache log4cxx 中有乙個 auto_ptr, 是基於物件計數的,更為好用。只是為了用乙個好的 auto_ptr 而不得不用 log4cxx , 對於很多專案來說,也不太好。當然了,ansi c++ 的 stl 中 std:auto_ptr 對於寫上面的例子已經足夠用了。

另外乙個思路是,把 getinstance 函式設計成 static member 可能更好,因為一般來說,singleton 物件都不大,static member 雖然必須一直占用記憶體,問題不大。這裡的析構函式必須設成 public 了。以下是標頭檔案 singlestaticobj.h

class csingletonstaticobj 

; 對應的 singlestaticobj.cpp 檔案為:

#include "singletonstaticobj.h"

#include 

#include 

using

namespace std;

csingletonstaticobj csingletonstaticobj::m_instance;

csingletonstaticobj::csingletonstaticobj()

csingletonstaticobj::~csingletonstaticobj()

csingletonstaticobj& csingletonstaticobj::getinstance()

void csingletonstaticobj::test()

呼叫方法:

csingletonstaticobj& singleton = csingletonautoptr::getinstance();

singleton.test();

從**量來說,似乎使用 static member ref 更為簡單。我更偏向於用這種方法。

但是,並不是所有情況下面都適合用 static member singleton。比如說,getinstance 需要動態決定返回不同的 instance 的時候,就不能用。舉例來說,filesystem::getinstance, 在 windows 下面執行可能需要返回 new winfilesystem, linux/unix 下面執行可能需要返回 new linuxfilesystem,這個時候還是需要用上面的 auto_ptr 包含 singleton 指標的方法。

C 中的singleton 類的實現討論

c 中的singleton 類的實現討論 ansi c 中的 singleton 實現說難不難,說容易也不容易,很多人寫 ansi c 的 singleton class 都有錯誤。這篇文章討論怎樣在 ansi c 中寫 singleton class,希望對大家有幫助。設計模式 中把 single...

Singleton模式與在Ogre中的實現

singleton 模式的用意是對於乙個 class 全域性只能建立乙個例項。常規的做法是把建構函式藏起來,然後通過靜態方法返回唯一的乙個靜態例項。class singleton protected singleton 如果我們想讓 singleton 的子類也具有唯一性,只需要在 getinsta...

Singleton模式與在Ogre中的實現

singleton模式的用意是對於乙個class全域性只能建立乙個例項。常規的做法是把建構函式藏起來,然後通過靜態方法返回唯一的乙個靜態例項。class singleton protected singleton 如果我們想讓singleton的子類也具有唯一性,只需要在getinstance中根據...