On the implementation of the single-mode embodiment

Singleton pattern (from novice tutorial)

Singleton (Singleton Pattern) is one of the easiest in Java design patterns. This type of design pattern belongs create schema, which provides the best way to create objects. This model involves a single class that is responsible for creating your own objects, while ensuring that only a single object is created. This class provides the only way to access the object can directly access, no instance of the object class.

Singleton pattern requires

  • Constructors must privatize (make sure that only you can create)
  • In the static method returns an instance (not outside the object to be acquired via new)
  • To ensure that only one instance of an object (only once instance of the class, have direct access for the first time after the instantiated objects)

Examples of single-mode

1, lazy man's (thread safe)

Description: This is the most basic way of implementation, but does not support multi-threading. Because there is no lock, multi-threading does not work

//懒汉式(线程不安全)
public class Singleton {  
    private static Singleton instance;  

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

2, the lazy man's (thread-safe)

Description: the ability to work in a multi-threaded, however, the efficiency is very low

//懒汉式(线程安全)
public class Singleton {  
    private static Singleton instance;  

    private Singleton (){}  

    public static synchronized Singleton getInstance() {  
        if (instance == null) {  
            instance = new Singleton();  
        }  
        return instance;  
    }  
}

3, double check the lock (DCL, that is double-checked locking) (thread-safe)

Description: optimization lazy formula (thread-safe), the use of the double lock mechanism, and in safety can maintain high performance multithreading

//双重校验锁
public class Singleton {
    private volatile static Singleton instance;

    private Singleton() {

    }

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

4, hungry man type (thread-safe)

Description: This method commonly used, but prone garbage objects

//饿汉式
public class Singleton {
    private static Singleton instance = new Singleton();

    private Singleton() {

    }

    public static Singleton getInstance() {
        return instance;
    }
}

5, static inner classes (thread-safe)

Description: This way to achieve the same lock with double check effect, this approach is only applicable to the case of a still domain, double check the lock can be used when necessary to delay the initialization of instance fields

//静态内部类
public class Singleton {
    private static class SingletonHolder {
        private static final Singleton INSTANCE = new Singleton();
    }

    private Singleton() {

    }

    public static final Singleton getInstance() {
        return SingletonHolder.INSTANCE;
    }
}

6, enumeration (thread-safe)

Description: This method has not been widely adopted, but this is the best method to achieve single-mode embodiment. More concise, automatically supports serialization mechanism to prevent multiple instances of absolute

//枚举
public enum Singleton {
    INSTANCE;

    public void whateverMethod() {

    }
}

to sum up

Under normal circumstances, it is not recommended to use the first type and second type lazy manner recommended fourth starving embodiment. Only when a clear realization of lazy loading, will use the first five kinds of static inner class way. If it comes to deserialize create an object, you can use the sixth enumeration method. If there are other needs, consider using the first three kinds of double checking lock mode.

PS: start facing spring resort, it has been a lot of face when it comes to design patterns. The most common interview questions is to realize and understand talk about singletons, so I write about, to deepen my understanding and impression of the Singleton pattern. Find help if you can little praise, thank you ~ ~ ~

Released six original articles · won praise 6 · views 127

Guess you like

Origin blog.csdn.net/weixin_37686415/article/details/105164764