Application scenarios of the singleton mode
The singleton pattern can be used in scenarios where only one object is needed for the entire program to run and multiple objects cannot be created. There are several common singleton patterns: 1, idlers mode 2, the hungry mode 3, enumeration singleton 4, 5, double lock the following static inner class will introduce the use of specific methods, it is important to note there is no perfect singleton pattern, only the most appropriate you use the singleton pattern of project, in the use of different scenarios, to think, use what kind of singleton pattern is the most suitable.
1. Hungrier Mode
The Hanky-hank pattern is inherently thread-safe because static objects are created for external use when the Singleton Class is generated, and the final keyword is declared.
public class Singleton {
private static final Singleton instance = new Singleton();
private Singleton(){
}
public static Singleton getInstance() {
returninstance; }}Copy the code
2. Slacker mode
The lazy mode is to declare a static object, using the constructor of private permissions and the getInstance() method to provide externally accessible access. The getInfo() method is added to the singleton pattern to illustrate examples for external calls. Here are some examples of thread-safe and non-thread-safe implementations, and during actual development, choose a slacker mode that fits your own project requirements. 1, 1 non-thread-safe implementation of non-thread-safe lazy mode process:
public class Singleton {
private static Singleton instance;
private Singleton(){
}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
public void getInfo() {
Log.i("Singleton"."Hello Singleton"); }}Copy the code
Call method:
Singleton.getInstance().getInfo();Copy the code
Print:
Singleton: Hello SingletonCopy the code
To implement thread-safe lazy mode, simply add the synchronized keyword to the getInstance() method. The concern in this threading mode is resource consumption, because every time the getInstance() method is called, a synchronization operation is performed, which is relatively inefficient. Procedure for implementing thread-safe lazy mode:
public class Singleton {
private static Singleton instance;
private Singleton(){
}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
public void getInfo() {
Log.i("Singleton"."Hello Singleton"); }}Copy the code
Enumerate singletons
Enumerations are the simplest code in a singleton implementation because enumerations are thread-safe by default in Java and are guaranteed to have only one instance because they can be serialized freely. Implementation process:
enum Singleton {
INSTANCE;
public void getInfo(){
Log.i("Singleton"."Hello Singleton"); }}Copy the code
Call method:
Singleton.INSTANCE.getInfo();Copy the code
Static inner class
Static inner classes are implemented in a manner similar to lazy mode in that they are not initialized when the Singleton is first loaded. Instead, they are initialized after the getInstance() method is called, which is thread-safe.
Implementation process:
public class Singleton {
private Singleton(){
}
public static class SingletonHolder{
private static final Singleton singleton = new Singleton();
}
public static Singleton getInstance() {return SingletonHolder.singleton;
}
public void getInfo() {
Log.i("Singleton"."Hello Singleton"); }}Copy the code
Call method:
Singleton.getInstance().getInfo();Copy the code
Double check locking
Double check refers to the nullity of the inttance object twice in getInstance() method. Combine that with a volatile declaration of instance objects to ensure thread-safety. Implementation process:
public class Singleton {
private volatile static Singleton instance = null;
private Singleton(){
}
public static Singleton getInstance() {
if(instance ! = null) { synchronized (Singleton.class) {if(instance ! = null) { instance = new Singleton(); }}}return instance;
}
public void getInfo() {
Log.i("Singleton"."Hello Singleton"); }}Copy the code
Call method:
Singleton.getInstance().getInfo();Copy the code