Singleton

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class {
private volatile static Singleton uniqueInstance;
private () {}
private static Singleton getInstance() {
if (uniqueInstance == null) {
synchronized (Singleton.class) {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
}
}
return uniqueInstance;
}
}
1
2
3
4
5
6
7
8
9
10
//急切模式
public class {
private static Singleton uniqueInstance = new Singleton();
private () {}
private static Singleton getInstance() {
return uniqueInstance;
}
}