java Singleton Mode (Hungry Man Mode and Lazy Man Mode)

  • 2021-10-13 07:11:20
  • OfStack

java singleton pattern

Hungry Han style single case

For the Hunger pattern, we can understand this: The singleton class is very hungry and desperate to eat, so it creates objects as soon as the class loads.

Lazy singleton class

For the lazy pattern, we can understand this: the singleton is very lazy, only acts when it needs to, and never knows how to prepare early. When it needs an object, it judges whether there is an object. If there is no object, it immediately creates an object and then returns. If there is an object, it will no longer be created and return immediately.

Singleton design patterns are commonly used in JDBC linked databases

Note:

1 We often use the first hungry Chinese style, because:

(1) Since the singleton design pattern is adopted, it is to use the object of singleton class, so it is instantiated first.

(2) In the lazy type, there is a certain security hazard, so it is necessary to add the synchronization keyword synchronized, otherwise it will not be a single case, but the efficiency will be slightly inferior if it is added with synchronized

2 In the first approach, the code private static final Single SINGLE=new Single (); Why is there an final?

Because classes modified by the final keyword cannot be inherited, member variables modified by final cannot be modified.

final is used here to reinforce and highlight the concept of "same 1 object"-there is only one object, and it cannot be modified.

If you don't use final to modify Single SINGLE, then there will be such a situation: the business is very complicated, and this object is inadvertently modified, causing various errors.

The first type (hungry Chinese type):

Thoughts:

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

Analysis:

Step 1: The constructor in the singleton class is privatized and an get () function is used to expose the object of this class, so other classes cannot construct the object of this class.
Step 2: But if other classes want to call the method in this singleton class, they have to use the inner name. Method name (), which requires that the method must be static static.
Step 3: Because static methods can only access static members! So you want to 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 Is the same 1 Objects are: s1==s2");//if Conditions hold  
       } 
   } 
} 
 
 
class Single{ 
  int number; 
  public int getNumber() { 
       return number; 
     } 
  public void setNumber(int number) { 
       this.number = number; 
     } 
  private Single(){};// No. 1 1 Step  
  private static final Single SINGLE=new Single();// No. 1 3 Step  
  public static Single getSingle(){// No. 1 1 Step sum 2 Step  
       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 Is the same 1 Objects are: s1==s2");//if Conditions hold  
       } 
     } 
 
 
} 
 
 
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(){// Add when multithreading synchronized Is the key! ! !  
      if(SINGLE==null){ 
         SINGLE=new Single(); 
         return SINGLE; 
       } 
      else{ 
         return SINGLE; 
        } 
   } 
} 


The above is a detailed explanation of java singleton design pattern. If you have any questions, please leave a message or discuss it in this site community. Thank you for reading it. I hope it can help you. Thank you for your support to this site!


Related articles: