c# 單例模式的實現(xiàn)方法
單例模式大概是所有設(shè)計模式中最簡單的一種,如果在面試時被問及熟悉哪些設(shè)計模式,你可能第一個答的就是單例模式。
單例模式的實現(xiàn)分為兩種:餓漢式和懶漢式。前者是在靜態(tài)構(gòu)造函數(shù)執(zhí)行時就立即實例化,后者是在程序執(zhí)行過程中第一次需要時再實例化。兩者有各自適用的場景,實現(xiàn)方式也都很簡單,唯一在設(shè)計時要考慮的一個問題就是:實例化時需要保證線程安全。
餓漢式
餓漢式實現(xiàn)很簡單,在靜態(tài)構(gòu)造函數(shù)中立即進行實例化:
public class Singleton { private static readonly Singleton _instance; static Singleton() { _instance = new Singleton(); } public static Singleton Instance { get { return _instance; } } }
注意,為了確保單例性,需要使用 readonly 關(guān)鍵字聲明實例不能被修改。
以上寫法可簡寫為:
public class Singleton { private static readonly Singleton _instance = new Singleton(); public static Singleton Instance { get { return _instance; } } }
這里的 new Singleton()
等同于在靜態(tài)構(gòu)造函數(shù)中實例化。在 C# 7 中還可以進一步簡寫如下:
public class Singleton { public static Singleton Instance { get; } = new Singleton(); }
一句代碼就搞定了,此寫法,實例化也是在默認的靜態(tài)構(gòu)造函數(shù)中進行的。如果是餓漢式需求,這種實現(xiàn)是最簡單的。有人會問這會不會有線程安全問題,如果多個線程同時調(diào)用 Singleton.Instance 會不會實例化了多個實例。不會,因為 CLR 確保了所有靜態(tài)構(gòu)造函數(shù)都是線程安全的。
注意,不能這么寫:
public class Singleton { public static Singleton Instance => new Singleton(); } // 等同于: public class Singleton { public static Singleton Instance { get { return new Singleton(); } } }
這樣會導(dǎo)致每次調(diào)用都會創(chuàng)建一個新實例。
懶漢式
懶漢式單例實現(xiàn)需要考慮線程安全問題,先來看一段經(jīng)典的線程安全的單列模式實現(xiàn)代碼:
public sealed class Singleton { private static volatile Singleton _instance; private static readonly object _lockObject = new Object(); public static Singleton Instance { get { if (_instance == null) { lock (_lockObject) { if (_instance == null) { _instance = new Singleton(); } } } return _instance; } } }
網(wǎng)上搜索 C# 單例模式,大部分都是這種使用 lock 來確保線程安全的寫法,這是經(jīng)典標準的單例模式的寫法,沒問題,很放心。在 lock 里外都做一次 instance 空判斷,雙保險,足以保證線程安全和單例性。但這種寫法似乎太麻煩了,而且容易寫錯。早在 C# 3.5 的時候,就有了更好的寫法,使用 Lazy<T>。
示例代碼:
public class LazySingleton { private static readonly Lazy<LazySingleton> _instance = new Lazy<LazySingleton>(() => new LazySingleton()); public static LazySingleton Instance { get { return _instance.Value; } } }
調(diào)用示例:
public class Program { public static void Main() { var instance = LazySingleton.Instance; } }
使用 Lazy<T> 可以使對象的實例化延遲到第一次被調(diào)用的時候執(zhí)行,通過訪問它的 Value 屬性來創(chuàng)建并獲取實例,并且讀取一個 Lazy<T> 實例的 Value 屬性只會執(zhí)行一次實例化代碼,確保了線程安全。
祝,編碼愉快!
以上就是c# 單例模式的實現(xiàn)方法的詳細內(nèi)容,更多關(guān)于c# 單例模式的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#操作SQLite數(shù)據(jù)庫方法小結(jié)
這篇文章介紹了C#操作SQLite數(shù)據(jù)庫的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06