What is the singleton pattern

  • The article was first published on the author’s wechat official account [Code ape Technology Column]Design pattern: singleton pattern
  • Singleton Pattern is a relatively simple Pattern that is widely used in practice, such as in Spring
    BeanAn instance is a singleton object.
  • Definition: Ensures that a class has only one instance, instantiates it itself and makes it available to the entire system.

Advantages and disadvantages of the singleton pattern

  • Evaluation of any kind of business should be seen from two aspects, not a single evaluation. Let’s look at the pros and cons of the singleton pattern.

advantages

  • Having only one instance reduces memory overhead, especially when instances are frequently created and destroyed.
  • The singleton mode can avoid multiple resource usage, such as a write file operation. Because only one instance exists in memory, simultaneous write operations on the same resource file are avoided.
  • The singleton pattern can set up global access points in the system, optimize and share resource access, for example, you can design a singleton class, responsible for all data table mapping processing.

disadvantages

  • The singleton pattern generally has no interface and is difficult to extend (depending on the environment).
  • The singleton pattern conflicts with the single responsibility principle. A class should only implement one logic, regardless of whether it is a singleton.

Implementation of singleton pattern

  • There are many implementations of the singleton pattern, but each implementation has its advantages and disadvantages. Let’s take a look at the various implementations.
  • The implementation of the singleton pattern meets the following requirements:

    • Constructors are private.
    • There is a static method to get an instance of the class.
    • This class is guaranteed to have only one instance.

LanHanShi

  • Lazy style is created when the object is used.
  • Advantages: This object is created only when needed, thus saving resources.
  • The simple implementation is as follows:
public class Singleton {  
    private static Singleton instance;  
    private Singleton (){}  
  
    public static Singleton getInstance() {  
    if (instance == null) {  
        instance = new Singleton();  
    }  
    returninstance; }}Copy the code
  • The above code basically satisfies the singleton pattern, but it does not satisfy the thread-safe, once in the multi-threaded environment is still not guaranteed to be a singleton class.
  • How do we keep thread-safe, of course, by locking, we can do thread-synchronous locking
    synchronized, the implementation is as follows:
public class Singleton {  
    private static Singleton instance;  
    private Singleton (a){}  
    public static synchronized Singleton getInstance(a) {  
    if (instance == null) {  
        instance = new Singleton();  
    }  
    returninstance; }}Copy the code
  • Although this implementation satisfies thread safety, locking will inevitably affect performance.

The hungry type

  • The hunger-style is the opposite of the slacker style, where the instance is created when the class is loaded.
  • Advantages: Not used to create, waste resources.
  • Disadvantages: Created at class load time, thread-safe.
  • The implementation is as follows:
public class Singleton {  
    private static Singleton instance = new Singleton();  
    private Singleton (a){}  
    public static Singleton getInstance(a) {  
    returninstance; }}Copy the code

Double check lock

  • Is a lazy load of a way of implementation, the use of double check mechanism, in the case of multithreading still maintain high performance.
  • The implementation is as follows:
public class Singleton {  
    private volatile static Singleton singleton;  
    private Singleton (a){}  
    public static Singleton getSingleton(a) {  
    if (singleton == null) {  
        synchronized (Singleton.class) {  
        if (singleton == null) {  
            singleton = newSingleton(); }}}returnsingleton; }}Copy the code

Anonymous inner class/static inner class

  • Take advantage of the principle that static variables, static code blocks, and static methods are loaded only once when the class is loaded.
  • The implementation is as follows:
public class Singleton {  
    private static class SingletonHolder {  
    private static final Singleton INSTANCE = new Singleton();  
    }  
    private Singleton (a){}  
    public static final Singleton getInstance(a) {  
    returnSingletonHolder.INSTANCE; }}Copy the code

conclusion

  • While the implementation of the singleton pattern is simple, there are performance and thread-safety issues to ensure implementation.
  • The main realization of singleton mode is introduced above, respectively
    Lazy threads are not safe,
    Lazy thread safety,
    The hungry type,
    Realization of double check lock,
    Implementation of static inner classes. These ways have advantages and disadvantages, Chen recommended the use of static internal class implementation.
  • The singleton pattern is also a design pattern used by Spring, ensuring that
    BeanTo manage and monitor Bean creation and destruction.