Package Creation _ Singleton mode _ registration; Import Java.util.hashmap;import java.util.map;/** * Registered Singleton actually maintains an instance of a set of singleton classes that store these instances in a MAP (register) *, For a single case that has already been registered, it is returned directly from the factory and, for non-registered, first registered, then * returned * @author PP * */public class Regsingleton {/** * register, to hold all registered instances */ private static map<string, regsingleton> Map = new hashmap<string, regsingleton> (); Add an instance to the register when the class loads static{Regsingleton singleton = new Regsingleton (); Map.put (Singleton.getclass (). GetName (), singleton);//Application of Reflection}/** * Protected default construction Method */protected Regsingleton () {}/** * Static Factory method, returns the unique instance of the specified Enlistment object * For a direct fetch that has already been registered, for a registration that has not yet been registered, and then take out return * */public static regs Ingleton getinstance (String name) {if (name==null) {name= "Regsingleton"; } if (Map.get (name) ==null) {try {map.put (name), (Regsingleton) class.forname (name). newinst Ance ()); } catch (Exception e) {e.printstacktrace (); } } return Map.get (name); }/** * A schematic business method */Public String about () {return "hello,i am Regsingleton"; }}
A single-instance pattern of Java design Patterns in registered Singleton