Java Singleton Mode

Source: Internet
Author: User

Singleton mode is a common design mode. <br/> Singleton mode is divided into three types: lazy Singleton, hungry Singleton, and registered Singleton. <Br/> the singleton mode has the following features: <br/> 1. A singleton class can only have one instance. <Br/> 2. The Singleton class must create its own unique instance. <Br/> 3. The Singleton class must provide this instance to all other objects. </P> <p> 1. When a class is loaded, the unique instance has been created. This design pattern is easy to implement in Java and hard to implement in other languages. <Br/> Singleton mode-Lazy Singleton <br/> public class lazysingleton {<br/>/*** private static object, initialization is not performed during loading */<br/> Private Static lazysingleton m_intance = NULL; <br/>/*** private constructor, avoid external instance creation */<br/> private lazysingleton () {}< br/>/*** static factory method, and return a unique instance of this class. * Initialization is performed only when the instance is not initialized. * @ return lazysingleton */<br/> synchronized public static lazysingleton getinstance () {<br/> If (m_intance = NULL) {<br/> m_intance = new lazysingleton (); <br />}< Br/> return m_intance; <br/>}</P> <p> // test the singleton mode. <br/> lazysingleton = lazysingleton. getinstance (); <br/> lazysingleton lazysingleton1 = lazysingleton. getinstance (); </P> <p> If (lazysingleton = lazysingleton1) {<br/> system. out. println ("same object instance"); <br/>} else {<br/> system. out. println ("not the same object instance"); <br/>}</P> <p> 2. Do not create a singleton instance during class loading. The instance is created only when the instance is requested for the first time, and is not created after the instance is created for the first time. <Br/>/*** Singleton mode-ELE. Me Singleton */<br/> public class eagersingleton {<br/>/** private) unique (static final) instance member. The singleton object is created when the class is loaded */<br/> Private Static final eagersingleton m_instance = new eagersingleton (); <br/>/*** private constructor to avoid external instance creation */<br/> private eagersingleton () {} // provides an empty constructor <br/>/*** a static factory method that returns a unique instance of this class. * @ return eagersingleton */<br/> Public static eagersingleton getinstance () {<br/> return m_insta NCE; <br/>}</P> <p> // check whether the singleton effect has been achieved (only one empty instance is available when the system runs) <br/> eagersingleton = eagersingleton. getinstance (); <br/> eagersingleton eagersingleton1 = eagersingleton. getinstance (); </P> <p> If (eagersingleton = eagersingleton1) {<br/> system. out. println ("same object instance"); <br/>} else {<br/> system. out. println ("not the same object instance "); <br/>}</P> <p> 3. The registration Singleton actually maintains a group of Singleton instances, store these instances in a map (register ). For example, it is returned directly from the factory. If there is no registration, it is first registered and then returned. <Br/>/*** Singleton mode-registration Singleton */<br/> public class regsingleton {<br/>/*** register thin, used to store all registered instances */<br/> Private Static Map <string, regsingleton> m_registry = new hashmap (); <br/> // Add an instance to the register when the class is loaded <br/> static {<br/> regsingleton x = new regsingleton (); <br/> m_registry.put (X. getclass (). getname (), x); <br/>}< br/>/*** protected default constructor */<br/> protected regsingleton () {}< br/>/*** the static factory method returns a unique instance of the specified object to be registered. <br/> * If you have not yet registered, you can directly retrieve and return the results. If you have not registered the data, register the data first, then retrieve and return <br/> * @ Param name <br/> * @ return <br/> regsingleton */<br/> Public static regsingleton getinstance (string name) {<br/> If (name = NULL) {<br/> name = "regsingleton"; <br/>}< br/> If (m_registry.get (name) = NULL) {<br/> try {<br/> m_registry.put (name, (regsingleton) class. forname (name ). newinstance (); <br/>} catch (instantiationexception e) {<br/> E. PRI Ntstacktrace (); <br/>} catch (illegalaccessexception e) {<br/> E. printstacktrace (); <br/>} catch (classnotfoundexception e) {<br/> E. printstacktrace (); <br/>}< br/> return m_registry.get (name ); <br/>}</P> <p>/*** a schematic commercial Method * @ return string */<br/> Public String about () {<br/> return "Hello, I am regsingleton! "; <Br/>}</P> <p> 4. An application in singleton mode. This application is a configuration file management class. In order to run this example correctly, I first created a XXXX under drive C. properties file with the following content: ------------------- </P> <p> User = rootpassword = leizhimin the <SPAN class = 'wp _ keywordlink '> code of the configuration file management class </span> is as follows: <br/> Singleton mode application-singleton type application-configuration file management */<br/> public class configmanager {<br/>/*** full name of attribute file */ <br/> Private Static final string pfile = "C: // XXX. properties "; <br/>

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.