您好,登錄后才能下訂單哦!
只能生成一個實例的類是實現了Singleton(單例)模式的類。以下為C#實現單例模式的方式。 方式一只使用于單線程環境 // 把構造函數設為私有函數以禁止他人創建實例 // 定義一個靜態的實例在需要的時候創建該實例 // 在Singlrton的靜態屬性Instance中只有在instance為null的時候才創建一個實例以避免 // 重復創建 // 把構造函數定義為私有函數 public sealed class Singleton1 { public int a = 2; private Singleton1() { } private static Singleton1 instance = null; public static Singleton1 Instance { get { if (instance == null) instance = new Singleton1(); return instance; } } } 方式二雖然在多線程環境中能工作但效率不高 // 每次通過屬性Instance得到Singleton2的實例都會試圖加上一個同步鎖 // 而加鎖是一個非常耗時的操作在沒有必要的時候應該盡量避免 public sealed class Singleton2 { public int a = 2; private Singleton2(){} private static readonly object syncObj = new object(); private static Singleton2 instance = null; public static Singleton2 Instance { get { lock (syncObj) { if (instance == null) instance = new Singleton2(); } return instance; } } } 可行的解法 加同步鎖前后兩次判斷實例是否已存在 // 只有instance為null即沒有創建時需要加鎖操作。 public sealed class Singleton3 { private Singleton3() { } private static readonly Object syncObj = new Object(); private static Singleton3 instance = null; public static Singleton3 Instance { get { if(instance == null) { lock(syncObj) { if(instance == null) instance = new Singleton3(); } } return instance; } } } 推薦的解法一利用靜態構造函數 // 在初始化靜態變量instance的時候創建一個實例 // 由于C#是在調用靜態構造函數時初始化靜態變量.NET運行時能夠確保只調用一次靜態構造 // 函數保證只初始化一次instance public sealed class Singleton4 { private Singleton4() { } private static Singleton4 instance = new Singleton4(); public static Singleton4 Instance { get { return instance; } } } 推薦的解法二 實現按需創建實例 // 在內部定義了一個私有類型Nested。 // 當第一次用到這個嵌套類的時候會調用靜態構造函數創建Singleton5的實例instance public sealed class Singleton5 { private Singleton5() { } public static Singleton5 Instance { get { return Nested.instance; } } class Nested { static Nested() { } internal static readonly Singleton5 instance = new Singleton5(); } } 擴展 定義一個表示總統的類型President可以從該類型繼承出FrenchPresident 和AmericanPresident等類型。這些派生類型都只能產生一個實例 public class President { private string name = ""; private string country = ""; public President() { } public string Name { get { return name; } set { name = value; } } public string Country { get { return country; } set { country = value; } } } public sealed class FrenchPresident: President { private FrenchPresident():base() { } private static FrenchPresident instance = new FrenchPresident(); public static FrenchPresident Instance { get { return (FrenchPresident)(Nested.instance); } } private class Nested { static Nested() { } internal static readonly FrenchPresident instance = new FrenchPresident(); } } public sealed class AmericanPresident : President { private AmericanPresident() : base() { } private static AmericanPresident instance = new AmericanPresident(); public static AmericanPresident Instance { get { return Nested.instance; } } private class Nested { static Nested() { } internal static readonly AmericanPresident instance = new AmericanPresident(); } } 實現泛型單例模式 public class SingletonExample<T> where T : class, new() { public static T Instance { get { return Nested.instance; } } private class Nested { static Nested() { } internal static readonly T instance = new T(); } } public class Two: SingletonExample<Two> { public int a = 2; public void Show() { Console.WriteLine(a); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。