C#單例模式Singleton的實(shí)現(xiàn)詳解
一、簡介
1、什么是單例模式
一句話解釋:
單一的類,只能自己來創(chuàng)建唯一的一個(gè)對象。
單例模式(Singleton Pattern)是日常開發(fā)中最簡單的設(shè)計(jì)模式之一。這種類型的設(shè)計(jì)模式屬于創(chuàng)建型模式,它提供了一種創(chuàng)建對象的最佳方式。
這種模式涉及到一個(gè)單一的類,該類負(fù)責(zé)創(chuàng)建自己的對象,同時(shí)確保只有一個(gè)對象被創(chuàng)建。這個(gè)類提供了一種訪問其唯一的對象的方式,可以直接訪問,不需要實(shí)例化該類的對象。
一個(gè)比喻:(班主任與學(xué)生)
比如一個(gè)班級,只有一個(gè)班主任,任何一個(gè)同學(xué)要找班主任,都是找的同一個(gè),班主任忙的時(shí)候,當(dāng)然就出現(xiàn)排隊(duì)的情況。
2、優(yōu)缺點(diǎn)和使用場景
- 優(yōu)點(diǎn):內(nèi)存里只有一個(gè)實(shí)例,減少了內(nèi)存的開銷,也避免了對象高頻創(chuàng)建帶來的性能損耗。
- 缺點(diǎn):任務(wù)量大時(shí),會出現(xiàn)排隊(duì),耗時(shí)增加。另外與單一職責(zé)原則沖突,一個(gè)類應(yīng)該只關(guān)心內(nèi)部邏輯,而不關(guān)心外面怎么樣來實(shí)例化。
使用場景舉例:
- 要求生產(chǎn)唯一序列號。
- WEB 中的計(jì)數(shù)器,不用每次刷新都在數(shù)據(jù)庫里加一次,用單例先緩存起來。
- 創(chuàng)建的一個(gè)對象需要消耗的資源過多,比如 I/O 與數(shù)據(jù)庫的連接等。
二、單例模式簡單實(shí)現(xiàn)
public class Singleton { private static Singleton instance = null; private static object lockObject = new object(); /// <summary> /// 私有化構(gòu)造函數(shù),防止外部實(shí)例化 /// </summary> private Singleton() { } public static Singleton Instance { get { if (instance == null) { lock (lockObject) // 線程同步鎖 { if (instance == null) // Lazy Initialization { instance = new Singleton(); } } } return instance; } } /// <summary> /// 重置 Singleton /// </summary> public void Reset() { instance = null; } }
測試代碼:
static void Main(string[] args) { var instance1 = Singleton.Instance; var instance2 = Singleton.Instance; Console.WriteLine(instance1 == instance2); // 輸出 true }
三、帶參數(shù)的單例模式實(shí)現(xiàn)
public class SingletonParameters { private static SingletonParameters instance = null; private static object lockObject = new object(); private int _firstvalue, _secondvalue; /// <summary> /// 私有化構(gòu)造函數(shù),防止外部實(shí)例化 /// </summary> private SingletonParameters(int first, int second) { this._firstvalue = first; this._secondvalue = second; } public static SingletonParameters InstanceParameters(int first, int second) { if (instance == null) { lock (lockObject) // 線程同步鎖 { if (instance == null) // Lazy Initialization { instance = new SingletonParameters(first, second); } } } else { instance.FirstValue = first; instance.SecondValue = second; } return instance; } public int FirstValue { get { return _firstvalue; } set { _firstvalue = value; } } public int SecondValue { get { return _secondvalue; } set { _secondvalue = value; } } /// <summary> /// 重置 Singleton /// </summary> public void Reset() { instance = null; } }
測試代碼:
var instance1 = SingletonParameters.InstanceParameters(1, 2); Console.WriteLine($"FirstValue:{instance1.FirstValue}"); Console.WriteLine($"SecondValue:{instance1.SecondValue}"); var instance2 = SingletonParameters.InstanceParameters(3, 4); Console.WriteLine($"FirstValue:{instance2.FirstValue}"); Console.WriteLine($"SecondValue:{instance2.SecondValue}"); Console.WriteLine($"instance1 == instance2 : {instance1 == instance2}");
到此這篇關(guān)于C#單例模式Singleton的實(shí)現(xiàn)詳解的文章就介紹到這了,更多相關(guān)C#單例模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在C#中g(shù)lobal關(guān)鍵字的作用及其用法
global 是 C# 2.0 中新增的關(guān)鍵字,理論上說,如果代碼寫得好的話,根本不需要用到它,但是不排除一些特別的情況,比如修改別人的代碼,本文僅舉例說明。2016-03-03C#條件編譯、內(nèi)聯(lián)函數(shù)、CLS介紹
這篇文章介紹了C#的條件編譯、內(nèi)聯(lián)函數(shù)、CLS,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03