Reputation:
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;
}
}
Upvotes: 25
Views: 36199
Reputation: 586
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;
// }
}
Upvotes: 0
Reputation: 27
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.
Upvotes: -1
Reputation: 65821
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;
}
Upvotes: 17
Reputation: 31463
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>
Upvotes: 5
Reputation: 329
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;
}
}
Upvotes: 3
Reputation: 691805
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.
Upvotes: 37