概述
Singleton模式要求一个类有且仅有一个实例,并且提供了一个全局的访问点。这就提出了一个问题:如何绕过常规的构造器,提供一种机制来保证一个类只有一个实例?客户程序在调用某一个类时,它是不会考虑这个类是否只能有一个实例等问题的,所以,这应该是类设计者的责任,而不是类使用者的责任。
从另一个角度来说,Singleton模式其实也是一种职责型模式。因为我们创建了一个对象,这个对象扮演了独一无二的角色,在这个单独的对象实例中,它集中了它所属类的所有权力,同时它也肩负了行使这种权力的职责!
意图
保证一个类仅有一个实例,并提供一个访问它的全局访问点。
模型图
逻辑模型图:

物理模型图:<Design Pattern>Singleton示例

比较:
我们先对四种方式针对它们的优缺点进行一个比较:
方法一:
public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
private Singleton(){}
public static Singleton Instance
{
get
{
return instance;
}
}
}
优点:简单明了
缺点:耗费资源
方法二:
public sealed class ClassicSingleton
{
private static ClassicSingleton instance;
private static object syncRoot = new Object();
private ClassicSingleton() { }
public static ClassicSingleton Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
{
//...custom code
instance = new ClassicSingleton();
}
}
}
return instance;
}
}
}
优点:节省资源
缺点:代码冗长
方法三:
public sealed class Singleton
{
static Singleton(){Instance = new Singleton();}
private Singleton(){}
public static Singleton Instance{get; private set;}
}
优点:既节省资源,又简单明了
缺点:线程不安全
方法四:
public class Singleton
{
private static Singleton instance;
// Added a static mutex for synchronising use of instance.
private static System.Threading.Mutex mutex;
private Singleton() { }
static Singleton()
{
instance = new Singleton();
mutex = new System.Threading.Mutex();
}
public static Singleton Acquire()
{
mutex.WaitOne();
return instance;
}
// Each call to Acquire() requires a call to Release()
public static void Release()
{
mutex.ReleaseMutex();
}
}
优点:既节省资源,又简单明了,线程也安全了(一箭三雕)
缺点:轻微冗长
以下是我们的示例:
在玩网游时,计算玩家在线人数,因为游对有很多大区,这时我们要用到单例模式.
首先新建Example.cs:
public sealed class Example
{
/// <summary>
/// 定义一个静态的Example
/// </summary>
private static Example SingleExample=new Example ();
private int SumCount = 0;
//私有构造函数
private Example()
{
/////线程延迟2000毫秒
Thread.Sleep(2000);
}
//析构函数,避免最终都没有执行Dispose
~Example()
{
this.Dispose();
}
private void Dispose()
{
SingleExample = null;
}
/// <summary>
/// 获取Example类型
/// </summary>
/// <returns></returns>
public static Example GetExample()
{
if (SingleExample == null)
SingleExample = new Example();
return SingleExample;
}
/// <summary>
/// 记数加1
/// </summary>
public void AddCount()
{
SumCount++;
}
/// <summary>
/// 返回记数
/// </summary>
/// <returns></returns>
public int GetCount()
{
return SumCount;
}
}
然后新建CountUserComeIn.cs:
public class CountUserComeIn
{
/// <summary>
/// 用于返回信息
/// </summary>
public StringBuilder strBuilder = new StringBuilder();
public CountUserComeIn()
{
}
/// <summary>
/// 调用Example
/// </summary>
public void GetUserComeIn()
{
Example example = Example.GetExample();
for (int i = 1; i <= 5; i++)
{
example.AddCount();
strBuilder.AppendLine("现在的大区是:" + Thread.CurrentThread.Name);
strBuilder.AppendLine("现在共有" + example.GetCount() + "个玩家进入系统.");
}
}
/// <summary>
/// 新建三个实例
/// </summary>
public void Start()
{
Thread thread = Thread.CurrentThread;
thread.Name = "华北区";
Thread threadone = new Thread(new ThreadStart(GetUserComeIn));
threadone.Name = "华东区";
Thread threadtwo = new Thread(new ThreadStart(GetUserComeIn));
threadtwo.Name = "华南区";
Thread threadthree = new Thread(new ThreadStart(GetUserComeIn));
threadthree.Name = "华中区";
threadone.Start();
threadtwo.Start();
threadthree.Start();
GetUserComeIn();
}
}
然后调用:
public partial class Run : Form
{
public Run()
{
InitializeComponent();
}
private void btnRun_Click(object sender, EventArgs e)
{
//ISaaSProcess process = new EmailEngine();
//process.StartProcess();
//process.StopProcess();
//foreach (string result in process.GetResult())
//{
// rtbResult.AppendText(result + "\n");
//}
//BuyComputer myBuy = new BuyComputer(new lenovo());
// rtbResult.AppendText(myBuy.ShowComputerConfigure());
//myBuy = new BuyComputer(new HP());
// rtbResult.AppendText(myBuy.ShowComputerConfigure());
CountUserComeIn CountUser = new CountUserComeIn();
CountUser.Start();
rtbResult.AppendText(CountUser.strBuilder.ToString());
}
}