不一樣的單例模式
阿新 • • 發佈:2020-12-11
DCL懶漢式
使用雙重檢查懶漢式時為什麼要使用volatile?
public class LazyMan { private static LazyMan lazyMan; private LazyMan() { } //雙重檢測模式的懶漢式單例 public static LazyMan getInstance(){ if (lazyMan==null){ synchronized (LazyMan.class){ if (lazyMan==null){ lazyMan = new LazyMan(); //不是一個原子性操作 /** * 1. 分配記憶體空間 * 2、執行構造方法,初始化物件 * 3、把這個物件指向這個空間 * * 123 * 132 A * B // 此時lazyMan還沒有完成構造 * 若發生這樣的指令重排則在多執行緒時可能會返回空物件,儘管可能性非常小 */ } } } return lazyMan; } }
靜態內部類
// 靜態內部類
public class Holder {
private Holder(){
}
public static Holder getInstace(){
return InnerClass.HOLDER;
}java
public static class InnerClass{
private static final Holder HOLDER = new Holder();
}
}
反射破解單例模式
// 懶漢式單例 // 道高一尺,魔高一丈! public class LazyMan { private static boolean is_exist = false; //設定標誌位來檢測構造器是否被執行過,來避免反射破壞單例 //但是,當標誌位被其他人知道後,任然可以通過main方法中被註釋的程式碼 //先修改標誌位再通過反射獲取新的物件。 //所謂道高一尺魔高一丈。 private LazyMan(){ synchronized (LazyMan.class){ if (is_exist == false){ is_exist = true; }else { throw new RuntimeException("不要試圖使用反射破壞異常"); } } } private volatile static LazyMan lazyMan; // 雙重檢測鎖模式的 懶漢式單例 DCL懶漢式 public static LazyMan getInstance(){ if (lazyMan==null){ synchronized (LazyMan.class){ if (lazyMan==null){ lazyMan = new LazyMan(); // 不是一個原子性操作 } } } return lazyMan; } // 反射! public static void main(String[] args) throws Exception { // LazyMan instance = LazyMan.getInstance(); //Field is_exist = LazyMan.class.getDeclaredField("is_exist"); //is_exist.setAccessible(true); Constructor<LazyMan> declaredConstructor = LazyMan.class.getDeclaredConstructor(null); declaredConstructor.setAccessible(true); LazyMan instance = declaredConstructor.newInstance(); //is_exist.set(instance,false); LazyMan instance2 = declaredConstructor.newInstance(); System.out.println(instance); System.out.println(instance2); } }
列舉單例
列舉可以保證不被反射
public enum EnumSingle { INSTANCE; public EnumSingle getInstance(){ return INSTANCE; } } class Test{ public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { EnumSingle instance1 = EnumSingle.INSTANCE; Constructor<EnumSingle> declaredConstructor = EnumSingle.class.getDeclaredConstructor(String.class,int.class); declaredConstructor.setAccessible(true); EnumSingle instance2 = declaredConstructor.newInstance(); // NoSuchMethodException: com.maple.single.EnumSingle.<init>() System.out.println(instance1); System.out.println(instance2); } }
列舉反編譯的程式碼
public final class EnumSingle extends Enum
{
public static EnumSingle[] values()
{
return (EnumSingle[])$VALUES.clone();
}
public static EnumSingle valueOf(String name)
{
return (EnumSingle)Enum.valueOf(com/maple/single/EnumSingle, name);
}
//沒有無參構造器,只有一個2參構造器
private EnumSingle(String s, int i)
{
super(s, i);
}
public EnumSingle getInstance()
{
return INSTANCE;
}
public static final EnumSingle INSTANCE;
private static final EnumSingle $VALUES[];
static
{
INSTANCE = new EnumSingle("INSTANCE", 0);
$VALUES = (new EnumSingle[] {
INSTANCE
});
}
}