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.
Creation design mode-singleton Mode