Singleton design pattern

Time:2021-3-5
  1. Singleton: a class can have only one instance object;
  2. How to implement singleton?
    1) Hungry man style: no matter whether the user needs the object or not, the object is created first;

    //① Enumeration
    public class SingletonPattern1 {
    
        public static void main(String[] args) {
            SingleEnum singleEnum1 = SingleEnum.INSTANCE;
            SingleEnum singleEnum2 = SingleEnum.INSTANCE;
            System.out.println(singleEnum1==singleEnum2);//true
        }
    }
    enum SingleEnum{
        INSTANCE
    }
    //② The constructor is privatized, and the global static variable is used to save the unique instance object;
    
    public class SingletonPattern2 {
    
    public static void main(String[] args) {
    
            Single single1 = Single.INSTANCE;
            Single single2 = Single.INSTANCE;
        
            System.out.println(single1==single2);//true
        }
    }
    
    class Single{
    
        public static final Single INSTANCE = new Single();
    
        private Single(){
    
        }
    }
    //③ The constructor is privatized. It uses private static weighing to save the unique instance object and provides static methods to return the constant object;
    public class SingletonPattern3 {
    
    public static void main(String[] args) {
    
            SingleClass single1 = SingleClass.getInstance();
            SingleClass single2 = SingleClass.getInstance();
            System.out.println(single1==single2);//true
        }
    }
    
    class SingleClass{
        private static final SingleClass INSTANCE = new SingleClass();
    
        private SingleClass(){
    
        }
        public static SingleClass getInstance(){
            return INSTANCE;
        }
    }

    2) Lazy: delay the creation of an object, which is created only when the user is using it;

    //① Common form
    public class SingletonPattern4 {
        public static void main(String[] args) {
    
            SingleClass4 single1 = SingleClass4.getINSTANCE();
            SingleClass4 single2 = SingleClass4.getINSTANCE();
            System.out.println(single1==single2);
        }
    }
    
    class SingleClass4{
        private static SingleClass4 INSTANCE;
        private SingleClass4(){
    
        }
    
        public static SingleClass4 getINSTANCE(){
    
            If (instance = = null) {// improve efficiency
                Synchronized (singleclass4. Class) {// ensure thread safety
                    If (instance = = null) {// judge implementation singleton
                        INSTANCE = new SingleClass4();
                    }
                }
            }
            return INSTANCE;
        }
    }
    //② Inner class form:
    public class SingletonPattern5 {
        public static void main(String[] args) {
    
            SingleClass5 s1 = SingleClass5.getINSTANCE();
            SingleClass5 s2 = SingleClass5.getINSTANCE();
            System.out.println(s1==s2);
        }
    }
    class SingleClass5{
    
        //Privatization constructor
        private SingleClass5(){
    
        }
        //Create the inner class and the unique object of the outer class;
        static class Inner{
            private static final SingleClass5 INSTANCE = new SingleClass5();
        }
        //Define methods to provide class objects
        public static SingleClass5 getINSTANCE(){
            return Inner.INSTANCE;
        }
    }

Statement: This article is compiled after self-study, for reference only, if there are omissions, thank you for your correction;