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