-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleton.java
More file actions
76 lines (72 loc) · 1.82 KB
/
Copy pathSingleton.java
File metadata and controls
76 lines (72 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
public class Singleton {
private static Singleton instance=null;
private Singleton(){
}
private static synchronized void syncInitSigleton(){
if(instance==null){
instance=new Singleton();
}
}
public static Singleton getInstance(){
if(instance==null){
syncInitSigleton();
}
return instance;
}
public Object readResolve(){
return instance;
}
public void doSomething(){
System.out.println("This is a singleton.");
}
}
/*
//内部类实现的单例模式
public class Singleton {
// 私有构造方法,防止被实例化
private Singleton() {
}
// 此处使用一个内部类来维护单例
private static class SingletonFactory {
private static Singleton instance = new Singleton();
}
// 获取实例
public static Singleton getInstance() {
return SingletonFactory.instance;
}
// 如果该对象被用于序列化,可以保证对象在序列化前后保持一致
public Object readResolve() {
return getInstance();
}
public void doSomething(){
System.out.println("This is a singleton.");
}
}
//影子实例实现对单例对象属性的同步更新
public class Singleton {
private static Singleton instance = null;
private Vector properties = null;
public Vector getProperties() {
return properties;
}
private Singleton() {
}
private static synchronized void syncInit() {
if (instance == null) {
instance = new Singleton();
}
}
public static Singleton getInstance() {
if (instance == null) {
syncInit();
}
return instance;
}
public void doSomething(){
System.out.println("This is a singleton.");
}
public void updateProperties() {
Singleton shadow = new Singleton();
properties = shadow.getProperties();
}
} */