這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)C#中怎么實(shí)現(xiàn)一個(gè)單例類(lèi),文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
實(shí)現(xiàn)1:懶漢式,線(xiàn)程不安全
該實(shí)現(xiàn)沒(méi)有額外開(kāi)銷(xiāo),不要求線(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:懶漢式,線(xiàn)程安全
由于每次訪(fǎng)問(wèn)單例類(lèi)實(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:餓漢式,線(xiàn)程安全
寫(xiě)法簡(jiǎn)單,線(xià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í)加鎖,提高訪(fǎng)問(wèn)性能:
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)部類(lèi)
在方法3的基礎(chǔ)上進(jìn)行改進(jìn),確保只有訪(fǎng)問(wè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)單例基類(lèi)
通過(guò)單例基類(lèi),我們可以簡(jiǎn)單的通過(guò)繼承創(chuàng)建一個(gè)單例類(lèi),實(shí)現(xiàn)代碼復(fù)用:
// 由于單例基類(lèi)不能實(shí)例化,故設(shè)計(jì)為抽象類(lèi)public abstract class Singleton<T> where T : class{ // 這里采用實(shí)現(xiàn)5的方案,實(shí)際可采用上述任意一種方案 class Nested { // 創(chuàng)建模板類(lèi)實(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ù)私有化,防止外部通過(guò)new創(chuàng)建 private TestSingleton() { }}
上述就是小編為大家分享的C#中怎么實(shí)現(xiàn)一個(gè)單例類(lèi)了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文題目:C#中怎么實(shí)現(xiàn)一個(gè)單例類(lèi)-創(chuàng)新互聯(lián)
文章路徑:http://chinadenli.net/article20/deseco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、商城網(wǎng)站、ChatGPT、全網(wǎng)營(yíng)銷(xiāo)推廣、外貿(mào)建站、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容