设计模式研究(二)-Singleton
本篇先討論單件 Singleton,單件的目標(biāo)是保證一個(gè)類型只有一個(gè)實(shí)例,那么由誰(shuí)來保證實(shí)例的唯一性呢?可能的方案有:
調(diào)用端調(diào)用一個(gè)類時(shí),他是不需要也不會(huì)去考慮這個(gè)類是否已經(jīng)被實(shí)例化的。而且把這樣的監(jiān)管工作交給調(diào)用端是很不負(fù)責(zé)的做法。
b)類型內(nèi)部保證。
類型內(nèi)部如何保證?
將實(shí)例創(chuàng)建工作放到類型內(nèi)部,這樣類型就可以將實(shí)例創(chuàng)建工作監(jiān)管起來。類型可以知道內(nèi)部的實(shí)例有沒有被創(chuàng)建,甚至可以知道創(chuàng)建實(shí)例的工作被執(zhí)行了多少次。
所以個(gè)人認(rèn)為理解單件需要分為兩步:
1、 監(jiān)管工作誰(shuí)來做?實(shí)例的監(jiān)管工作需要類型自己去做。
2、 監(jiān)管工作如何做?類型如何保證實(shí)例唯一就是技術(shù)實(shí)現(xiàn)問題了,可以看到的版本有 線程安全的、雙重鎖定的、延遲初始化的等。
下面使用偽代碼逐步分析實(shí)例化工作放到類型內(nèi)部的做法。
?調(diào)用我,實(shí)例我給你
????{
????????Singleton?Instance?=?null;
????????// 實(shí)例化類型?Singleton
????????Singleton?GetInstance()
????????{
????????????Instance?=?new?Singleton();
????????????return?Instance;
????????}
????}
你只管調(diào)用,我保證唯一
????{
????????Singleton?Instance?=?null;
????????//實(shí)例化類型?Singleton
????????Singleton?GetInstance()
????????{
????????????Instance?=?new?Singleton();
????????????return?Instance;
????????}
????????//?實(shí)例化類型?Singleton,實(shí)例化時(shí)判斷類型有沒有被創(chuàng)建過,這樣就保證了實(shí)例的唯一
????????Singleton?GetInstance()
????????{
????????????if?(Instance?==?null)
????????????{
????????????????Instance?=?new?Singleton();
????????????}
????????????return?Instance;
????????}
????}
你們都可以調(diào)用,我需要統(tǒng)計(jì)調(diào)用次數(shù)
????{
????????Singleton?Instance?=?null;
????????public?int?Count?{?get;?set;?}
????????//實(shí)例化類型?Singleton
????????Singleton?GetInstance()
????????{
????????????Instance?=?new?Singleton();
????????????return?Instance;
????????}
????????//?實(shí)例化類型?Singleton,實(shí)例化時(shí)判斷類型有沒有被創(chuàng)建過,這樣就保證了實(shí)例的唯一
????????Singleton?GetInstance()
????????{
????????????if?(Instance?==?null)
????????????{
????????????????Instance?=?new?Singleton();
????????????}
????????????return?Instance;
????????}
????????//?實(shí)例化類型?Singleton,并且加入一個(gè)計(jì)數(shù)器,這樣能知道實(shí)例化工作被執(zhí)行了多少次
????????Singleton?GetInstance()
????????{
????????????Count++;
????????????if?(Instance?==?null)
????????????{
????????????????Instance?=?new?Singleton();
????????????}
????????????return?Instance;
????????}
????}
想使用實(shí)例?請(qǐng)出示合法證件
????{
????????Singleton?Instance?=?null;
????????public?int?Count?{?get;?set;?}
????????//實(shí)例化類型?Singleton
????????Singleton?GetInstance()
????????{
????????????Instance?=?new?Singleton();
????????????return?Instance;
????????}
????????//?實(shí)例化類型?Singleton,實(shí)例化時(shí)判斷類型有沒有被創(chuàng)建過,這樣就保證了實(shí)例的唯一
????????Singleton?GetInstance()
????????{
????????????if?(Instance?==?null)
????????????{
????????????????Instance?=?new?Singleton();
????????????}
????????????return?Instance;
????????}
????????//?實(shí)例化類型?Singleton,并且加入一個(gè)計(jì)數(shù)器,這樣能知道實(shí)例化工作被執(zhí)行了多少次
????????Singleton?GetInstance()
????????{
????????????Count++;
????????????if?(Instance?==?null)
????????????{
????????????????Instance?=?new?Singleton();
????????????}
????????????return?Instance;
????????}
????????//?實(shí)例化類型?Singleton,并且接收一個(gè)合法的授權(quán),這樣可以知道每個(gè)授權(quán)方的調(diào)用次數(shù),使用頻率
????????Singleton?GetInstance(string?caller)
????????{
????????????//Check?調(diào)用方合法性驗(yàn)證
????????????if?(Check(caller))
????????????{
????????????????CallerCount(caller);
????????????????if?(Instance?==?null)
????????????????{
????????????????????Instance?=?new?Singleton();
????????????????}
????????????????return?Instance;
????????????}
????????????else
????????????????return?null;
????????}
????????//記錄調(diào)用方調(diào)用次數(shù)
????????public?void?CallerCount(string?caller)
????????{
????????????//caller?Count++
????????}
????????public?bool?Check(string?caller)
????????{
????????????return?true;
????????}
????}
?歡迎一起討論!
?--------------------------補(bǔ)充-------------------------------
我把幾種流行的 Singleton 寫法發(fā)出來,省的大家再去查資料。
??public?sealed?class?MySingleton????{
????????static?MySingleton?instance?=?null;
????????MySingleton()?{?}
????????//簡(jiǎn)單寫法
????????public?static??MySingleton?Istance?
????????{
????????????get
????????????{
????????????????if?(instance?==?null)
????????????????{
????????????????????instance?=?new?MySingleton();
????????????????}
????????????????return?instance;
????????????}
????????}
????????//線程安全
????????static?readonly?object?obj?=?new?object();
????????public?static?MySingleton?SafeInstance
????????{
????????????get
????????????{
????????????????lock?(obj)
????????????????{
????????????????????if?(instance?==?null)
????????????????????????instance?=?new?MySingleton();
????????????????????return?instance;
????????????????}
????????????}
????????}
????????//雙重鎖定?節(jié)約開銷
????????public?static?MySingleton?LockInstance
????????{
????????????get
????????????{
????????????????if?(instance?==?null)
????????????????{
????????????????????lock?(obj)
????????????????????{
????????????????????????if?(instance?==?null)
????????????????????????????instance?=?new?MySingleton();
????????????????????}
????????????????}
????????????????return?instance;
????????????}
????????}
????????//靜態(tài)初始化
????????static?MySingleton()?{?}
????????static?readonly?MySingleton?staticinstance?=?new?MySingleton();
????????public?static?MySingleton?StaticInstance
????????{
????????????get
????????????{
????????????????return?staticinstance;
????????????}
????????}
????????//延遲初始化
????????public?static?MySingleton?lazyInstance
????????{
????????????get
????????????{
????????????????return?Lazy.staticinstance;
????????????}
????????}
????????class?Lazy
????????{
????????????internal?static?readonly?MySingleton?staticinstance?=?new?MySingleton();
????????????static?Lazy()?{?}
????????}
????}
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/tenghoo/archive/2010/08/18/tenghoo_Singleton.html
總結(jié)
以上是生活随笔為你收集整理的设计模式研究(二)-Singleton的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Flex学习笔记2010-08-16
- 下一篇: VB.NET(2005)中关于dll调用