单例模式:使用枚举版本

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

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

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

标准模式是让你的枚举实现一个接口 – 这样你就不需要在幕后公开任何更多的function了。

 // 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章节的在线参考。

 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. 

这里解释一下: 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; } } 

它比所有其他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; // } } 

以下是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; } } 

此片段也将在multithreading环境中进行锻炼,因为它会对类应用锁定。