Singleton設(shè)計(jì)模式的C#實(shí)現(xiàn)
電子科技大學(xué) 張申 (handi@sina.com)
關(guān)鍵字:singleton 設(shè)計(jì)模式 同步 C#
1 Singleton模式。
Singleton(譯為單件或單態(tài))模式是設(shè)計(jì)模式中比較簡(jiǎn)單而常用的模式。
有些時(shí)候在整個(gè)應(yīng)用程序中,會(huì)要求某個(gè)類有且只有一個(gè)實(shí)例,這個(gè)時(shí)候可以采用Singleton模式進(jìn)行設(shè)計(jì)。用Singleton模式設(shè)計(jì)的類不僅能保證在應(yīng)用中只有一個(gè)實(shí)例,而且提供了一種非全局變量的方法進(jìn)行全局訪問,稱為全局訪問點(diǎn),這樣對(duì)于沒有全局變量概念的純面向?qū)ο笳Z言來說是非常方便的,比如C#。
本文用一個(gè)計(jì)數(shù)器的例子來描述在C#中如何使用Singleton模式:計(jì)數(shù)的值設(shè)計(jì)為計(jì)數(shù)器類的一個(gè)私有成員變量,它被4個(gè)不同的線程進(jìn)行讀寫操作,為保證計(jì)數(shù)的正確性,在整個(gè)應(yīng)用當(dāng)中必然要求計(jì)數(shù)器類的實(shí)例是唯一的。
2 Singleton的實(shí)現(xiàn)方式。
首先看看教科書方式的Singleton標(biāo)準(zhǔn)實(shí)現(xiàn)的兩種方法,以下用的是類C#偽代碼:
方法一:
using System;
namespace csPattern.Singleton
{
public class Singleton
{
static Singleton uniSingleton = new Singleton();
private Singleton() {}
static public Singleton instance()
{
return uniSingleton;
}
}
}
方法二:
using System;
namespace csPattern.Singleton
{
public class Singleton
{
static Singleton uniSingleton;
private Singleton() {}
static public Singleton instance()
{
if (null == uniSingleton)
{
uniSingleton = new Singleton _lazy();
}
return uniSingleton;
}
}
}
Singleton模式的實(shí)現(xiàn)有兩個(gè)技巧:一是使用靜態(tài)成員變量保存“全局”的實(shí)例,確保了唯一性,使用靜態(tài)的成員方法instance() 代替 new關(guān)鍵字來獲取該類的實(shí)例,達(dá)到全局可見的效果。二是將構(gòu)造方法設(shè)置成為private,如果使用new關(guān)鍵字創(chuàng)建類的實(shí)例,則編譯報(bào)錯(cuò),以防編程時(shí)候筆誤。
上面方法二的初始化方式稱為lazy initialization,是在第一次需要實(shí)例的時(shí)候才創(chuàng)建類的實(shí)例,與方法一中類的實(shí)例不管用不用一直都有相比,方法二更加節(jié)省系統(tǒng)資源。但是方法二在多線程應(yīng)用中有時(shí)會(huì)出現(xiàn)多個(gè)實(shí)例化的現(xiàn)象。
假設(shè)這里有2個(gè)線程:主線程和線程1,在創(chuàng)建類的實(shí)例的時(shí)候可能會(huì)遇到一些原因阻塞一段時(shí)間(比如網(wǎng)絡(luò)速度或者需要等待某些正在使用的資源的釋放),此時(shí)的運(yùn)行情況如下:
主線程首先去調(diào)用instance()試圖獲得類的實(shí)例,instance()成員方法判斷該類沒有創(chuàng)建唯一實(shí)例,于是開始創(chuàng)建實(shí)例。由于一些因素,主線程不能馬上創(chuàng)建成功,而需要等待一些時(shí)間。此時(shí)線程1也去調(diào)用instance()試圖獲得該類的實(shí)例,因?yàn)榇藭r(shí)實(shí)例還未被主線程成功創(chuàng)建,因此線程1又開始創(chuàng)建新實(shí)例。結(jié)果是兩個(gè)線程分別創(chuàng)建了兩次實(shí)例,對(duì)于計(jì)數(shù)器類來說,就會(huì)導(dǎo)致計(jì)數(shù)的值被重置,與Singleton的初衷違背。解決這個(gè)問題的辦法是同步。
下面看看本文的計(jì)數(shù)器的例子的實(shí)現(xiàn):
使用方法一:
using System;
using System.Threading;
namespace csPattern.Singleton
{
public class Counter
{
static Counter uniCounter = new Counter(); //存儲(chǔ)唯一的實(shí)例。
private int totNum = 0; //存儲(chǔ)計(jì)數(shù)值。
private Counter()
{
Thread.Sleep(100); //這里假設(shè)因?yàn)槟撤N因素而耽擱了100毫秒。
//在非lazy initialization 的情況下, 不會(huì)影響到計(jì)數(shù)。.
}
static public Counter instance()
{
return uniCounter;
}
public void Inc() { totNum ++;} //計(jì)數(shù)加1。
public int GetCounter() { return totNum;} //獲得當(dāng)前計(jì)數(shù)值。
}
}
以下是調(diào)用Counter類的客戶程序,在這里我們定義了四個(gè)線程同時(shí)使用計(jì)數(shù)器,每個(gè)線程使用4次,最后得到的正確結(jié)果應(yīng)該是16:
using System;
using System.IO;
using System.Threading;
namespace csPattern.Singleton.MutileThread
{
public class MutileClient
{
public MutileClient() {}
public void DoSomeWork()
{
Counter myCounter = Counter.instance(); //方法一
//Counter_lazy myCounter = Counter_lazy.instance(); //方法二
for (int i = 1; i < 5; i++)
{
myCounter.Inc();
Console.WriteLine("線程{0}報(bào)告: 當(dāng)前counter為: {1}", Thread.CurrentThread.Name.ToString(), myCounter.GetCounter().ToString());
}
}
public void ClientMain()
{
Thread thread0 = Thread.CurrentThread;
thread0.Name = "Thread 0";
Thread thread1 =new Thread(new ThreadStart(this.DoSomeWork));
thread1.Name = "Thread 1";
Thread thread2 =new Thread(new ThreadStart(this.DoSomeWork));
thread2.Name = "Thread 2";
Thread thread3 =new Thread(new ThreadStart(this.DoSomeWork));
thread3.Name = "Thread 3";
thread1.Start();
thread2.Start();
thread3.Start();
DoSomeWork(); //線程0也只執(zhí)行和其他線程相同的工作。
}
}
}
(接
|
溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!