public static Singleton getInstance() { if(singleton == null) { synchronized(Singleton.class) { singleton = new Singleton(); } } return singleton; }
public static Singleton getInstance() { if(singleton == null) { synchronized(Singleton.class) { if(singleton == null) { singleton = new Singleton(); } } } return singleton; }
public class Singleton { public final static Singleton INSTANCE = new Singleton(); private Singleton() { // Exists only to defeat instantiation. } }
Singleton singleton = Singleton.INSTANCE; singleton.dothis(); singleton.dothat(); ...
import java.util.HashMap; import org.apache.log4j.Logger; public class Singleton { private static HashMap map = new HashMap(); private static Logger logger = Logger.getRootLogger(); protected Singleton() { // Exists only to thwart instantiation } public static synchronized Singleton getInstance(String classname) { if(classname == null) throw new IllegalArgumentException("Illegal classname"); Singleton singleton = (Singleton)map.get(classname); if(singleton != null) { logger.info("got singleton from map: " + singleton); return singleton; } if(classname.equals("SingeltonSubclass_One")) singleton = new SingletonSubclass_One(); else if(classname.equals("SingeltonSubclass_Two")) singleton = new SingletonSubclass_Two(); map.put(classname, singleton); logger.info("created singleton: " + singleton); return singleton; } // Assume functionality follows that's attractive to inherit }
import java.util.HashMap; import org.apache.log4j.Logger; public class Singleton { private static HashMap map = new HashMap(); private static Logger logger = Logger.getRootLogger(); protected Singleton() { // Exists only to thwart instantiation } public static synchronized Singleton getInstance(String classname) { Singleton singleton = (Singleton)map.get(classname); if(singleton != null) { logger.info("got singleton from map: " + singleton); return singleton; } try { singleton = (Singleton)Class.forName(classname).newInstance(); } catch(ClassNotFoundException cnf) { logger.fatal("Couldn't find class " + classname); } catch(InstantiationException ie) { logger.fatal("Couldn't instantiate an object of type " + classname); } catch(IllegalAccessException ia) { logger.fatal("Couldn't access class " + classname); } map.put(classname, singleton); logger.info("created singleton: " + singleton); return singleton; } }
import java.util.HashMap; import org.apache.log4j.Logger; public class SingletonRegistry { public static SingletonRegistry REGISTRY = new SingletonRegistry(); private static HashMap map = new HashMap(); private static Logger logger = Logger.getRootLogger(); protected SingletonRegistry() { // Exists to defeat instantiation } public static synchronized Object getInstance(String classname) { Object singleton = map.get(classname); if(singleton != null) { return singleton; } try { singleton = Class.forName(classname).newInstance(); logger.info("created singleton: " + singleton); } catch(ClassNotFoundException cnf) { logger.fatal("Couldn't find class " + classname); } catch(InstantiationException ie) { logger.fatal("Couldn't instantiate an object of type " + classname); } catch(IllegalAccessException ia) { logger.fatal("Couldn't access class " + classname); } map.put(classname, singleton); return singleton; } }
import java.util.HashMap; import org.apache.log4j.Logger; public class Singleton { protected Singleton() { // Exists only to thwart instantiation. } public static Singleton getInstance() { return (Singleton)SingletonRegistry.REGISTRY.getInstance(classname); } }