单例模式, 保证一个类仅有一个实例, 并提供一个访问他的全局访问点.
class Singleton {
private static Singleton instance;
private Singleton() {
}
public static Singleton GetInstance() {
if(instance == null) {
instance = new Singleton();
}
return instance;
}
}
class Singleton {
private static Singleton instance;
private static readonly object syncRoot = new object();
private Singleton() {
}
public static Singleton GetInstance() {
lock(syncRoot) {
if(instance == null) {
instance = new Singleton();
}
}
return instance;
}
}
这样可以保证不会有多个实例的生成, 但是在每次调用GetInstance的时候都会调用lock, 会影响性能.
class Singleton {
private static Singleton instance;
private static readonly object syncRoot = new Object();
private Singleton() {
}
public static Singleton GetInstance() {
if(instance == null) {
lock(syncRoot) {
if(instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
// sealed阻止发生派生, 而派生可能会增加实例
public sealed class Singleton {
// 在第一次引用类的任何成员时创建实例
// 公共语言运行库负责处理变量初始化
private static readonly Singleton instance = new Singleton();
private Singleton() {
}
public static Singleton GetSintance() {
return instance;
}
}