C#單例類的實現


C#單例類的實現

單例類保證一個類全局僅有一個實例,並提供一個全局訪問點,由於只能生成一個實例,因此我們必須把構造函數設為私有函數以禁止他人創建實例。

實現1:懶漢式,線程不安全

該實現沒有額外開銷,不要求線程安全的情況下可以使用:

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

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

實現2:懶漢式,線程安全

由於每次訪問單例類實例都會加鎖,而加鎖是一個非常耗時的操作,故不推薦使用

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;
        }
    }
}

實現3:餓漢式,線程安全

寫法簡單,線程安全,但構造時機不是由程序員掌控的:

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");
    }
}

當.NET運行時發現第一次使用Singleton3時會創建單例的實例,而不是在第一次調用Singleton3.Instance屬性時創建,如進行以下操作:

Singleton3.Test();

實現4:懶漢式,雙重校驗鎖

在實現2的基礎上進行改進,只在第一次創建實例時加鎖,提高訪問性能:

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;
        }
    }
}

實現5:懶漢式,內部類

在方法3的基礎上進行改進,確保只有訪問Singleton5.Instance屬性時才會構造實例:

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

實現單例基類

通過單例基類,我們可以簡單的通過繼承創建一個單例類,實現代碼復用:

// 由於單例基類不能實例化,故設計為抽象類
public abstract class Singleton<T> where T : class
{
    // 這里采用實現5的方案,實際可采用上述任意一種方案
    class Nested
    {
        // 創建模板類實例,參數2設為true表示支持私有構造函數
        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>
{
    // 將構造函數私有化,防止外部通過new創建
    private TestSingleton() { }
}

參考資料

1、《劍指offer》

2、http://www.runoob.com/design-pattern/singleton-pattern.html

3、https://blog.csdn.net/ycl295644/article/details/46770451


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM