The simple design pattern, in fact, is the effective solution to the problem. In fact it is a kind of thought.
1. Single case design mode.
Solve the problem: is to ensure that a class in memory of the object uniqueness. (Single instance)
Use a single design pattern requirement: When you must use the same configuration information object for multiple programs, you need to guarantee the uniqueness of that object.
How do I ensure object uniqueness? Solution steps:
1. Other programs are not allowed to create the object with new. 1. Privatize the class constructor.
2. Create an instance of this class in the class. 2. Create an object of this class in the class by using new.
3. Provide an external method for other programs to get the object. 3. Define a public method to return the created object.
A hungry man (often used) class
single//class is loaded, the object already exists.
{
private static single S = new single ();
Private single () {} public
static single getinstance ()
{return
s;
}
}
Lazy Type (interview is often asked, in multithreaded concurrent access may lead to not guarantee the uniqueness of the object, there are security risks!) Class
single2//classes are loaded in, no objects are created, and only the GetInstance method is invoked.
//deferred loading form.
{
private static Single2 s = null;
Private Single2 () {} public
static Single2 getinstance ()
{
if (s==null)
s = new Single2 ();
return s;
}
}
Calling class class
Singledemo
{public
static void Main (string[] args)
{single
S1 = Single.getinstance ();
Single S2 = Single.getinstance ();
System.out.println (S1==S2);
Single SS = SINGLE.S;//This is not used here because it is not controllable, uses single.getinstance (), can pass the parameter to make the corresponding call.
}
}
An A hungry man singleton class instantiates itself when it is loaded. Even if the loader is static, it will instantiate itself when the A hungry man Singleton class is loaded. In terms of resource utilization efficiency, this is slightly worse than the lazy single case class. In terms of speed and reaction time, it is slightly better than the lazy single case class.
3. Registration type:
Code:
Package Pattern.singleton;import Java.util.hashmap;import Java.util.Map;
A single instance of the registration class.
Like spring inside the method, the class name registration, the next time from the inside directly to get.
public class Singleton3 {private static map<string,singleton3> Map = new hashmap<string,singleton3> ();
static{Singleton3 single = new Singleton3 ();
Map.put (Single.getclass (). GetName (), single); }//protected default constructor protected Singleton3 () {}//static Factory method, return the unique instance of this class public static Singleton3 getinstance (String name) {if (name = =
NULL) {name = Singleton3.class.getName ();
System.out.println ("name = = NULL" + "--->name=" +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 ();
} return Map.get (name);
}//A schematic business method public String about () {return "Hello, I am Regsingleton."; public static void Main (string[] args) {SingletoN3 single3 = singleton3.getinstance (null);
System.out.println (Single3.about ());
}
}