Talk about the JAVASE singleton design pattern

  • 2020-04-01 04:27:51
  • OfStack

Simply put, design patterns are effective solutions to problems. It's an idea.

1. Singleton design pattern.

          The problem: you can guarantee the uniqueness of a class in memory. (single instance)

          Use singleton design pattern requirements: when the same configuration information object must be used for multiple programs, it needs to be unique.

          How do I guarantee object uniqueness?                                                                                                           Solution steps:

          1. Other programs are not allowed to create this object with new.                                                                                       1. Privatize the class constructor.

          2. Create an instance of this class in this class.                                                                                                                 2. Create an object of this class in this class with new.

          3. Provide a method for other programs to obtain the object.                                                       3. Define a public method that returns the created object.


//Hungry Chinese style (often used in development)
class Single//The object already exists as soon as the class is loaded.
{
  private static Single s = new Single();
 
  private Single(){}
 
  public static Single getInstance()
  {
    return s;
  }
}
 

 


//Lazy (interview often asked, in the multi-threaded concurrent access may not be able to ensure the uniqueness of the object, there is a security risk!)
class Single2//The class loads in, there is no object, and the object is created only when the getInstance method is called.
      //Lazy loading form.
{
  private static Single2 s = null;
 
  private Single2(){}
 
  public static Single2 getInstance()
  {
    if(s==null)
      s = new Single2();
    return s;
  }
}
 
//Call the 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; // I'm not using this because I can't control it. I'm using this  Single.getInstance(); You can pass parameters to make the corresponding call. 
 
  }
}

The hungry singleton class instantiates itself when it is loaded. Even if the loader is static, it still instantiates itself when the hunchback singleton class is loaded. This is slightly worse than the lazy singleton class in terms of resource efficiency alone. From the point of view of speed and reaction time, it is better than the lazy singleton class.

3. Registration:

Code:


package pattern.singleton;import java.util.HashMap;import java.util.Map;
 //Register singleton class.
 //Similar to the Spring method, register the class name and get it directly from it next time.
public class Singleton3 {
 private static Map<String,Singleton3> map = new HashMap<String,Singleton3>();
 static{
 Singleton3 single = new Singleton3();
 map.put(single.getClass().getName(), single);
 }
 //The default constructor of the guard
protected Singleton3(){}
 //Static factory method that returns a 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 approach
public String about() {
 return "Hello, I am RegSingleton.";
 }
 public static void main(String[] args) {
 Singleton3 single3 = Singleton3.getInstance(null);
 System.out.println(single3.about());
 }
 }


Related articles: