Design Patterns--Singleton Pattern

1. Evil Chinese style: static constants

    Singleton instances are declared static and final, and are initialized when the class is loaded into memory, and are also thread-safe .

public class Singleton1 {  
  
    private final static Singleton1 instance = new Singleton1();  
    private Singleton1(){}  
    public static Singleton1 getInstance(){  
        return instance;  
    }  

       Disadvantage: not lazy loading, singleton will be initialized after the class is loaded, even if the getInstance method is not called. It will not be available in some scenarios: for example, the creation of a Singleton instance depends on parameters or configuration files, and a method must be called to set parameters to it before getInstance(), so the singleton writing method cannot be used.


2. Lazy Man: Thread Unsafe

public class Singleton2 {  
      
    private static Singleton2 instance ;  
    private Singleton2(){}  
    public  static Singleton2 getInstance(){  
        if(instance == null){  
            instance = new Singleton2();  
        }  
        return instance;  
    }  
}  

Disadvantage: When multiple threads call getInstance in parallel, it does not work properly (there are thread safety issues)

3. Lazy Man: Thread Safety

public class Singleton4 {  
  
    private static Singleton4 instance;  
    private Singleton4(){}  
    public static synchronized Singleton4 getInstance(){  
        if(instance == null){  
            instance = new Singleton4();  
        }  
        return instance;  
    }  

 --Although it is thread-safe, it is not efficient.

4. Lazy Man: Static Inner Classes (recommended)

public class Singleton5 {  
  
    private static class SingletonHandler{  
        private static final Singleton5 INSTANCE = new Singleton5();  
    }  
    private Singleton5(){}  
    public static Singleton5 getInstance(){  
        return SingletonHandler.INSTANCE;  
    }  

-- There is no synchronization when reading instances, no performance flaws, and no dependency on JDK version.

5. Double check lock ( recommended )

There may be multiple threads entering the if outside the synchronized block together, and multiple instances will be generated if the second check is not performed in the synchronized block.

public class Singleton6 {  
      
    private static Singleton6 instance;  
    private Singleton6(){}  
    public static Singleton6 getSingleton6(){  
        if(instance==null){  
            synchronized(Singleton6.class){  
                if(instance==null){  
                   instance = new Singleton6();  
                }  
            }  
        }  
        return instance;  
    }  
public class Singleton6 {  
      
    private volatile static Singleton6 instance;  
    private Singleton6(){}  
    public static Singleton6 getSingleton6(){  
        if(instance==null){  
            synchronized(Singleton6.class){  
                if(instance==null){  
                   instance = new Singleton6();  
                }  
            }  
        }  
        return instance;  
    }  

    --Thread safe but inefficient

6. Enumeration

public class Singleton7 {  
  
    public enum EasySingleton{  
        INSTANCE;  
    }  
}  

--Creating an enumeration is thread-safe by default, and it also prevents deserialization from causing re-creation of new objects. Accessible directly through EasySingleton.INSTANCE.


Reference: bingogirl Hirofumi




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325689458&siteId=291194637