Creation mode -- singleton mode

Keywords: Java Attribute

1. Lazy mode

1. Build a single instance object

ps: to test whether the Singleton object created again is the same object, add the attribute name

public class Singleton {
    //Add attribute name
    private String name;
    private static Singleton instance;
    //Private constructor,Make it impossible to pass new Get the object
    private Singleton() {}
    public static Singleton getInstance() {
        if (instance == null) {//Judge whether it is empty,Implementation of single example
            instance = new Singleton();
        }
        return instance;
    }
    //get and set Method
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Singleton [name=" + name + "]";
    }
}

 

2. Test

public class HungryModeTest {
    @Test
    public void hungryMode(){
        //Created by its methods singleton object
        Singleton singleton = Singleton.getInstance();
        //by name Property assignment
        singleton.setName("Zhang San");
        System.err.println(singleton.toString());
        //Create a new singleton object singletonNew
        Singleton singletonNew= Singleton.getInstance();
        System.err.println(singletonNew.toString());
    }
}

3. Test output

4. Summary:

Lazy type only creates objects when needed, but when multiple threads load at the same time, lazy type will have the problem of constructing multiple objects at the same time. Lazy type is thread unsafe

2. Lazy mode of thread safety

1. Try to add thread lock for ordinary lazy people

ps: add red part as thread lock to solve the problem of multithreading

public class Singleton {
    //Add attribute name
    private String name;
    private static Singleton instance;
    //Private constructor,Make it impossible to pass new Get the object
    private Singleton() {}
    public static synchronized Singleton getInstance() {
        if (instance == null) {//Judge whether it is empty,Implementation of single example
            instance = new Singleton();
        }
        return instance;
    }
    //get and set Method
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Singleton [name=" + name + "]";
    }
}

 

3. Hungry Han style

1. Create a singleton object

public class Singleton {
    // Add attribute name
    private String name;
    private static Singleton instance = new Singleton();
    //Private constructor
    private Singleton() {}
    public static Singleton getInstance() {
        return instance;
    }
    // get and set Method
    public String getName() {return name;}
    public void setName(String name) {this.name = name;}
    public String toString() {return "Singleton [name=" + name + "]";}
}

2. Test

ps: the test code is the same as the lazy test code above

3. Test results

 

4. Double check lock

1. The lazy mode with lock will delay loading, and double check lock is a further improvement

public class Singleton {
    // Add attribute name
    private String name;
    //adopt volatile Reordering optimization of forbidden instructions,Guarantee object initialization
    private static volatile Singleton instance = null;
    private Singleton() {}
    public static Singleton getInstance() {
        if (instance == null) {//Example of blank judgment guarantee
            synchronized (Singleton.class) {//Line lock
                if (instance == null) {//In a blank judgment,Ensure multithreading
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
    // get and set Method
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String toString() {
        return "Singleton [name=" + name + "]";
    }
}

 

5. Static internal class

1. Using class loading mechanism to ensure that only one instance is created

public class Singleton {
    // Add attribute name
    private String name;
    private static class SingletonInner{ //Inner class 
        public static Singleton instance = new Singleton();  
    }  
    private Singleton(){}  //Private structure
    public static Singleton newInstance(){  
        return SingletonInner.instance;  
    }  
    // get and set Method
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String toString() {
        return "Singleton [name=" + name + "]";
    }
}

 

 

Welcome to reprint:

Chinese Name: Huifan

Blog name: drowned fish o0

When reprinting, please indicate the source: http://www.cnblogs.com/huifan/

Posted by furtivefelon on Sun, 03 May 2020 17:36:39 -0700