单例模式大家都不模式,java1.5发行版之前大家都用两种方法实现singleton。
第一种:静态成员
public class Singleton1 {
public static final Singleton1 INSTANCE = new Singleton1();
private Singleton1() {
}
}
第二种:静态工厂方法
public class Singleton2 {
private static final Singleton2 INSTANCE = new Singleton2();
private Singleton2() {
}
public static Singleton2 getInstance() {
return INSTANCE;
}
}
这两种实现方式都有缺陷,享有特权的AccessibleObject.setAccessible()方法可以通过反射机制调用private成员变量,可以参考这篇文章。如果需要抵御这种攻击,可以在构造方法内判断创建第二个实例的时候抛出异常
如果上面两种方法实现的Singleton是可以序列化的,加上 implements Serializable只保证它可以序列化,为了保证反序列化的时候,实例还是Singleton,必须声明所有的实例域都是transient的,并且提供 readResolve方法,否则,每次反序列化都会生成新的实例。
public class Singleton1 implements Serializable {
private static final Singleton1 INSTANCE = new Singleton1();
private Singleton1() {}
public void say() {
System.out.println("HAHAHAHHAH");
}
public static Singleton1 getInstance() {
return INSTANCE;
}
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
Singleton1 singleton1 = Singleton1.getInstance();
singleton1.say();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Singleton.txt"));
oos.writeObject(singleton1);
oos.close();
// 反序列化的时候就会生成新的实例
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Singleton.txt"));
Singleton1 singleton2 = (Singleton1)ois.readObject();
ois.close();
System.out.println(singleton1 == singleton2);
}
}
输出结果:
HAHAHAHHAH
false
加入readResolve方法:
public class Singleton1 implements Serializable {
private static final Singleton1 INSTANCE = new Singleton1();
private Singleton1() {}
public void say() {
System.out.println("HAHAHAHHAH");
}
public static Singleton1 getInstance() {
return INSTANCE;
}
private Object readResolve() {
return INSTANCE;
}
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
Singleton1 singleton1 = Singleton1.getInstance();
singleton1.say();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Singleton.txt"));
oos.writeObject(singleton1);
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Singleton.txt"));
Singleton1 singleton2 = (Singleton1)ois.readObject();
ois.close();
System.out.println(singleton1 == singleton2);
}
}
输出结果:
HAHAHAHHAH
true
第三种:单元素枚举类型
public enum Singleton2 {
INSTANCE;
public void say() {
System.out.println("HAHAHAHHAH");
}
public static void main(String[] args) {
Singleton2 singleton2 = Singleton2.INSTANCE;
singleton2.say();
}
}
通过枚举实现Singleton更加简洁,同时枚举类型无偿地提供了序列化机制,可以防止反序列化的时候多次实例化一个对象。枚举类型也可以防止反射攻击,当你试图通过反射去实例化一个枚举类型的时候会抛出IllegalArgumentException异常。
单元素枚举类型实现单例是最方便的。