Java singleton mode (hungry man mode and lazy man mode)

Time:2021-4-30

Java singleton mode

A single example of starving Han style

For the hungry man pattern, we can understand it as follows: the singleton class is very hungry and needs to eat urgently, so it creates an object immediately when the class is loaded.

Lazy singleton class

For the lazy mode, we can understand it as follows: This singleton class is very lazy. It only acts when it needs to, and never knows how to prepare as soon as possible. Only when it needs an object can it judge whether it has an object. If it does not, it creates an object immediately and returns it. If it has an object, it does not create any more and returns it immediately.

Singleton design pattern is often used in JDBC linked database

be careful:

1. We usually use the first hungry man style because:

 ( 1) Since the singleton design pattern is used to use singleton class objects, we instantiate them first

(2) There are some security risks in the lazy style, so we need to add the synchronized keyword, otherwise we can’t talk about singleton, but with synchronized, the efficiency is a little lower

In the first method, the code is private static final single single = new single(); Why is there final?

Because the class modified by final keyword cannot be inherited and the member variable modified by final cannot be modified

Final is used here to strengthen and highlight the concept of “the same object” – there is only one object, and it can’t be modified

If you don’t use final to modify single single, there will be such a situation: the business is very complex, and this object is modified inadvertently, resulting in various errors

The first type (starving Han style)

Thinking:

(1) Do not let other classes create objects of this class. That is to say, set the constructor to private!
(2) Customize an object of this class
(3) Provide the custom object. That is to define a get method, and the return value is the object of this class.

  analysis:

  Step 1: privatize the constructor in the singleton class and use a get() function to provide the object of this class, so other classes cannot construct the object of this class.
  Step 2: if other classes want to call the method in this singleton class, they can only use the inner name. Method name (), which requires the method to be static.
  Step 3: because static methods can only access static members! So set single to static

public class SingleDemo { 
  public static void main(String[] args) { 
     Single s1=Single.getSingle(); 
     s1.setNumber(44); 
     Single s2=Single.getSingle(); 
     s2.setNumber(55); 
     System.out.println("s1.number="+s1.number); 
     System.out.println("s2.number="+s2.number); 
     if(s1.equals(s2)){ 
        System. Out. Println ("S1 and S2 are the same object, that is: S1 = = S2")// If condition holds 
       } 
   } 
} 
 
 
class Single{ 
  int number; 
  public int getNumber() { 
       return number; 
     } 
  public void setNumber(int number) { 
       this.number = number; 
     } 
  private Single(){};// The first step 
  private static final Single SINGLE=new Single();// The third step 
  Public static single getsingle() {// step 1 and step 2 
       return SINGLE; 
  } 
}

The second type (lazy type)

package cn.com; 
public class SingleDemo2 { 
   public static void main(String[] args) { 
      Single s1=Single.getSingle(); 
      s1.setNumber(44); 
      Single s2=Single.getSingle(); 
      s2.setNumber(55); 
      System.out.println("s1.number="+s1.number); 
      System.out.println("s2.number="+s2.number); 
      if(s1.equals(s2)){ 
        System. Out. Println ("S1 and S2 are the same object, that is: S1 = = S2")// If condition holds 
       } 
     } 
 
 
} 
 
 
class Single{ 
   int number; 
   public int getNumber() { 
       return number; 
     } 
   public void setNumber(int number) { 
       this.number = number; 
     } 
   private Single(){}; 
   private static Single SINGLE=null; 
   Public static synchronized single getsingle() {// when multithreading, adding synchronized is the key!!! 
      if(SINGLE==null){ 
         SINGLE=new Single(); 
         return SINGLE; 
       } 
      else{ 
         return SINGLE; 
        } 
   } 
}

The above is the detailed explanation of Java singleton design mode. If you have any questions, please leave a message or go to our community to exchange and discuss. Thank you for reading. I hope I can help you. Thank you for your support!