Singleton mode-enumeration

So now write another most popular singleton pattern, namely the enumerated singleton pattern.

The code for the enumeration mode is as follows:

Date is for testing convenience.

public enum  EnumInstance {
    INSTANCE;
    private Object date;

    public Object getDate() {
        return date;
    }

    public void setDate(Object date) {
        this.date = date;
    }
    public static EnumInstance getInstance(){
        return INSTANCE;
    }
}

1. Then we can write a serialized chestnut for testing.

public class SerivalTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        EnumInstance instance = EnumInstance.getInstance();
        instance.setDate(new Object());
        //放文件
        ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("single_file"));
        outputStream.writeObject(instance);

        //取文件
        File file = new File("single_file");
        ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file));
        EnumInstance hungrySingleton = (EnumInstance) inputStream.readObject();

        System.out.println(instance);
        System.out.println(hungrySingleton);
        System.out.println(instance==hungrySingleton);
    }
}

 The result is:

 

how about it? Enumeration is so powerful, so how does serialization and deserialization handle enumeration? First enter through inputStream.readObject () and find the readEnum () method.

as follows:

The enumeration constant is obtained by type and name, because the name in the enumeration is unique and corresponds to an enumeration constant, so the 2012 line is definitely the only constant object

In this way, no new objects are created. The singleton attribute of this object is maintained. This processing method in enumeration is still very simple and easy to understand, so the enumeration class is for serialization

This damage is unaffected.

 

 

 2. Write an example of a reflection attack.

public class Testreflection {
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class object = EnumInstance.class;
        Constructor constructor = object.getDeclaredConstructor();
        constructor.setAccessible(true);

        EnumInstance instance = EnumInstance.getInstance();
        EnumInstance newInstance = (EnumInstance) constructor.newInstance();

        System.out.println(instance.getDate());
        System.out.println(newInstance.getDate());
        System.out.println(instance.getDate() == newInstance.getDate());

     }
    }

See the result:

 

It means that no parameterless constructor is obtained, then we open the source code and take a look. java.lang.enum can see that the enumeration has no parameterless constructor, and there is only one constructor with two parameters, as shown below. Then we will pass these two parameters in and test again.

 

 

It can be seen that an exception is thrown, but it can be clearly understood that this exception is that it cannot be reflected to create an object. We enter the source code from the place where the error was reported to view the details.

 

The source code clearly tells us that if it is an enumerated type, an exception is thrown, showing how powerful the enumeration is!

 

Guess you like

Origin www.cnblogs.com/shenwen/p/12676720.html