单子模式。使用Enum版本[英] Singleton Pattern: Using Enum Version

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

问题描述

我不明白如何实现Singleton模式的Enum版本.以下是使用单例模式实现"传统"方法的示例.我想将其更改为使用枚举版本,但我不确定如何.

public class WirelessSensorFactory implements ISensorFactory{

    private static WirelessSensorFactory wirelessSensorFactory;

    //Private Const
    private WirelessSensorFactory(){
        System.out.println("WIRELESS SENSOR FACTORY");
    }

    public static WirelessSensorFactory getWirelessFactory(){

        if(wirelessSensorFactory==null){
            wirelessSensorFactory= new WirelessSensorFactory();
        }

        return wirelessSensorFactory;
    }

}

推荐答案

public enum WirelessSensorFactory {
    INSTANCE;

    // all the methods you want
}

这是您的单身人士:只有一个实例的枚举.

请注意,此单身人士是线程安全的,而您的不是:两个线程可能会遇到种族条件或可见性问题,并且都创建了自己的Singleton实例.

其他推荐答案

标准模式是让您的枚举实现接口 - 这样,您就不需要比幕后的更多功能了.

// Define what the singleton must do.
public interface MySingleton {

    public void doSomething();
}

private enum Singleton implements MySingleton {

    /**
     * The one and only instance of the singleton.
     *
     * By definition as an enum there MUST be only one of these and it is inherently thread-safe.
     */
    INSTANCE {

                @Override
                public void doSomething() {
                    // What it does.
                }

            };
}

public static MySingleton getInstance() {
    return Singleton.INSTANCE;
}

其他推荐答案

有效Java章的在线参考" nofollow">在这里.

public enum WirelessSensorFactory implements ISensorFactory { // change CLASS to ENUM here

        INSTANCE; //declare INSTANCE of the Enum

        //private static WirelessSensorFactory wirelessSensorFactory;

        // Remove the private construct - it's Enum, 
        // so you don't need to protect instantiations of the class
          //private WirelessSensorFactory(){
          //   System.out.println("WIRELESS SENSOR FACTORY");
          //}

        // You don't need to check if instance is already created, 
        // because it's Enum, hence you don't need the static var
          //public WirelessSensorFactory getWirelessFactory(){
          //    if(wirelessSensorFactory==null){
          //        wirelessSensorFactory= new WirelessSensorFactory();
          //    }
          //    return wirelessSensorFactory;
          //}

        /*
         * All other methods you need and 
         * implementation of all the Factory methods from your interface
         */

}

用法:

WirelessSensorFactory.INSTANCE.<any public method>

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

问题描述

I do not understand how to implement the Enum Version of the Singleton pattern. Below is an example of implementing "traditional" approach using the Singleton pattern. I would like to change it to use the Enum version but I am not sure how.

public class WirelessSensorFactory implements ISensorFactory{

    private static WirelessSensorFactory wirelessSensorFactory;

    //Private Const
    private WirelessSensorFactory(){
        System.out.println("WIRELESS SENSOR FACTORY");
    }

    public static WirelessSensorFactory getWirelessFactory(){

        if(wirelessSensorFactory==null){
            wirelessSensorFactory= new WirelessSensorFactory();
        }

        return wirelessSensorFactory;
    }

}

推荐答案

public enum WirelessSensorFactory {
    INSTANCE;

    // all the methods you want
}

Here's your singleton: an enum with only one instance.

Note that this singleton is thread-safe, while yours is not: two threads might both go into a race condition or visibility problem and both create their own instance of your singleton.

其他推荐答案

The standard pattern is to have your enum implement an interface - this way you do not need to expose any more of the functionality behind the scenes than you have to.

// Define what the singleton must do.
public interface MySingleton {

    public void doSomething();
}

private enum Singleton implements MySingleton {

    /**
     * The one and only instance of the singleton.
     *
     * By definition as an enum there MUST be only one of these and it is inherently thread-safe.
     */
    INSTANCE {

                @Override
                public void doSomething() {
                    // What it does.
                }

            };
}

public static MySingleton getInstance() {
    return Singleton.INSTANCE;
}

其他推荐答案

Online reference of the Effective Java chapter here.

public enum WirelessSensorFactory implements ISensorFactory { // change CLASS to ENUM here

        INSTANCE; //declare INSTANCE of the Enum

        //private static WirelessSensorFactory wirelessSensorFactory;

        // Remove the private construct - it's Enum, 
        // so you don't need to protect instantiations of the class
          //private WirelessSensorFactory(){
          //   System.out.println("WIRELESS SENSOR FACTORY");
          //}

        // You don't need to check if instance is already created, 
        // because it's Enum, hence you don't need the static var
          //public WirelessSensorFactory getWirelessFactory(){
          //    if(wirelessSensorFactory==null){
          //        wirelessSensorFactory= new WirelessSensorFactory();
          //    }
          //    return wirelessSensorFactory;
          //}

        /*
         * All other methods you need and 
         * implementation of all the Factory methods from your interface
         */

}

Usage:

WirelessSensorFactory.INSTANCE.<any public method>