用Enum实现Singleton(在Java中)。[英] Implementing Singleton with an Enum (in Java)

本文是小编为大家收集整理的关于用Enum实现Singleton(在Java中)。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我已经读到可以使用Enum在Java中实现Singleton,例如:

public enum MySingleton {
     INSTANCE;   
}

但是,上述如何工作?具体而言,必须实例化Object.在这里,如何实例化MySingleton?谁在做new MySingleton()?

推荐答案

这个,

public enum MySingleton {
  INSTANCE;   
}

有一个隐式的空构造函数.改为明确,

public enum MySingleton {
    INSTANCE;
    private MySingleton() {
        System.out.println("Here");
    }
}

如果您添加了其他类main()方法

的类
public static void main(String[] args) {
    System.out.println(MySingleton.INSTANCE);
}

您会看到

Here
INSTANCE

enum字段是编译时间常数,但它们是其enum类型的实例.而且,当枚举类型首次引用 .

时,它们会构造.

其他推荐答案

an enum类型是class的特殊类型.

您的enum实际上将被编译为

之类的东西
public final class MySingleton {
    public final static MySingleton INSTANCE = new MySingleton();
    private MySingleton(){} 
}

当您的代码首次访问INSTANCE时,类MySingleton将由JVM加载和初始化.此过程一旦(懒惰)初始化static字段.

其他推荐答案

在此 > Joshua Bloch,您可以找到解释为什么您应该使用私人构造函数或枚举类型执行Singleton属性.本章很长,因此将其总结为:

让班级成为单身人士可能会很难测试其客户端,因为除非实现其类型的接口,否则不可能将模拟实现替换为单身人士. 推荐的方法是通过简单地制作一个具有一个元素的枚举类型来实现单例:

// Enum singleton - the preferred approach
public enum Elvis {
INSTANCE;
public void leaveTheBuilding() { ... }
}

这种方法在功能上等同于公共场所方法,除了它 更简洁,免费提供序列化机械,并提供 Ironclad保证可以防止多重实例化,即使面对精致 序列化或反射攻击.

虽然这种方法尚未广泛 采用,单元素枚举类型是实现单身人士的最佳方法.

本文地址:https://www.itbaoku.cn/post/627506.html

问题描述

I have read that it is possible to implement Singleton in Java using an Enum such as:

public enum MySingleton {
     INSTANCE;   
}

But, how does the above work? Specifically, an Object has to be instantiated. Here, how is MySingleton being instantiated? Who is doing new MySingleton()?

推荐答案

This,

public enum MySingleton {
  INSTANCE;   
}

has an implicit empty constructor. Make it explicit instead,

public enum MySingleton {
    INSTANCE;
    private MySingleton() {
        System.out.println("Here");
    }
}

If you then added another class with a main() method like

public static void main(String[] args) {
    System.out.println(MySingleton.INSTANCE);
}

You would see

Here
INSTANCE

enum fields are compile time constants, but they are instances of their enum type. And, they're constructed when the enum type is referenced for the first time.

其他推荐答案

An enum type is a special type of class.

Your enum will actually be compiled to something like

public final class MySingleton {
    public final static MySingleton INSTANCE = new MySingleton();
    private MySingleton(){} 
}

When your code first accesses INSTANCE, the class MySingleton will be loaded and initialized by the JVM. This process initializes the static field above once (lazily).

其他推荐答案

In this Java best practices book by Joshua Bloch, you can find explained why you should enforce the Singleton property with a private constructor or an Enum type. The chapter is quite long, so keeping it summarized:

Making a class a Singleton can make it difficult to test its clients, as it’s impossible to substitute a mock implementation for a singleton unless it implements an interface that serves as its type. Recommended approach is implement Singletons by simply make an enum type with one element:

// Enum singleton - the preferred approach
public enum Elvis {
INSTANCE;
public void leaveTheBuilding() { ... }
}

This approach is functionally equivalent to the public field approach, except that it is more concise, provides the serialization machinery for free, and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks.

While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton.