Java self-study-class and object singleton mode

Keywords: Java jvm Attribute Database

Java Hungry Han and Lazy Singleton Patterns

There is a strange Dragon named GiantDragon in LOL, only one, so this class can only be instantiated once

Step 1: Singleton mode

Singleton mode, also known as Singleton mode, refers to a class in which only one instance exists in a JVM.

Step 2: Hungry Han singleton mode

GiantDragon should only have one, making it impossible for the outside to get new instances through new by privatizing its construction methods.
GiantDragon provides a public static getInstance method through which external callers get 12-line-defined objects, each time the same object is obtained.This achieves the purpose of the singleton.
This singleton pattern is also known as the Hungry Han singleton pattern and creates an instance anyway

package charactor;
 
public class GiantDragon {
 
    //The privatization construction method prevents the class from being externally instantiated with new
    private GiantDragon(){
         
    }
 
    //Prepare a class property to point to an instantiated object.Because it is a class attribute, there is only one
 
    private static GiantDragon instance = new GiantDragon();
     
    //The public static method, which provides the caller with an object defined in 12 rows
    public static GiantDragon getInstance(){
        return instance;
    }
     
}

.

package charactor;
 
public class TestGiantDragon {
 
    public static void main(String[] args) {
        //Error will occur with new instantiation
//      GiantDragon g = new GiantDragon();
         
        //Objects can only be obtained through getInstance
         
        GiantDragon g1 = GiantDragon.getInstance();
        GiantDragon g2 = GiantDragon.getInstance();
        GiantDragon g3 = GiantDragon.getInstance();
         
        //Are all the same object
        System.out.println(g1==g2);
        System.out.println(g1==g3);
    }
}

Step 3: Lazy Singleton Mode

Lazy Singleton mode is different from Hungry Singleton mode in that instances are created only when getInstance is called

package charactor;
 
public class GiantDragon {
  
    //The privatization construction method prevents the class from being externally instantiated with new
    private GiantDragon(){       
    }
  
    //Prepare a class property to point to an instantiated object, but temporarily to null
    private static GiantDragon instance;
      
    //public static method, returns instance object
    public static GiantDragon getInstance(){
        //The first time you visit an instance, you find that it does not point to any object, and instantiate an object
        if(null==instance){
            instance = new GiantDragon();
        }
        //Returns the object to which instance points
        return instance;
    }
      
}

.

package charactor;
 
public class TestGiantDragon {
 
    public static void main(String[] args) {
        //Error will occur with new instantiation
//      GiantDragon g = new GiantDragon();
         
        //Objects can only be obtained through getInstance
         
        GiantDragon g1 = GiantDragon.getInstance();
        GiantDragon g2 = GiantDragon.getInstance();
        GiantDragon g3 = GiantDragon.getInstance();
         
        //Are all the same object
        System.out.println(g1==g2);
        System.out.println(g1==g3);
    }
}

Step 4: When to use the Hungry Man style and when to use the Lazy Man style

Hungry Han is the way to load immediately, whether or not this object is used.
If you write performance-intensive, time-consuming code in the construction method, such as establishing a connection to a database, you will feel a little stuck at startup.

Lazy, delayed loading, loads only when used.And there are Thread Security Considerations.
With the Lazy Man style, you will feel slightly faster at startup than the Hungry Man style, because there is no instantiation of the object.But on the first call, the instantiation takes place and feels a little slower.

Depending on your business needs, use the Hungry Man style if your business allows sufficient start and initialization time, otherwise use the Lazy Man style

Step 5: Single Case Mode Three Elements

This is the point that is often taken during an interview. The common question asked in an interview is:

What is the singleton pattern?

When answering, there are three elements to answer:

  1. Privatization of construction methods
  2. Static property points to instance
  3. public static's getInstance method, returns the static property of the second step

Posted by DimeDropper on Fri, 06 Sep 2019 19:50:48 -0700