Singleton應該可以算是GOF的23個模式中最簡單的一個模式了,它有兩個要求:一是保證一個類僅有一個執行個體;二是提供一個訪問它的全域訪問點。這在實現中分別對應為:一是建構函式非public;二是提供一個靜態函數作為全域訪問點。
在C#中,我們可以這麼寫:
public class ExampleSingleton
{
// code to support Singleton
protected ExampleSingleton(){}
protected static ExampleSingleton instance = new ExampleSingleton();
public static ExampleSingleton Instance
{
get{return instance;}
}
// This class's real functionalities
public void Write(){Console.WriteLine("Hello, World!");}
}
// use this singleton class
ExampleSingleton.Instance.Write();
類似的,C++中實現如下:
class ExampleSingleton
{
// code to support Singleton
protected:
ExampleSingleton(){}
public:
static ExampleSingleton& Instance()
{
static ExampleSingleton instance;
return instance;
}
// This class's real functionalities
void Write(){printf("Hello, World!");}
};
// use this singleton class
ExampleSingleton::Instance().Write();
這樣寫的確符合了singleton的兩個要求,但是如果我們的系統中有許多個Singleton類,而對於每一個類,我們都要寫那些固定的,重複的代碼去支援其singleton的屬性。這不是一個好現象,我們希望可以把這些固定的代碼提取出來,使我們的Singleton類只需要專註於實現其真正的功能,相信很多人都聽說過這個做法:Singleton模板基類。
對於C#:
// Singleton base class, each class need to be a singleton should
// derived from this class
public class Singleton<T> where T: new()
{
// Instead of compile time check, we provide a run time check
// to make sure there is only one instance.
protected Singleton(){Debug.Assert(null == instance);}
protected static T instance = new T();
public static T Instance
{
get{return instance;}
}
}
// Concrete singleton class, derived from Singleton<T>
public class ExampleSingleton: Singleton<ExampleSingleton>
{
// since there is no "freind class" in C#, we have to make
// this contructor public to support the new constraint.
public ExampleSingleton(){}
// This class's real functionalities
public void Write(){Console.WriteLine("Hello, World!");}
}
// use this singleton class
ExampleSingleton.Instance.Write();
這裡,我們把為了支援Singleton功能的代碼提到一個Singleton<T>的類模板當中,任何需要成為Singlton的類,只需從其派生便自然獲得Singleton功能。這裡的一個問題是:為了支援模板類中的new()constraint,我們不得不把作為具體singleton類的衍生類別的建構函式作為public,這就導致我們無法在編譯期阻止使用者自行new出第二個,第三個執行個體來,但我們可以在運行期來進行檢查進而保證其執行個體的單一性,這就是這singleton基類建構函式的作用:
protected Singleton(){Debug.Assert(null == instance);}
當然,有另外一種實現方法,那就是singleton基類不提供new() constraint, 這樣我們就可以讓ExampleSingleton的建構函式為非public,在要建立T執行個體的時候,由於不能使用new, 我們用reflection反射出類型T的非public建構函式並調用之。這樣,我們的確能保證編譯期執行個體的唯一性,但是由於用了反射,感覺代碼不是那麼的簡單優雅,並且對其效能持保留態度,所以不太傾向與這種方法。
但畢竟是越早檢查出錯誤越好,所以大家如果有好的解決方案,不妨提出來一起討論討論。
而C++中由於提供了友元這個特性,實現起來要好一些:
// Singleton base class, each class need to be a singleton should
// derived from this class
template <class T> class Singleton
{
protected:
Singleton(){}
public:
static T& Instance()
{
static T instance;
return instance;
}
};
// Concrete singleton class, derived from Singleton<T>
class ExampleSingleton: public Singleton<ExampleSingleton>
{
// so that Singleton<ExampleSingleton> can access the
// protected constructor
friend class Singleton<ExampleSingleton>;
protected:
ExampleSingleton(){}
public:
// This class's real functionalities
void Write(){printf("Hello, World!");}
};
// use this singleton class
ExampleSingleton::Instance().Write();
在C++友元的協助下,我們成功實現了在編譯期保證執行個體的唯一性。(當然,前提是你不要"亂交朋友")。
有人可能會問,實現singleton的代碼並不多,我們沒必要搞這麼一個機制來做代碼複用吧? 的確,我們複用的代碼並不是很多,但是,我想代碼複用的目的不僅僅是減少代碼量,其最重要的目的還是在於保持行為的一致性,以便於使用與維護。(用函數替換程式碼片段便是一個很好的例子)。
對於這裡的singleton類來講,如果不做這個設計,我們在每個具體的singleton類內部實現其singleton機制,那麼可能出現的問題是
1. 很難保證其介面的一致性
張三寫了一個singleton類,全域訪問函數是Instance, 李四也寫了一個Singleton類,全域訪問函數可能就是GetInstance了。。。。。我們並沒有一個健壯的機制來保證介面的一致性,從而導致了使用的混亂性。
2. 不易維護
Singleton建立執行個體有兩種:一種為lazy initialization, 一種就是early initialization, 假如開始的實現是所有的singleton都用lazy initialization, 突然某種需求要求你用early initialization,你的噩夢就開始了,你不得不重寫每一個singleton類。
而用了singleton模板基類這種機制,以上問題就不會存在,我們得到的不僅僅是節約幾行代碼:)
(搬自以前blog, 2007-07-17)