Single case mode of 23 design modes

Keywords: Java jvm

In core java, there are three types of design patterns, which can be further divided into the following parts:
1. Create design mode

  • Factory mode
  • Abstract factory pattern
  • Singleton mode
  • Prototype mode
  • Builder mode

Structural design mode

  • Adapter mode
  • Bridge mode
  • Combination mode
  • Decorator mode
  • Facade mode
  • Flyweight mode
  • Proxy mode

Behavior design pattern

  • Responsibility chain model
  • Command mode
  • Interpreter mode
  • Iterator mode
  • Mediator mode
  • Memo mode
  • Observer mode
  • State mode
  • Strategic model
  • Template mode
  • Visitor mode

Singleton mode

  • The single case model of the evil Chinese style
    Starved Chinese style (thread safety, high call efficiency, but can't delay loading)
public class SingletonDemo1 {
    //No parameter construction is privatized, only self initialization is allowed
    private SingletonDemo1() {

    }

    //When the class is initialized, load the object immediately (without delay). When the class is loaded, the thread is naturally safe.
    private static SingletonDemo1 instance = new SingletonDemo1();

    //Method is not synchronized, and the calling efficiency is high
    public static SingletonDemo1 getInstance() {
        return instance;
    }
}
  • Lazy singleton mode
    Lazy (thread safe, inefficient call, but can delay loading)
public class SingletonDemo2 {

    //Class initialization does not initialize this object (delay loading and create it when it is used).
    private static SingletonDemo2 instance;

    private SingletonDemo2() { //Privatization constructor
    }

    //Method synchronization, low call efficiency!
    public static synchronized SingletonDemo2 getInstance() {
        if (instance == null) {
            instance = new SingletonDemo2();
        }
        return instance;
    }

}
  • Implementation of double detection lock in singleton mode
public class SingletonDemo3 {
    private SingletonDemo3() {

    }

    /**
     * Disadvantages: there is an optimization of instruction reordering in the JVM's immediate compiler, which can be solved by using volatile
     */
    private volatile SingletonDemo3 instance;

    public SingletonDemo3 getInstance() {
        if (instance == null) {
            synchronized (SingletonDemo3.class) {
                if (instance == null) {
                    instance = new SingletonDemo3();
                }
            }
        }
        return instance;
    }
}
  • Static inner class implements singleton mode
 private static class SingleInstance {
        private static final SingletonDemo4 instance = new SingletonDemo4();
    }

    private SingletonDemo4() {

    }
     //Method is not synchronized, and the calling efficiency is high
    public static SingletonDemo4 getInstance() {
        return SingleInstance.instance;
    }
  • Implementation of singleton mode by enumeration
public enum SingletonDemo5 {
//This enumeration element itself is a singleton object!
    INSTANCE;
    //Add actions you need
    public void singletonOperation() {

    }
}

Posted by ChrisMayhew on Thu, 30 Apr 2020 11:16:27 -0700