Creation design mode-singleton Mode
1: Singleton mode:
Ensure that there is only one instance in a class and provide a method to access its instance.
The most awesome Singleton mode is a dual test:
Class Singleton {private Singleton () {}; // private method private static Singleton instance = null; // defines the instance of the class as a static public Singleton getInstanSingleton () {if (instance = null) {// because synchronization is time-consuming, first determine whether it is null and then synchronize synchronized (Singleton. class) {// synchronize the class Object of Singleton; if (instance = null) {// because of the preceding synchronization, in multi-thread mode, other threads may have already created instances. Therefore, you must make another judgment at this time! Instance = new Singleton () ;}} return instance ;}}
Java source code example: Runtime. getRuntime (); returns the Runtime object related to the current Java application.
Calendar. getInstance (); obtain a Calendar in the current environment;
The application scenario is: resource management, such as windows recycle bin, only one.