只能生成一個實例的類是實現(xiàn)了Singleton(單例)模式的類。以下為C#實現(xiàn)單例模式的方式。
方式一只使用于單線程環(huán)境
// 把構造函數(shù)設為私有函數(shù)以禁止他人創(chuàng)建實例
// 定義一個靜態(tài)的實例在需要的時候創(chuàng)建該實例
// 在Singlrton的靜態(tài)屬性Instance中只有在instance為null的時候才創(chuàng)建一個實例以避免
// 重復創(chuàng)建
// 把構造函數(shù)定義為私有函數(shù)
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; } } } 方式二雖然在多線程環(huán)境中能工作但效率不高
// 每次通過屬性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即沒有創(chuàng)建時需要加鎖操作。
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;
}
}
}
推薦的解法一利用靜態(tài)構造函數(shù)
// 在初始化靜態(tài)變量instance的時候創(chuàng)建一個實例
// 由于C#是在調用靜態(tài)構造函數(shù)時初始化靜態(tài)變量.NET運行時能夠確保只調用一次靜態(tài)構造
// 函數(shù)保證只初始化一次instance
public sealed class Singleton4
{
private Singleton4() { }
private static Singleton4 instance = new Singleton4();
public static Singleton4 Instance
{
get
{
return instance;
}
}
}
推薦的解法二 實現(xiàn)按需創(chuàng)建實例
// 在內部定義了一個私有類型Nested。
// 當?shù)谝淮斡玫竭@個嵌套類的時候會調用靜態(tài)構造函數(shù)創(chuàng)建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();
}
}
擴展 定義一個表示總統(tǒng)的類型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();
}
}
實現(xiàn)泛型單例模式
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);
}
}創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網絡助力業(yè)務部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準確進行流量調度,確保服務器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務器買多久送多久。

網站欄目:C#的單例模式實現(xiàn)-創(chuàng)新互聯(lián)
文章鏈接:http://www.chinadenli.net/article18/dpsegp.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供關鍵詞優(yōu)化、網站內鏈、品牌網站制作、App開發(fā)、全網營銷推廣、網站設計公司
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)