I, Structure of Singleton Mode
The Singleton mode has three key points:
First, a class can only have one instance;
Second, it must create this case on its own;
Third, it must provide the instance to the entire system.
Hungry Chinese Singleton type
The source code is as follows:
Public class eagersingleton <br/>{< br/> Private Static final eagersingleton m_instance = new eagersingleton (); <br/> private eagersingleton () {}< br/> Public static eagersingleton getinstance () <br/>{< br/> return m_instance; <br/>}< br/>}
Feature: When a class is loaded, the static variable m_instance will be initialized, and the private constructor of the class will be called. At this time, the unique instance of the class will be created.
Lazy Singleton type
The source code is as follows:
Public class lazysingleton <br/>{< br/> Private Static lazysingleton m_instance = NULL; <br/> private lazysingleton () {}< br/> synchronized public static lazysingleton getinstance () <br/>{< br/> If (m_instance = NULL) <br/> m_instance = new lazysingleton (); <br/> return m_instance; <br/>}< br/>}
Feature: When a class is loaded, m_instance is initialized and initialized only when it is referenced for the first time. In addition, the static factory method is synchronized in the above program to process multi-threaded environments. Some designers suggest using the so-called "double check Examples" here. It must be noted that "double check Examples" cannot be used in Java.
Comparison between hungry Chinese and lazy Chinese:
From the perspective of resource utilization efficiency: The ELE. Me Singleton class is slightly less efficient, so the ELE. Me Singleton class instantiates itself when it is loaded.
From the perspective of speed and response time: the hungry Chinese Singleton class is a little better, because when the lazy Singleton class is instantiated, you must handle the access restriction issue when multiple threads reference this class for the first time.
From a Language Perspective: The hungry Chinese style is easy to implement in Java and not easy to implement in C ++, because static initialization does not have a fixed sequence in C ++, and problems may occur, therefore, using the hungry Chinese Style in Java is better, and using the lazy Chinese Style in C ++ is better.
Registration type Singleton type
Overcome the disadvantages that the hungry and lazy standalone classes cannot inherit.
The Code is as follows:
// Regsingleton <br/> package COM. FG. singleton; <br/> Import Java. util. hashmap; <br/> public class regsingleton <br/>{< br/> static private hashmap m_registry = new hashmap (); <br/> static <br/> {<br/> regsingleton x = new regsingleton (); <br/> m_registry.put (X. getclass (). getname (), x); <br/>}< br/> protected regsingleton () {}< br/> static public regsingleton getinsatance (string name) {<br/> If (name = NULL) {<br/> name = "com. FG. singleton. regsingleton "; <br/>}< br/> If (m_registry.get (name) = NULL) {<br/> try {<br/> m_registry.put (name, class. forname (name ). newinstance (); <br/>} catch (exception e) {<br/> system. out. println ("error happened"); <br/>}< br/> return (regsingleton) (m_registry.get (name )); <br/>}</P> <p> // regsingletonchild <br/> package COM. FG. singleton; <br/> public class regsingletonchild extends regsingleton {<br/> Public regsingletonchild () {}< br/> static public regsingletonchild getinstance () {<br/> return (regsingletonchild) regsingleton. getinsatance ("com. FG. singleton. regsingletonchild "); <br/>}< br/>}
Two cases of incorrect Singleton mode:
1. Do not use a singleton class to store all the "full process" variables. This does not meet the three conditions of Singleton mode and violates the intention of Singleton mode. A well-designed system should not have so-called "full-course" variables, which should be placed in the classes corresponding to the entities they describe. Extract these variables from the entity classes they describe and place them in an unrelated Singleton class, which produces incorrect dependencies and Coupling Relationships.
2. When you manage connections to databases in the system, use the singleton class to wrap a connection object and close the connection object in the Finalize method. This is not appropriate. Do not use the singleton mode unless you have a single instance. Here, the connection object can coexist with several instances, not a single instance.
II, Application of Singleton mode in Java
JavaRuntimeObject
Within the Java language, the Java. Lang. runtime object is an example of using the singleton mode. Each Java application has a unique runtime object. The runtime class provides a static factory method getruntime (): Public static runtime getruntime (). The common purposes of a runtime object include executing external commands and returning existing memory, that is, all the memory; run the Garbage Collector and load the dynamic library.
Introspector (Introspection)Class
The constructor class constructor is private. A static factory method instantiate () provides a unique instance of the introspector class. Generally, the beaninfo information of an object is obtained through the introspector class, and the property descriptor (propertydescriptor) is obtained through beaninfo ), the property descriptor can be used to obtain the getter/setter method corresponding to a property, and then we can call these methods through the reflection mechanism.