雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

C#單例類的實(shí)現(xiàn)方法

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊:

單例類保證一個(gè)類全局僅有一個(gè)實(shí)例,并提供一個(gè)全局訪問點(diǎn),由于只能生成一個(gè)實(shí)例,因此我們必須把構(gòu)造函數(shù)設(shè)為私有函數(shù)以禁止他人創(chuàng)建實(shí)例。

實(shí)現(xiàn)1:懶漢式,線程不安全

該實(shí)現(xiàn)沒有額外開銷,不要求線程安全的情況下可以使用:

public class Singleton1
{
  private static Singleton1 instance = null;
  private Singleton1() { }

  public static Singleton1 Instance
  {
    get
    {
      if (instance == null)
      {
        instance = new Singleton1();
      }
      return instance;
    }
  }
}

實(shí)現(xiàn)2:懶漢式,線程安全

由于每次訪問單例類實(shí)例都會(huì)加鎖,而加鎖是一個(gè)非常耗時(shí)的操作,故不推薦使用:

public class Singleton2
{
  private readonly static object lockObj = new object();
  private static Singleton2 instance = null;
  private Singleton2() { }

  public static Singleton2 Instance
  {
    get
    {
      lock(lockObj)
      {
        if (instance == null)
        {
          instance = new Singleton2();
        }
      }
      return instance;
    }
  }
}

實(shí)現(xiàn)3:餓漢式,線程安全

寫法簡(jiǎn)單,線程安全,但構(gòu)造時(shí)機(jī)不是由程序員掌控的:

public class Singleton3
{
  private static Singleton3 instance = new Singleton3();
  private Singleton3() { }
  public static Singleton3 Instance { get { return instance; } }

  public static void Test()
  {
    Console.WriteLine("test");
  }
}

當(dāng).NET運(yùn)行時(shí)發(fā)現(xiàn)第一次使用Singleton3時(shí)會(huì)創(chuàng)建單例的實(shí)例,而不是在第一次調(diào)用Singleton3.Instance屬性時(shí)創(chuàng)建,如進(jìn)行以下操作:

Singleton3.Test();

實(shí)現(xiàn)4:懶漢式,雙重校驗(yàn)鎖

在實(shí)現(xiàn)2的基礎(chǔ)上進(jìn)行改進(jìn),只在第一次創(chuàng)建實(shí)例時(shí)加鎖,提高訪問性能:

public class Singleton4
{
  private readonly static object lockObj = new object();
  private static Singleton4 instance = null;
  private Singleton4() { }

  public static Singleton4 Instance
  {
    get
    {
      if (instance == null)
      {
        lock (lockObj)
        {
          if (instance == null)
          {
            instance = new Singleton4();
          }
        }
      }
      return instance;
    }
  }
}

實(shí)現(xiàn)5:懶漢式,內(nèi)部類

在方法3的基礎(chǔ)上進(jìn)行改進(jìn),確保只有訪問Singleton5.Instance屬性時(shí)才會(huì)構(gòu)造實(shí)例:

public class Singleton5
{
  class Nested 
  {
    internal static readonly Singleton5 instance = new Singleton5();
  }
  private Singleton5() { }
  public static Singleton5 Instance { get { return Nested.instance; } }
}

實(shí)現(xiàn)單例基類

通過單例基類,我們可以簡(jiǎn)單的通過繼承創(chuàng)建一個(gè)單例類,實(shí)現(xiàn)代碼復(fù)用:

// 由于單例基類不能實(shí)例化,故設(shè)計(jì)為抽象類
public abstract class Singleton<T> where T : class
{
  // 這里采用實(shí)現(xiàn)5的方案,實(shí)際可采用上述任意一種方案
  class Nested
  {
    // 創(chuàng)建模板類實(shí)例,參數(shù)2設(shè)為true表示支持私有構(gòu)造函數(shù)
    internal static readonly T instance = Activator.CreateInstance(typeof(T), true) as T;
  }
  private static T instance = null;
  public static T Instance { get { return Nested.instance; } }
}

使用方法如下:

class TestSingleton : Singleton<TestSingleton>
{
  // 將構(gòu)造函數(shù)私有化,防止外部通過new創(chuàng)建
  private TestSingleton() { }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:ZooKeeper的安裝及部署教程

欄    目:C#教程

下一篇:C#窗體間常用的幾種傳值方式及委托與事件詳解

本文標(biāo)題:C#單例類的實(shí)現(xiàn)方法

本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4738.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有