单实例模式(Singleton)
最近一个人好无聊想写点东西,正好刚学了点设计模式,觉得Singleton这个词最能引起我的共鸣:( 。Single一个人,ton好多人,‘街道上人潮衬托我的孤单,想象,谁幸运陪在你身旁,却误会一场,你也在等你手机响’。晕了, 闲话少说,步入正题。
创建Singleton模式的目的是:‘ensure a class has only one instance, and provide a global point of access to it’. 使用一个全局的静态变量只能解决global access的问题,当然你也不能期望这个类的用户会保证only one instance。所以我们需要控制类实例的创建,并且保证任何时候只有一个实例存在。 下面是c++版本的singleton:
// Declaration
class Singleton {
public:
static Singleton* Instance();
protected:
Singleton();
private:
static Singleton* _instance;
}
// Implementation
Singleton* Singleton::_instance = 0;
Singleton* Singleton::Instance() {
if (_instance == 0) {
_instance = new Singleton;
}
return _instance;
}
class Singleton {
public:
static Singleton* Instance();
protected:
Singleton();
private:
static Singleton* _instance;
}
// Implementation
Singleton* Singleton::_instance = 0;
Singleton* Singleton::Instance() {
if (_instance == 0) {
_instance = new Singleton;
}
return _instance;
}
上面的代码看起来管用,但在多线程环境下会有潜在的危险。如果有两个线程同时进入if语句块,两个实例会被创建。可以使用临界区限制同时访问,但是那样会对性能带来不好的影响。
先看下面的代码:(c#)
class Singleton
{
public static Singleton Instance() {
if (_instance == null) {
lock (typeof(Singleton)) {
if (_instance == null) {
_instance = new Singleton();
}
}
}
return _instance;
}
protected Singleton() {}
private static volatile Singleton _instance = null;
}
{
public static Singleton Instance() {
if (_instance == null) {
lock (typeof(Singleton)) {
if (_instance == null) {
_instance = new Singleton();
}
}
}
return _instance;
}
protected Singleton() {}
private static volatile Singleton _instance = null;
}
上面使用了"double-check"解决了使用临界区对性能的影响,同时又保证了only one instance.注意使用了volatile关键字保证编译器不做优化.
上面的代码还是冗长, 既然用.net, 我们可以充分利用.net给我们带来的方便:
// .NET Singleton
sealed class Singleton
{
private Singleton() {}
public static readonly Singleton Instance = new Singleton();
}
大家可能会怀疑这还是Singleton吗? 先看一个例子:
using System;/// <summary>
/// Sample Singleton class
/// </summary>
sealed class SingletonCounter
{
public static readonly SingletonCounter Instance = new SingletonCounter();
private long Count = 0;private SingletonCounter() { }
public long NextValue()
{
return ++Count;
}
}
class SingletonClient
{
//[STAThread]
static void Main()
{
for (int i = 0; i < 20; i++)
{
Console.WriteLine("Next singleton value: {0}",
SingletonCounter.Instance.NextValue());
}
}
}在vs2005中新建一个c# console项目粘贴上面的代码,ctrl+F5,一切ok. 下面解释一下,首先是only one instance,.net 在JIT阶段, 静态变量只有在使用时才被实例化, 关于线程安全,.net framework保证了静态变量初始化安全.