只能生成一個(gè)實(shí)例的類是實(shí)現(xiàn)了Singleton(單例)模式的類。以下為C#實(shí)現(xiàn)單例模式的方式。
方式一只使用于單線程環(huán)境
// 把構(gòu)造函數(shù)設(shè)為私有函數(shù)以禁止他人創(chuàng)建實(shí)例
// 定義一個(gè)靜態(tài)的實(shí)例在需要的時(shí)候創(chuàng)建該實(shí)例
// 在Singlrton的靜態(tài)屬性Instance中只有在instance為null的時(shí)候才創(chuàng)建一個(gè)實(shí)例以避免
// 重復(fù)創(chuàng)建
// 把構(gòu)造函數(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的實(shí)例都會試圖加上一個(gè)同步鎖
// 而加鎖是一個(gè)非常耗時(shí)的操作在沒有必要的時(shí)候應(yīng)該盡量避免
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;
}
}
}
可行的解法 加同步鎖前后兩次判斷實(shí)例是否已存在
// 只有instance為null即沒有創(chuàng)建時(shí)需要加鎖操作。
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)構(gòu)造函數(shù)
// 在初始化靜態(tài)變量instance的時(shí)候創(chuàng)建一個(gè)實(shí)例
// 由于C#是在調(diào)用靜態(tài)構(gòu)造函數(shù)時(shí)初始化靜態(tài)變量.NET運(yùn)行時(shí)能夠確保只調(diào)用一次靜態(tài)構(gòu)造
// 函數(shù)保證只初始化一次instance
public sealed class Singleton4
{
private Singleton4() { }
private static Singleton4 instance = new Singleton4();
public static Singleton4 Instance
{
get
{
return instance;
}
}
}
推薦的解法二 實(shí)現(xiàn)按需創(chuàng)建實(shí)例
// 在內(nèi)部定義了一個(gè)私有類型Nested。
// 當(dāng)?shù)谝淮斡玫竭@個(gè)嵌套類的時(shí)候會調(diào)用靜態(tài)構(gòu)造函數(shù)創(chuàng)建Singleton5的實(shí)例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();
}
}
擴(kuò)展 定義一個(gè)表示總統(tǒng)的類型President可以從該類型繼承出FrenchPresident 和AmericanPresident等類型。這些派生類型都只能產(chǎn)生一個(gè)實(shí)例
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();
}
}
實(shí)現(xiàn)泛型單例模式
public class SingletonExample 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
{
public int a = 2;
public void Show()
{
Console.WriteLine(a);
}
}
當(dāng)前名稱:C#的單例模式實(shí)現(xiàn)
標(biāo)題網(wǎng)址:
http://weahome.cn/article/pgdcie.html