Guarantee that a class has only one instance and provide a global access point to access it.

Second, implementation ideas

(1) Privatization construction method
(2) Class internal construction instance
(3) Provide a common static method that returns an object instance

Three or seven implementation codes

(1) Hungry (static constant)
// Advantages: Classes are instantiated when loaded, avoiding thread synchronization issues
// Disadvantages: Memory is wasted if not used
// Conclusion: It can be used in development, but may cause memory waste
public class SingletonHungry{
    //1
    private SingletonHungry(a) {}// create an object instance within the class
    private final static SingletonHungry instance=new SingletonHungry() ;
    //3, provide a public static method that returns an instance object
    public static SingletonHungry getInstance(a) {
        returninstance; }}Copy the code
(2) Hungry (static code block)
// Advantages: Classes are instantiated when loaded, avoiding thread synchronization issues
// Disadvantages: Memory is wasted if not used
// Conclusion: It can be used in development, but may cause memory waste
public class SingletonHungry {
    //1
    private SingletonHungry(a) {}// create an object instance within the class
    private final static SingletonHungry instance ;
    static  { // Load in a static code block
        instance = new SingletonHungry();
    }
    //3, provide a public static method that returns an instance object
    public static SingletonHungry getInstance(a) {
        returninstance; }}Copy the code
(3) Lazy (threads are not safe)
// Advantages: It is created only when used, so there is no memory waste
// Disadvantages: Multi-threaded environments may create multiple
// Conclusion: Not used in development.
public class SingletonLazy {

    private SingletonLazy(a) {}
    private static SingletonLazy instance;
    
    public static SingletonLazy getInstance(a) {
        if (instance == null) {
            instance =new SingletonLazy();
        }
        returninstance; }}Copy the code
(4) Synchronized
// Advantages: Thread-safe
// Disadvantages: inefficient
// Conclusion: Not recommended for development
public class SingletonLazy {
    private SingletonLazy(a) {}
    private static SingletonLazy instance;

    public static synchronized SingletonLazy getInstance(a) {
        if (instance ==null) {
            instance = new SingletonLazy();
        }
        returninstance; }}Copy the code
(5) Lazy (double check)
// double check
// Advantages: Solve thread synchronization and efficiency problems
// Conclusion: Recommended
/** * Note: * If the volatile keyword is not used, an exception may be generated because * instance=new SingletonLazy() is not atomic. It is divided into three parts: * 1, allocating memory for an object * 2, initializing an object * 3, instance pointing to memory * Instruction reordering may occur when the compiler runs, 132, which may cause an uninitialized object to be returned. * * Volatile is used to prevent instruction reordering */
public class SingletonLazy {
    private SingletonLazy(a) {}

    private static volatile SingletonLazy instance;

    public static SingletonLazy getInstance(a) {
        if (instance ==null) {
            synchronized (SingletonLazy.class) {
                if (instance==null) {
                    instance=newSingletonLazy(); }}}returninstance; }}Copy the code
(6) Lazy mode static inner class

/** * Static inner class features: * 1, SingletonStaticClass does not load when loaded (avoids memory waste) * 2, thread-safe when loaded. * Conclusion: */ is recommended

public class SingletonStaticClass {
    private SingletonStaticClass(a) {}private static class SingletonStaticClassInstance{
        private static final SingletonStaticClass instance = new SingletonStaticClass();
    }
    public static SingletonStaticClass getInstance(a) {
        returnSingletonStaticClassInstance.instance; }}Copy the code
(7) Enumeration
// Not only can solve the problem of multithreading and memory waste. It also prevents reserialization from creating new objects
// Conclusion: Recommended
enum SingletonEnum {
    INSTANCE;
        public SingletonEnum getInstance(a){
        returnINSTANCE; }}Copy the code