简体   繁体   中英

Singleton Pattern: Using Enum Version

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>

It is explained here: http://javarevisited.blogspot.sk/2012/07/why-enum-singleton-are-better-in-java.html So, it can be simple done like this:

public enum EasySingleton{
    INSTANCE;
}

and also with using abstract factory design pattern:

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

It is much easier than the all other Singleton creation version : -

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

}

Following is a sample of singlton class,

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

This snippet will workout in the multithreaded environment too as it applies lock on the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM