1. What is the singleton pattern?
Singleton: Ensure that a class has only one instance and provide a global access point to access it.
The singleton pattern is one of the commonly used software design patterns. Its purpose is to ensure that there is only a single instance of a class in the entire application.
For example, when we start the system, we need to load some public configuration information, which is visible and unique throughout the entire life cycle of the application. In this case, we need to design it in singleton mode. Such as: spring container, session factory, cache, database connection pool, etc.
2. How to ensure the uniqueness of the instance
1) Prevent external initialization
2) Instantiate by the class itself 3) Guarantee instantiation once
4) Provide external methods to obtain instances
5) Thread safety
3. Comparison of several simple profit models
(1) Hungry Chinese style
"Because I am hungry, I have to eat immediately without delay", while defining the static private variables of the class Perform instantiation.
public class Singleton { private static final Singleton singleton = new Singleton(); private Singleton() { } public static Singleton getInstance() { return singleton; } }
①Declare static private class variables and instantiate them immediately, ensuring one instantiation
②Private construction to prevent external instantiation (it can be instantiated through reflection, this situation is not considered)
③Provide public The getInstance() method is used to obtain singleton instances externally
Benefits: Thread safety; fast acquisition of instances Disadvantages: Class loading means initializing the instance, memory waste
(2) Lazy style
"This person is lazy, wait for it "Instantiate it when you need it", lazy loading.
public class Singleton { private static Singleton singleton = null; private Singleton() { } public static Singleton getInstance() { if (singleton == null) { singleton = new Singleton(); } return singleton; } }
Advantages: In the method of obtaining the instance, initialize the instance and save system resources
Disadvantages: ①If there is a lot of initialization work when obtaining the instance, the loading speed will slow down and affect the system performance
②Every time Obtaining an instance requires a non-null check, which causes high system overhead. It is not thread-safe. When multiple threads access getInstance() at the same time, multiple instances may be generated. Next, perform a thread-safe transformation on it:
1) Synchronization lock
public synchronized static Singleton getInstance() { if (singleton == null) { singleton = new Singleton(); } return singleton; }
Advantages: Thread safety, Disadvantages: Locking is required every time you obtain an instance, which consumes resources. In fact, as long as the instance has been generated, you do not need to lock it again for subsequent acquisitions
2) Double check lock
public static Singleton getInstance() { if (singleton == null) { synchronized (Singleton.class) { if (singleton == null) { singleton = new Singleton(); } } } return singleton; }
Advantages: Thread safety, double checking, ensuring synchronization only before the instance is initialized, high efficiency Disadvantages: Still judging whether the instance is not empty, consuming certain resources
3) Static inner class
public class Singleton { private Singleton() { } private static class SingletonHolder { private static final Singleton singleton = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.singleton; } }
Advantages: both It avoids the performance loss caused by synchronization and can delay loading
(3) Enumeration
public enum Singleton { INSTANCE; public void init() { System.out.println("资源初始化。。。"); } }
is naturally thread-safe and prevents reflection from generating instances.
4. Advantages and Disadvantages of Singleton Pattern