单例模式

单例模式, 保证一个类仅有一个实例, 并提供一个访问他的全局访问点.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Singleton {
private static Singleton instance;
private Singleton() {
}
public static Singleton GetInstance() {
if(instance == null) {
instance = new Singleton();
}
return instance;
}
}

多线程时的单例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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, 会影响性能.

双重锁定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
}
}

静态初始化

1
2
3
4
5
6
7
8
9
10
11
12
// sealed阻止发生派生, 而派生可能会增加实例
public sealed class Singleton {
// 在第一次引用类的任何成员时创建实例
// 公共语言运行库负责处理变量初始化
private static readonly Singleton instance = new Singleton();
private Singleton() {
}
public static Singleton GetSintance() {
return instance;
}
}