国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

設計模式C#實現(六)——單例模式

2019-11-14 15:48:45
字體:
來源:轉載
供稿:網友

單例模式:保證一個類僅有一個實例,并提供一個訪問它的全局訪問點。

 

構成:

1.私有的構造函數

2.私有靜態的實例

3.返回實例的靜態方法

  public class Singleton    {        PRivate static Singleton uniqueInstance = new Singleton();        private Singleton() { Console.WriteLine("this is a new singleton"); }        public static Singleton getInstance()        {            if (uniqueInstance == null)            {                return uniqueInstance;            }            return uniqueInstance;        }    }

 

這種叫做餓漢模式,實例在類加載時就創建了,缺點是如果實例如果消耗大量的資源而沒有使用就會造成浪費,另一種懶漢模式,實例在被使用時才創建,

  public class Singleton    {        private static Singleton uniqueInstance;        private Singleton() { Console.WriteLine("this is a new singleton"); }        public static Singleton getInstance()        {            if (uniqueInstance == null)            {                return uniqueInstance = new Singleton();            }            return uniqueInstance;        }    }

但是這不是線程安全的

例如

class Program {             static void Main(string[] args) {            while (true) {                Thread t1 = new Thread(Test);                t1.Start();            }        }        static void Test() {            Singleton s = Singleton.getInstance();        }           }

執行的結果有可能是這樣

程序創建了多個實例,這不是我們想要的結果,原因是某個線程if (uniqueInstance == null)語句執行后讓出了使用權,當它重新獲得CPU使用權的時候,可能別的CPU已經創建了實例,而它并不知道,繼續執行return uniqueInstance= new Singleton();導致出現多個實例。

因此,要為方法加鎖

  public class Singleton    {        private static Singleton uniqueInstance;        private Singleton() { Console.WriteLine("this is a new singleton"); }        private static readonly object syncRoot = new object();        public static Singleton getInstance()        {            lock (syncRoot)            {                if (uniqueInstance == null)                {                      return uniqueInstance = new Singleton();                }            }            return uniqueInstance;        }    }

但是這又帶來了一個問題,在實例已經創建完成了,但還是會有大量的線程卡在lock (syncRoot),它們都還會嘗試創建實例,這降低了性能

為此,還要為此方法創建另外一個驗證

 public static Singleton getInstance()        {            if (uniqueInstance == null)            {                lock (syncRoot)                {                    if (uniqueInstance == null)                    {                       return uniqueInstance = new Singleton();                    }                }            }            return uniqueInstance;        }

此時,當實例已經創建完成之后,各線程不再訪問臨界區,提高了性能

 

單例模式和靜態類的比較

1.單例模式可以懶加載,靜態類執行更快(為什么?),即在不同條件下二者有不同的性能表現

2.單例可以繼承和override

3.單例易于模擬,利于測試

4.單例利于維護狀態信息

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 荆门市| 岐山县| 梓潼县| 巴林左旗| 沁阳市| 瑞丽市| 清涧县| 兴山县| 聂荣县| 巫山县| 夏邑县| 洞头县| 唐海县| 夹江县| 贵溪市| 延吉市| 阿克苏市| 济南市| 延庆县| 漯河市| 浪卡子县| 正镶白旗| 德昌县| 剑河县| 七台河市| 德安县| 沙雅县| 东乌珠穆沁旗| 宾川县| 工布江达县| 石林| 淄博市| 南昌市| 离岛区| 临洮县| 历史| 松原市| 抚顺市| 安陆市| 蒲城县| 城步|