Turn from: https://www.cnblogs.com/lwbqqyumidi/p/3738059.html
In the Java design pattern, the single case pattern is relatively simple as a build pattern. The scenario that applies is that for a class that is defined, there is only one instance object during the execution of the entire application. such as the common Application object in Android.
The singleton pattern is used to instantiate and provide the system with access to this single instance.
Depending on the timing of this single instance (which, of course, is the first and only time this single instance is produced), it can be divided into loafers, a hungry man, and registrations.
One, the lazy type:
It is characterized by deferred loading, that is, when a single instance needs to be used, to initialize 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 //Privatisation constructor
9 Private SingleTon () {All } //Static public method To provide a single case fetch to the entire application-public static SingleTon getinstance ()
{ if (instance = null) { Instance = new SingleTon (); instance return ;
21}
Lazy Thread-safe notation
1 package Com.qqyumidi;
2
3 public class SingleTon {
4
5 //Static instance variable plus volatile
6 private static volatile SingleTon instance;
7
8 //Privatisation constructor
9 Private SingleTon () {A/ /double check lock The public static SingleTon getinstance () { if (instance = null) { synchronized ( Singleton.class) { (instance = = null) { instance = new Singleton ();
}
instance return ;
25}
Two, a hungry man type:
The feature of the A Hungry man is that the application does not have to use this single instance before it is instantiated. A common classic is:
1 package Com.qqyumidi;
2
3 public class SingleTon {
4
5 //Static instance variable, direct initialization
6 private static SingleTon instance = new single Ton ();
7
8 //Privatisation constructor
9 Private SingleTon () {All } //Static public method To provide a single case access to the entire application of the public static SingleTon getinstance () {return instance;
18}
Third, the registration type single case mode:
A single example mode of registration, typically through a specialized class to manage and maintain this single instance of individual cases. It is easy to achieve this goal through the map method. 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 case a = (a) getinstance (A.class.getname (
));
13//Get B class of single case B = (b) getinstance (B.class.getname ()); 15} 16 17//per type get single instance of public static Object getinstance (String className) {19//judgment Singletonmap is There is no such single case, then returned after the acquisition, without adding a single case after the!singletonmap.containskey (className) {a try {sin
Gletonmap.put (ClassName, Class.forName (className). newinstance ()); } catch (Instantiationexception | illegalaccessexception |
ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();
Singletonmap.get return (className); 29} 30} 31 32