In Java design mode, Singleton mode is relatively simple as a construction mode. The applicable scenario is that for a defined class, there is only one instance object during the entire application execution. such as application objects that are common in Android.
The singleton mode is used to instantiate and provide this single instance access method to the system.
Depending on the timing of this single instance (which is, of course, the first and only time this single instance is produced), it can be divided into lazy, a hungry man and registered.
First, lazy type:
It is characterized by lazy loading, which is the only way to initialize this single instance when it is necessary to use this single instance. The common classics are written as follows:
1 package Com.qqyumidi; 2 3 public class SingleTon {4 5 //Static instance variable 6 private static SingleTon instance; 7 8 //Privatization constructor 9
private SingleTon () { }12 //Static public method, provides a single-instance acquisition method for the entire application, public static SingleTon getinstance () { Instance = = null) { instance = new SingleTon (); }18 return instance;19 }20 21}
Second, a hungry man type:
The A hungry man is characterized by the instantiation of this single instance when it is not needed in the application. The Common classic notation is:
1 package Com.qqyumidi; 2 3 public class SingleTon {4 5 //Static instance variable, direct initialization 6 private static SingleTon instance = new SingleTon (); 7< C5/>8 //Privatization constructor 9 private SingleTon () { }12 //static public method, providing single-instance access to the entire application public static SingleTon getinstance () { return instance;16 }17 18}
Three, the registration of single-case mode:
The registered singleton mode is generally managed and maintained by a specialized class for each singleton pattern. This can be achieved conveniently through the map mode. The common code is as follows:
1 package Com.qqyumidi; 2 3 Import Java.util.HashMap; 4 Import Java.util.Map; 5 6 public class Singletonmanager {7 8 private static Map Singletonmap = new HashMap (); 9 public static void Main (string[] args) {11//Get a Class A single example of a A = (a) getinstance (A.class.getname ()); 13//Get a single example of Class B 14 b b = (b) getinstance (B.class.getname ()); 15}16 17//Get a singleton public static Object getinstance by type (String ClassName) {19//Determine if there is a single case in the Singletonmap, there is a return after the acquisition, none adds a single case after the return of the IF (!singletonmap.containskey (ClassName)) {21 try {singletonmap.put (className, Class.forName (className). newinstance ()); Atch (Instantiationexception | illegalaccessexception | ClassNotFoundException e) {//TODO auto-generated catch block25 e.printstacktrace (); 26 }27}28 return Singletonmap.get (className);}30}31 class A {37}35 38}
In addition: It is important to note that in multi-threaded environments, the above methods of constructing a singleton pattern need to take into account thread safety issues.
Improved lazy type (directly meet thread safety)--through static inner class implementation
In the lazy singleton mode as above, for multithreaded environments. Thread safety can be achieved through common methods such as synchronized, while further improvements can be achieved through Java static internal classes.
The common code is as follows:
1 package Com.qqyumidi; 2 3 public class SingleTon {4 5 //Use the static inner class attribute to implement a singleton of the outer class 6 private static class Singletonbuilder {7 Priv Ate static SingleTon SingleTon = new SingleTon (); 8 } 9 //Privatization constructor one private SingleTon () { }14 public static SingleTon getinstance () {16< C13/>return singletonbuilder.singleton;17 }18 public static void Main (string[] args) { SingleTon Instance = getinstance (); }22}
The main principle is: In Java, the static inner class can access the member properties and methods of its external class, while the static inner class only starts to be loaded for the first time when it is called, and with this feature, it can be implemented as a lazy, static initialization of a single instance of a class in static inner classes.
Single-Case mode