首页 > 技术文章 > 单例模式

cherious 原文

单例模式(Singleton):保证一个类仅有一个实例,并提供一个访问它的全局访问点

public abstract class Singleton<T> where T:new ()
{
private static T _instance;
private static readonly object obj=new object();

public static T Instance
{
get
{
if (_instance == null)
{
lock (obj)
{
if (_instance == null)
{
_instance=new T();
}
}
}
return _instance;
}
}

}

推荐阅读