聊聊如何使用單例模式
本文轉(zhuǎn)載自微信公眾號(hào)「UP技術(shù)控」,作者conan5566。轉(zhuǎn)載本文請(qǐng)聯(lián)系UP技術(shù)控公眾號(hào)。
介紹
單例模式,顧名思義就是只有一個(gè)實(shí)例,并且她自己負(fù)責(zé)創(chuàng)建自己的對(duì)象,這個(gè)類提供了一種訪問其唯一的對(duì)象的方式,可以直接訪問,不需要實(shí)例化該類的對(duì)象。下面我們來看下有哪幾種實(shí)現(xiàn)方式吧。
實(shí)現(xiàn)方式
1、使用lock ,保證方法只有一個(gè)線程可以進(jìn)入。
- /// <summary>
 - /// 單例類
 - /// </summary>
 - public class Singleton
 - {
 - private Singleton()
 - {
 - long lResult = 0;
 - for (int i = 0; i < 100 - 000 - 000; i++)
 - {
 - lResult += i;
 - }
 - Thread.Sleep(2000);
 - Console.WriteLine($"{this.GetType().Name}完成構(gòu)造....");
 - }
 - private static Singleton Instance = null;
 - private static readonly object Singleton_Lock = new object();
 - public static Singleton CreateInstance()
 - {
 - if (Instance == null)
 - {
 - lock (Singleton_Lock)//保證方法塊兒只有一個(gè)線程可以進(jìn)入
 - {
 - Console.WriteLine("進(jìn)入lock排隊(duì)....");
 - Thread.Sleep(1000);
 - if (Instance == null)
 - Instance = new Singleton();
 - }
 - }
 - return Instance;
 - }
 - public static void DoNothing()
 - {
 - Console.WriteLine("DoNothing");
 - }
 - public void Show()
 - {
 - Console.WriteLine($"{this.GetType().Name} Show....");
 - }
 - }
 
2、使用靜態(tài)構(gòu)造函數(shù),由CLR調(diào)用,在類型第一次被使用前調(diào)用,且只調(diào)用一次。
- /// <summary>
 - /// 單例類
 - /// </summary>
 - public class SingletonSecond
 - {
 - private SingletonSecond()
 - {
 - long lResult = 0;
 - for (int i = 0; i < 100 - 000 - 000; i++)
 - {
 - lResult += i;
 - }
 - Thread.Sleep(2000);
 - Console.WriteLine($"{this.GetType().Name}完成構(gòu)造....");
 - }
 - private static SingletonSecond Instance = null;
 - /// <summary>
 - /// 靜態(tài)構(gòu)造函數(shù),由CLR調(diào)用,在類型第一次被使用前調(diào)用,且只調(diào)用一次!
 - /// </summary>
 - static SingletonSecond()
 - {
 - Instance = new SingletonSecond();
 - }
 - public static SingletonSecond CreateInstance()
 - {
 - return Instance;
 - }
 - public static void DoNothing()
 - {
 - Console.WriteLine("DoNothing");
 - }
 - public void Show()
 - {
 - Console.WriteLine($"{this.GetType().Name} Show....");
 - }
 - }
 
3、使用靜態(tài)字段,由CLR調(diào)用,在類型第一次被使用前初始化,且只初始化一次。
- /// <summary>
 - /// 單例類
 - /// </summary>
 - public class SingletonThird
 - {
 - private SingletonThird()
 - {
 - long lResult = 0;
 - for (int i = 0; i < 100 - 000 - 000; i++)
 - {
 - lResult += i;
 - }
 - Thread.Sleep(2000);
 - Console.WriteLine($"{this.GetType().Name}完成構(gòu)造....");
 - }
 - /// <summary>
 - /// 靜態(tài)字段,由CLR調(diào)用,在類型第一次被使用前初始化,且只初始化一次!
 - /// </summary>
 - private static SingletonThird Instance = new SingletonThird();
 - public static SingletonThird CreateInstance()
 - {
 - return Instance;
 - }
 - public static void DoNothing()
 - {
 - Console.WriteLine("DoNothing");
 - }
 - public int iNum = 0;
 - public void Show()
 - {
 - Console.WriteLine($"{this.GetType().Name} Show..{iNum++}..");
 - }
 - public void Add()
 - {
 - this.iNum++;
 - }
 - }
 
使用場(chǎng)景
1、需要生成唯一序列的環(huán)境。
2、需要頻繁實(shí)例化然后銷毀的對(duì)象。
3、創(chuàng)建對(duì)象時(shí)耗時(shí)過多或者耗資源過多,但又經(jīng)常用到的對(duì)象。
4、方便資源相互通信的環(huán)境。















 
 
 










 
 
 
 