A single-instance pattern of Java design patterns

Source: Internet
Author: User

In Java, the singleton pattern is a common pattern, the singleton pattern is divided into three kinds: lazy type single case, the Han type single case, the registration type single case.

There are a few features of the Singleton model:

. A singleton class can have only one instance

The Singleton class must create its own unique instance

The Singleton class must provide this instance to all other objects

The singleton mode must ensure that there is only one instance, and instantiate it yourself and provide this instance to the system. Selecting Singleton mode is to avoid inconsistent states.

One, lazy type single case

 PackageCom.singleton; Public classSingleton {//Lazy Singleton, instantiate itself on first call    PrivateSingleton () {}//Note that there is no final    Private StaticSingleton single =NULL; //Static Factory class     PublicSingleton getinstance () {if(single =NULL) { single=NewSingleton (); }        returnSingle ; }}

Singleton the class is instantiated externally by restricting the construction method to private, the unique instance of Singleton can only be accessed through the getinstance () method within the same virtual machine scope

Lazy-type singleton is thread insecure

Two, a single case of the Chinese style of evil

 package   Com.singleton;  public  class   Singleton1 {  In the case of a Han-style singleton, the class was initialized with a near-self instantiation of the  private   private  static  Span style= "color: #0000ff;" >final  Singleton1 single = new   Singleton1    ();  //  static factory class  public   Singleton1 getinstance () {     return   single; }}

The single case of the Han style is thread-safe. The single case of the Han Dynasty has created a static object for common system use at the same time as the class creation, so it is thread-safe to change again.

III. Registration of single cases

 PackageCom.singleton;ImportJava.util.HashMap;ImportJava.util.Map; Public classSingleton3 {//registered Singleton, similar to the method in spring, register the name of the class, the next time directly from the inside to get    Private Staticmap<string,singleton3> map =NewHashmap<string,singleton3>(); Static{Singleton3 single=NewSingleton3 ();    Map.put (Single.getclass (). GetName (), single); }    //default construction method for protection    protectedSingleton3 () {} Public StaticSingleton3 getintance (String name) {if(Name = =NULL) {Name= Singleton3.class. GetName (); System.out.println ("NAME = = NULL" + "---->" +name); }        if(Map.get (name) = =NULL){            Try{map.put (name, (Singleton3) class.forname (name). newinstance ()); } Catch(instantiationexception e) {e.printstacktrace (); } Catch(illegalaccessexception e) {e.printstacktrace (); } Catch(ClassNotFoundException e) {e.printstacktrace (); }        }        returnmap.get (name); }    //a schematic method.     PublicString About () {return"Hello,i am Regsingleton."; }         Public Static voidmain (String [] args) {Singleton3 single= Singleton3.getintance (NULL);    System.out.println (Single.about ()); }    }

A registered singleton actually maintains an instance of a set of singleton classes that are stored in a map (the register), returned directly from the map for an instance that has been recently registered, registered first, and then returned

A single-instance pattern of Java design patterns

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.