Singleton應(yīng)該可以算是GOF的23個(gè)模式中最簡(jiǎn)單的一個(gè)模式了,它有兩個(gè)要求:一是保證一個(gè)類僅有一個(gè)實(shí)例;二是提供一個(gè)訪問(wèn)它的全局訪問(wèn)點(diǎn)。這在實(shí)現(xiàn)中分別對(duì)應(yīng)為:一是構(gòu)造函數(shù)非public;二是提供一個(gè)靜態(tài)函數(shù)作為全局訪問(wèn)點(diǎn)。
在C#中,我們可以這么寫:
{
// 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();
{
// 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的兩個(gè)要求,但是如果我們的系統(tǒng)中有許多個(gè)Singleton類,而對(duì)于每一個(gè)類,我們都要寫那些固定的,重復(fù)的代碼去支持其singleton的屬性。這不是一個(gè)好現(xiàn)象,我們希望可以把這些固定的代碼提取出來(lái),使我們的Singleton類只需要專注于實(shí)現(xiàn)其真正的功能,相信很多人都聽說(shuō)過(guò)這個(gè)做法:Singleton模板基類。
對(duì)于C#:
// 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功能的代碼提到一個(gè)Singleton<T>的類模板當(dāng)中,任何需要成為Singlton的類,只需從其派生便自然獲得Singleton功能。這里的一個(gè)問(wèn)題是:為了支持模板類中的new()constraint,我們不得不把作為具體singleton類的派生類的構(gòu)造函數(shù)作為public,這就導(dǎo)致我們無(wú)法在編譯期阻止用戶自行new出第二個(gè),第三個(gè)實(shí)例來(lái),但我們可以在運(yùn)行期來(lái)進(jìn)行檢查進(jìn)而保證其實(shí)例的單一性,這就是這singleton基類構(gòu)造函數(shù)的作用:
當(dāng)然,有另外一種實(shí)現(xiàn)方法,那就是singleton基類不提供new() constraint, 這樣我們就可以讓ExampleSingleton的構(gòu)造函數(shù)為非public,在要?jiǎng)?chuàng)建T實(shí)例的時(shí)候,由于不能使用new, 我們用reflection反射出類型T的非public構(gòu)造函數(shù)并調(diào)用之。這樣,我們的確能保證編譯期實(shí)例的唯一性,但是由于用了反射,感覺代碼不是那么的簡(jiǎn)單優(yōu)雅,并且對(duì)其性能持保留態(tài)度,所以不太傾向與這種方法。
但畢竟是越早檢查出錯(cuò)誤越好,所以大家如果有好的解決方案,不妨提出來(lái)一起討論討論。
而C++中由于提供了友元這個(gè)特性,實(shí)現(xiàn)起來(lái)要好一些:
// 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++友元的幫助下,我們成功實(shí)現(xiàn)了在編譯期保證實(shí)例的唯一性。(當(dāng)然,前提是你不要"亂交朋友")。
有人可能會(huì)問(wèn),實(shí)現(xiàn)singleton的代碼并不多,我們沒必要搞這么一個(gè)機(jī)制來(lái)做代碼復(fù)用吧? 的確,我們復(fù)用的代碼并不是很多,但是,我想代碼復(fù)用的目的不僅僅是減少代碼量,其最重要的目的還是在于保持行為的一致性,以便于使用與維護(hù)。(用函數(shù)替換代碼段便是一個(gè)很好的例子)。
對(duì)于這里的singleton類來(lái)講,如果不做這個(gè)設(shè)計(jì),我們?cè)诿總€(gè)具體的singleton類內(nèi)部實(shí)現(xiàn)其singleton機(jī)制,那么可能出現(xiàn)的問(wèn)題是
1. 很難保證其接口的一致性
張三寫了一個(gè)singleton類,全局訪問(wèn)函數(shù)是Instance, 李四也寫了一個(gè)Singleton類,全局訪問(wèn)函數(shù)可能就是GetInstance了。。。。。我們并沒有一個(gè)健壯的機(jī)制來(lái)保證接口的一致性,從而導(dǎo)致了使用的混亂性。
2. 不易維護(hù)
Singleton創(chuàng)建實(shí)例有兩種:一種為lazy initialization, 一種就是early initialization, 假如開始的實(shí)現(xiàn)是所有的singleton都用lazy initialization, 突然某種需求要求你用early initialization,你的噩夢(mèng)就開始了,你不得不重寫每一個(gè)singleton類。
而用了singleton模板基類這種機(jī)制,以上問(wèn)題就不會(huì)存在,我們得到的不僅僅是節(jié)約幾行代碼:)