A single-instance pattern of Java design Patterns in registered Singleton

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.