single case mode definition
Single case mode is a common software design pattern. In its core structure, there is only one special class called a single example. A single example pattern ensures that only one instance of a class in a system is available. That is, a class has only one object instance
How to implement a single example pattern: lazy implementation, but not multiple-threaded under the security
public class Singleton {
private static Singleton instance;
Private Singleton () {} public
static Singleton getinstance () {
if (instance = = null) {
instance = new Singleto n ();
}
return instance
}
}
Lazy Implementation 2: Thread-safe (add synchronied keyword) (unfortunately multithreaded execution is inefficient, in most cases, multithreading is not required)
public class Singleton {
private static Singleton instance;
Private Singleton () {} public
static synchronized Singleton getinstance () {
if (instance = null) {
Instan CE = new Singleton ();
}
return instance
}
}
A hungry man implementation: thread-safe (because a single instance object of the class is created directly when the class is loaded, so the object is not different because of multithreading, but the implementation obviously does not achieve the lazy loading effect)
public class Singleton {
private static Singleton instance = new Singleton ();
Private Singleton () {} public
static Singleton getinstance () {return
instance;
}
}
a hungry man variants (similar to the third, different order of initialization)
public class Singleton {
private Singleton instance = null;
static {
instance = new Singleton ();
}
Private Singleton () {} public
static Singleton getinstance () {return
this.instance;
}
}
static inner class: (Implements the effect of the lazy loading, because the static inner class is not used when the outer class is loaded, and the static inner class is loaded when the getinstance method of the outer class is invoked, thus generating the external class instance of the singleton pattern)
When the public class Singleton {
//outer class is loaded, the inner class is not loaded until the GetInstance method is invoked, and therefore the effect of the lazy loading is also implemented as
private static Class Singletonholder {
private static final Singleton INSTANCE = new Singleton ();
}
Private Singleton () {} public
static final Singleton getinstance () {return
singletonholder.instance;
}
}
enumerations: (less used)
double-check Lock: A single case pattern implementation with the highest thread safety, but it does not guarantee that he will function on a single-processor or multiprocessor computer, which is due to the so-called "unordered input" allowed by the Java platform's memory model
public class Singleton {
private volatile static Singleton Singleton;
Private Singleton () {} public
static Singleton Getsingleton () {
if (Singleton = = null) {
//To prevent multithreading concurrency, Locks the current class
synchronized (singleton.class) {
if (Singleton = null) {
Singleton = new Singleton ();
}
}
}
Return singleton
}
}
single Case Mode usage scenario:
A single case mode is used when connecting to the database
A single case pattern used by the servlet container