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