25

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

}
3
  • What do you mean by "enum version"? Commented May 18, 2014 at 10:55
  • 1
    Another form of implementing the Singleton pattern using an Enum, it was introduced in java 1.5 Commented May 18, 2014 at 10:55
  • thanks now I see what you mean. Try this link: stackoverflow.com/questions/18425693/… Commented May 18, 2014 at 10:56

6 Answers 6

37
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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, How would i then use this class within a main method?
As any other enum: WirelessSensorFactory.INSTANCE.someMethod()
Yeah, so only one instance of the class can only ever be used, is a multiton implemented in the same way?
This is also safe from allowing multiple objects from deserilization as well as reflection.
17

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

Comments

5

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>

2 Comments

Thanks, How would i then use this class within a main method?
@RNI2013 I included example usage.
3

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

Comments

0

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

}

Comments

-1

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.

4 Comments

This is wrong. The way the code is, it only works in multithreaded SINGLE core CPU systems. In multicore system this method is faulty. There is no "happens-before" link between the writes and reads.
Make singletonInstance volatile to guarantee happens-before
Yeah...needed a object to be volatile, private volatile static SingletonClass singletonInstance = null;
As discussed before, volatile is required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.