Singleton mode
Ensure that only one instance of an object can exist for a class throughout the software system and that the class only provides a method (static method) to get its object instance. Example: Database client, make sure that only one access class is created for one client access.
-
Hungry Han Style (Static Constant)
class Singleton{ // Constructor Privatization private Singleton(){ } // Create object instances inside this class private final static Singleton instance = new Singleton(); // Provide a common static method that returns an instance object public static Singleton getInstance(){ return instance; } }
Instantiation is done when the class is loaded to avoid thread synchronization issues, but memory waste occurs when the fruit instance is not used.
-
Hungry Han Style (Static Code Block)
class Singleton{ // Constructor Privatization private Singleton(){ } // Create object instances inside this class private static Singleton; // Create a singleton object in a static block of code static{ instance = new Singleton(); } // Provide a common static method that returns an instance object public static Singleton getInstance(){ return instance; } }
Ditto.
-
Lazy (thread insecure)
class Singleton { private static Singleton instance; private Singleton(){} //Use this static method to create an instance public static Singleton getInstance() { if(instance == null){ instance = new Singleton(); } return instance; } }
Plays the role of lazy loading, thread insecurity, actual development is not used. One thread has entered the judgment statement before it is time to execute down, and the other thread has passed the judgment statement, resulting in multiple instances.
-
Lazy (thread-safe, synchronization method)
class Singleton { private static Singleton instance; private Singleton(){} //Synchronization to solve thread security issues public static synchronized Singleton getInstance() { if(instance == null){ instance = new Singleton(); } return instance; } }
Low efficiency, not used in actual development.
-
Lazy (Thread Safe, Synchronize Code Blocks)
class Singleton{ private static Singleton instance; private Singleton(){} public static Singleton getInstance(){ if(instance == null){ synchronized(Singleton.class){ instance = new Singleton(); } } return instance; } }
Thread insecure, wrong method, no use.
-
duplication check
class Singleton{ private static volatile Singleton instance; private Singleton(){} public static Singleton getInstance() { if(instance == null){ synchronized(Singleton.class){ if(instance == null){ instance = new Singleton(); } } } return instance; } }
Twice if checks are common in double-check multithreading to ensure thread safety. The instantiation code is executed only once, and when accessed again later, if is judged, the instantiated object is return ed directly, avoiding repeated method synchronization. Thread-safe, delayed loading, and extremely efficient.
-
Static Internal Class
class Singleton{ private static volatile Singleton instance; private Singleton(){} private static class SingletonInstance{ private static final Singleton INSTANCE = new Singleton(); } public static synchronized Singleton getInstance(){ return SingletonInstance.INSTANCE; } }
The static internal class SingletonInstance is not loaded when the Singleton class is loaded. When getInstance() is called, the SingletonInstance class is loaded and the Singleton class is instantiated. The static properties of the class are only initialized when the class is first loaded and are always thread-safe during class loading (JDK ensures). Thread-safe, delayed loading with static internal class characteristics, high efficiency. Recommended.
-
enumeration
-
enum Singleton{ INSTANCE; public void sayOK(){ System.out.printlen("ok"); } }
It is recommended to enumerate thread safety and prevent deserialization from re-creating new objects.