statement:本篇內(nèi)容只是建立在我目前經(jīng)驗的基礎(chǔ)之上,必然有不完善甚至是不正確的地方,請謹(jǐn)慎閱讀,如果能指出錯誤與不足之處,更是不甚感激
一、傳統(tǒng)單例模式的缺點
- 傳統(tǒng)單例模式的常規(guī)寫法
public class SingletonTest {
private static final SingletonTest instance= new SingletonTest ();
private SingletonTest () {}
public static SingletonTest getInstance() {
return instance;
}
}
- 傳統(tǒng)單例模式存在的隱患
由于序列化會生成新的對象,所以如果使用了傳統(tǒng)單例模式實現(xiàn)的類要實現(xiàn)序列化,則必須多操作一番:- 添加readResolve方法,該方法會在序列化之后被調(diào)用,其返回的對象將代替readObject生成的新對象
- 保證所有非基礎(chǔ)類型的實例域都是transient修飾的,transient修飾的域不參與序列化,也保證了不會有非法操作(非法操作者可以把非transient非基礎(chǔ)類型的實例域替換為自己的對象,該對象包含一個readResolve方法,該對象會被先序列化,接著執(zhí)行該對象的readResolve方法返回一個惡意的值,這樣原來的值就完全被修改了)執(zhí)行在readResolve方法之前
- 除此之外,利用反射也可以生成新的實例,所以得修改構(gòu)造器,使其在嘗試創(chuàng)建新的實例時拋出異常
public class SingletonTest implements Serializable{
private static final long serialVersionUID = 113454417668258756L;
private static final SingletonTest instance= new SingletonTest ();
//基礎(chǔ)類型實例域
private int fieldInt;
//非基礎(chǔ)類型實例域
private transient String fieldString;
private SingletonTest () {
if(Objects.nonNull(instance)) throw new IllegalStateException("try to create new instance");
}
public static SingletonTest getInstance() {
return instance;
}
private Object readResolve() {
return instance;
}
}
二、枚舉單例模式的寫法與優(yōu)點
- 枚舉單例模式的寫法
public enum EnumSingleton {
INSTANCE;
}
- 枚舉單例模式的優(yōu)點
- 枚舉本身的機制保證其不會受到反射攻擊
- 枚舉本身就是可序列化的,且不會因為序列化而生出新的對象
- 枚舉寫法簡單
- 什么?你說寫法過于簡單,看不懂怎么用?回去好好看看枚舉怎么用!
public enum EnumSingleton {
INSTANCE("hello");
private String name;
private EnumSingleton(String name) {
this.name = name;
}
public String getName(){
return name;
}
public static void main(String[] args) {
EnumSingleton es = EnumSingleton.INSTANCE;
System.out.println(es.getName());
}
}
參考文檔:
[1] 《Effective Java》