1. 程式人生 > >Singleton模式在C#與C++中的實現

Singleton模式在C#與C++中的實現

     Singleton應該可以算是GOF23個模式中最簡單的一個模式了,它有兩個要求:一是保證一個類僅有一個例項;二是提供一個訪問它的全域性訪問點。這在實現中分別對應為:一是建構函式非public;二是提供一個靜態函式作為全域性訪問點。
  在
C#中,我們可以這麼寫:
publicclass ExampleSingleton
{
    
// code to support Singletonprotected ExampleSingleton(){}
    
protectedstatic ExampleSingleton instance =new ExampleSingleton();

    
public
static ExampleSingleton Instance
    {
        
get{return instance;}
    }

    
// This class's real functionalitiespublicvoid Write(){Console.WriteLine("Hello, World!");}
}

// use this singleton classExampleSingleton.Instance.Write();
    類似的,C++中實現如下:
class ExampleSingleton
{
    
// code to support Singletonprotected
:
        ExampleSingleton(){}
public:
    
static ExampleSingleton& Instance()
    {
        
static ExampleSingleton instance;
        
return instance;
    }

    
// This class's real functionalitiesvoid Write(){printf("Hello, World!");}
};

// use this singleton classExampleSingleton::Instance().Write();
    這樣寫的確符合了
singleton的兩個要求,但是如果我們的系統中有許多個Singleton類,而對於每一個類,我們都要寫那些固定的,重複的程式碼去支援其singleton的屬性。這不是一個好現象,我們希望可以把這些固定的程式碼提取出來,使我們的Singleton類只需要專注於實現其真正的功能,相信很多人都聽說過這個做法:Singleton模板基類。
對於C#:
// Singleton base class, each class need to be a singleton should 
// derived from this classpublicclass 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);}
    
protectedstatic T instance =new T();
    
publicstatic T Instance
    {
        
get{return instance;}
    }
}

// Concrete singleton class, derived from Singleton<T>publicclass 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 functionalitiespublicvoid Write(){Console.WriteLine("Hello, World!");}
}

// use this singleton classExampleSingleton.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 classtemplate <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 functionalitiesvoid Write(){printf("Hello, World!");}
};

// use this singleton classExampleSingleton::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模板基類這種機制,以上問題就不會存在,我們得到的不僅僅是節約幾行程式碼:)

posted on 2007-07-17 23:54 SmartPtr 閱讀(1596) 評論(7)  編輯 收藏 引用