-
Notifications
You must be signed in to change notification settings - Fork 0
Description
Item 3 单例模式
饿汉式 static final method
public class Singleton{
//类加载时就初始化
private static final Singleton instance = new Singleton();
private Singleton(){}
public static Singleton getInstance(){
return instance;
}
}注意,这种方法实现序列化接口的时候会有问题
- 在序列化后,再经过反序列化会得到一个不同的对象,需要在单例类中加入readResolve()方法,才能防止反序列化生成的对象与原有对象不是同一对象的问题 , 具体实现如下:
public class SingletonSerizlizable implements Serializable{
private static final SingletonSerizlizable INSTANCE=new SingletonSerizlizable();
private SingletonSerizlizable(){};
public static SingletonSerizlizable getInstance()
{
return INSTANCE;
}
private Object readResolve()
{
return INSTANCE;//依旧返回最初创建的对象,不再创建新对象
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}静态内部类 static nested class
public class Singleton {
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
private Singleton (){}
public static final Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}枚举
public enum Elvis {
INSTANCE;
public void levaveTheBuilding(){
System.out.println("leave ...");
}
public static void main(String[] args){
Elvis singleton = Elvis.INSTANCE;
singleton.levaveTheBuilding();
}
}Effective Java作者最推荐的方式,多线程安全,简单,序列化和反序列化没有问题。
Enum constants are serialized differently than ordinary serializable or externalizable objects. The serialized form of an enum constant consists solely of its name; field values of the constant are not present in the form. To serialize an enum constant, ObjectOutputStream writes the value returned by the enum constant’s name method. To deserialize an enum constant, ObjectInputStream reads the constant name from the stream; the deserialized constant is then obtained by calling the java.lang.Enum.valueOf method, passing the constant’s enum type along with the received constant name as arguments. Like other serializable or externalizable objects, enum constants can function as the targets of back references appearing subsequently in the serialization stream. The process by which enum constants are serialized cannot be customized: any class-specific writeObject, readObject, readObjectNoData, writeReplace, and readResolve methods defined by enum types are ignored during serialization and deserialization. Similarly, any serialPersistentFields or serialVersionUID field declarations are also ignored–all enum types have a fixedserialVersionUID of 0L. Documenting serializable fields and data for enum types is unnecessary, since there is no variation in the type of data sent.