單例模式(Singleton Pattern)是 程序開發 中最簡單的設計模式之一。這種類型的設計模式屬於建立型模式,它提供了一種建立對象的最佳方式。設計模式
這種模式涉及到一個單一的類,該類負責建立本身的對象,同時確保只有單個對象被建立。這個類提供了一種訪問其惟一的對象的方式,能夠直接訪問,不須要實例化該類的對象。緩存
注意:多線程
意圖:保證一個類僅有一個實例,並提供一個訪問它的全局訪問點。函數
主要解決:一個全局使用的類頻繁地建立與銷燬。性能
什麼時候使用:當您想控制實例數目,節省系統資源的時候。spa
如何解決:判斷系統是否已經有這個單例,若是有則返回,若是沒有則建立。線程
關鍵代碼:構造函數是私有的。設計
優勢:code
缺點:沒有接口,不能繼承,與單一職責原則衝突,一個類應該只關心內部邏輯,而不關心外面怎麼樣來實例化。對象
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 單例模式演示 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 Singleton obj =Singleton.CreateInstance(); 14 Singleton obj2 = Singleton.CreateInstance(); 15 Singleton obj3 = Singleton.CreateInstance(); 16 Console.ReadKey(); 17 } 18 /// <summary> 19 /// 當把一個類的構造函數的訪問修飾符設爲private後,那麼這個類外部就不能夠被建立對象了 20 /// </summary> 21 public class Singleton 22 { 23 /// <summary> 24 /// 重寫構造函數 25 /// </summary> 26 private Singleton() 27 { 28 Console.WriteLine('.'); 29 } 30 private static Singleton _instance; 31 /// <summary> 32 /// 建立實例 33 /// </summary> 34 /// <returns></returns> 35 public static Singleton CreateInstance() 36 { 37 if (_instance==null) 38 { 39 _instance = new Singleton(); 40 } 41 return _instance; 42 } 43 } 44 } 45 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Threading; 7 8 namespace 單例模式之多線程 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 for (int i = 0; i < 1000; i++) 15 { 16 Thread t = new Thread(new ThreadStart(() => 17 { 18 Singleton s = Singleton.CreateInstance(); 19 })); 20 t.Start(); 21 } 22 Console.WriteLine("ok"); 23 Console.ReadKey(); 24 } 25 /// <summary> 26 /// 當把一個類的構造函數的訪問修飾符設爲private後,那麼這個類外部就不能夠被建立對象了 27 /// </summary> 28 public class Singleton 29 { 30 /// <summary> 31 /// 重寫構造函數 32 /// </summary> 33 private Singleton() 34 { 35 Console.WriteLine("."); 36 } 37 private static Singleton _instance; 38 private static readonly object syn = new object(); 39 /// <summary> 40 /// 建立實例 41 /// </summary> 42 /// <returns></returns> 43 public static Singleton CreateInstance() 44 { 45 lock (syn) //加鎖 46 { 47 if (_instance == null) 48 { 49 _instance = new Singleton(); 50 } 51 } 52 return _instance; 53 } 54 } 55 } 56 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Threading; 7 8 namespace 單例模式之多線程最優方法 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 for (int i = 0; i < 1000; i++) 15 { 16 Thread t = new Thread(new ThreadStart(() => 17 { 18 Singleton s = Singleton.CreateInstance(); 19 })); 20 t.Start(); 21 } 22 Console.WriteLine("ok"); 23 Console.ReadKey(); 24 } 25 /// <summary> 26 /// 當把一個類的構造函數的訪問修飾符設爲private後,那麼這個類外部就不能夠被建立對象了 27 /// </summary> 28 public class Singleton 29 { 30 /// <summary> 31 /// 重寫構造函數 32 /// </summary> 33 private Singleton() 34 { 35 Console.WriteLine("."); 36 } 37 private static Singleton _instance; 38 private static readonly object syn = new object(); 39 /// <summary> 40 /// 建立實例 41 /// </summary> 42 /// <returns></returns> 43 public static Singleton CreateInstance() 44 { 45 if (_instance == null) 46 { 47 lock (syn) //加鎖 48 { 49 if (_instance == null) 50 { 51 _instance = new Singleton(); 52 } 53 } 54 } 55 return _instance; 56 } 57 } 58 } 59 }
項目連接:https://pan.baidu.com/s/1EpGurdSHqzUmEd7Ta6z7mQ
提取碼:fd9s