单例模式:使用枚举版本

时间:2014-05-18 10:52:56

标签: java design-patterns enums singleton

我不明白如何实现Enum模式的Singleton版本。以下是实施"传统"使用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;
    }

}

6 个答案:

答案 0 :(得分:31)

public enum WirelessSensorFactory {
    INSTANCE;

    // all the methods you want
}

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

请注意,这个单例是线程安全的,而你的单例不是:两个线程可能都会进入竞争条件或可见性问题,并且都创建自己的单例实例。

答案 1 :(得分:15)

标准模式是让你的枚举实现一个接口 - 这样你就不需要在幕后公开任何其他功能了。

// 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;
}

答案 2 :(得分:4)

有效Java章节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
         */

}

<强>用法:

WirelessSensorFactory.INSTANCE.<any public method>

答案 3 :(得分:1)

这里解释: http://javarevisited.blogspot.sk/2012/07/why-enum-singleton-are-better-in-java.html 所以,它可以这样简单地完成:

public enum EasySingleton{
    INSTANCE;
}

以及使用抽象工厂设计模式:

public class Singleton{
    //initailzed during class loading
    private static final Singleton INSTANCE = new Singleton();

    //to prevent creating another instance of Singleton
    private Singleton(){}

    public static Singleton getSingleton(){
        return INSTANCE;
    }
}

答案 4 :(得分:0)

它比所有其他Singleton创建版本容易得多: -

public enum WirelessSensorFactory {

        INSTANCE;

        //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;
       // }

}

答案 5 :(得分:-1)

以下是singlton类的示例,

public class SingletonClass {
    private static SingletonClass singletonInstance = null;

    private SingletonClass() {      
    }

    public static SingletonClass getSingletonInstance() {       
        if(singletonInstance == null) {
            synchronized (SingletonClass.class) {
                if(singletonInstance == null) {
                  singletonInstance = new SingletonClass();
                }
            }
        }
        return singletonInstance;
    }
}

这段代码也将在多线程环境中进行锻炼,因为它会对类进行锁定。