Home  >  Article  >  Java  >  Examples of enumeration singleton pattern and source code analysis in Java

Examples of enumeration singleton pattern and source code analysis in Java

黄舟
黄舟Original
2017-08-08 09:57:041523browse

This article mainly introduces relevant information about JAVA enumeration singleton mode and source code analysis examples. Friends who need it can refer to

JAVA enumeration singleton mode and source code analysis. Detailed explanation of examples

There are many ways to implement the singleton mode. It has also been analyzed online that it is best to use enumeration to implement the single interest mode. The benefits are nothing more than three points:

1. Thread safety

2. No new instances will be generated due to serialization

3. Prevent reflection attacks. But there seems to be no article explaining how the ENUM singleton achieves the above three points. Please explain it to the experts. Let’s look at these three points:

Regarding the first point of thread safety, it can be seen from the decompiled class source code that it is also guaranteed through the class loading mechanism. This should be the case (solution)

Regarding the second serialization issue, there is an article saying that the enumeration class implements the readResolve() method itself, so it is anti-serialization. This method is implemented by the current class itself (solution)

About the second point Three-point reflection attack. I tried the following reflection attack myself, but an error was reported... After reading the decompiled class source code below, I understand that because the modification of the singleton class is abstract, it cannot be instantiated. (Solution)

The following is an enumeration singleton I wrote and the decompiled class of its class file

Enumeration singleton


public enum Singleton {
  INSTANCE {

    @Override
    protected void read() {
      System.out.println("read");
    }

    @Override
    protected void write() {
      System.out.println("write");
    }

  };
  protected abstract void read();
  protected abstract void write();
}

Class restored after decompilation


public abstract class Singleton extends Enum
{

  private Singleton(String s, int i)
  {
    super(s, i);
  }

  protected abstract void read();

  protected abstract void write();

  public static Singleton[] values()
  {
    Singleton asingleton[];
    int i;
    Singleton asingleton1[];
    System.arraycopy(asingleton = ENUM$VALUES, 0, asingleton1 = new Singleton[i = asingleton.length], 0, i);
    return asingleton1;
  }

  public static Singleton valueOf(String s)
  {
    return (Singleton)Enum.valueOf(singleton/Singleton, s);
  }

  Singleton(String s, int i, Singleton singleton)
  {
    this(s, i);
  }

  public static final Singleton INSTANCE;
  private static final Singleton ENUM$VALUES[];

  static 
  {
    INSTANCE = new Singleton("INSTANCE", 0) {

      protected void read()
      {
        System.out.println("read");
      }

      protected void write()
      {
        System.out.println("write");
      }

    };
    ENUM$VALUES = (new Singleton[] {
      INSTANCE
    });
  }
}

The above is the detailed content of Examples of enumeration singleton pattern and source code analysis in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn