单例模式,就是同一时间只有一个该类的对象存在
#region 单例模式一
//简单版本 多线程不安全。可以会在同一时间点上都创建一个实例,虽然一般不会出异常错误,但是起码不是我们谈论的只保证一个实例了。
public sealed class Singleton_1
{
private string name;
public string Name
{
set
{
if (string.IsNullOrEmpty(this.name))
{
name = value;
}
}
get
{
return this.name;
}
}
private static Singleton_1 instance;
private Singleton_1(string name)
{
this.Name = name;
}
public static Singleton_1 Instance(string name)
{
if (instance == null) {
instance = new Singleton_1(name);
}
return instance;
}
}
#endregion
#region 单例模式二
// 通过readonly实现单例。
public sealed class Singleton_2
{
private string name;
public string Name
{
set {
if (string.IsNullOrEmpty(this.name))
{
name = value;
}
}
get {
return this.name;
}
}
private Singleton_2(string name)
{
this.Name = name;
}
private static readonly Singleton_2 instance = new Singleton_2("唯一实例");
public static Singleton_2 Instance
{
get {
return instance;
}
}
}
#endregion
#region 单例模式三
// 利用volatile关键字保证volatile在被访问之前被赋值实例。
// 利用lock保证多线程安全。
// 补充: volatile的作用是: 作为指令关键字,确保本条指令不会因编译器的优化而省略,且要求每次直接读值.
public sealed class Singleton_3
{
// 依然是静态自动hold实例
private static volatile Singleton_3 instance = null;
// Lock对象,线程安全所用
private static object syncRoot = new Object();
private Singleton_3() { }
public static Singleton_3 Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new Singleton_3();
}
}
return instance;
}
}
}
#endregion
#region 单例模式四
// 在模式二的基础上加了一个静态构造函数。
public class Singleton_4
{
private static readonly Singleton_4 instance = new Singleton_4();
private Singleton_4() { }
static Singleton_4() { }
// 声明静态构造函数就是为了删除IL里的BeforeFieldInit标记
// 以保证静态自动在使用之前被初始化
public static Singleton_4 Instance
{
get {
return instance;
}
}
}
#endregion
#region 单例模式五
//通过lazy<T>延时初始化,多线程安全,只会初始化一次 以后每次调用都使用它。
public class Singleton_5
{
private static readonly Lazy<Singleton_5> instance = new Lazy<Singleton_5>(() => new Singleton_5());
private Singleton_5() { }
public static Lazy<Singleton_5> Instance
{
get {
return instance;
}
}
}
#endregion
class 单例模式
{
static void Main(string[] args)
{
# region 单例模式一
//Singleton_1 singleton = Singleton_1.Instance("唯一实例");
//Console.WriteLine(singleton.Name);
//Singleton_1 singleton_new = Singleton_1.Instance("另一个实例");
//Console.WriteLine(singleton_new.Name);
#endregion
#region 单例模式二
//Singleton_2 singleton = Singleton_2.Instance;
//Console.WriteLine(singleton.Name);
//singleton.Name = "改名了";
//Console.WriteLine(singleton.Name);
//Singleton_2 singleton2 = Singleton_2.Instance;
//Console.WriteLine(singleton == singleton2);
#endregion
#region 单例模式三
//Singleton_3 singleton = Singleton_3.Instance;
//Singleton_3 singleton2 = Singleton_3.Instance;
//Console.WriteLine(singleton == singleton2);
#endregion
#region 单例模式四
//Singleton_4 singleton = Singleton_4.Instance;
//Singleton_4 singleton2 = Singleton_4.Instance;
//Console.WriteLine(singleton == singleton2);
#endregion
#region 单例模式五
Lazy<Singleton_5> singleton = Singleton_5.Instance;
Lazy<Singleton_5> singleton2 = Singleton_5.Instance;
Console.WriteLine(singleton == singleton2);
#endregion
}
}