Singleton schema definition
Definition: Ensures that a class has only one instance, instantiates it itself and makes it available to the entire system
Several implementations of the singleton pattern
Steps and Precautions:
Steps:
1) Define the constructor of the class as a private method, so that code elsewhere cannot call the constructor to instantiate objects of the class, only to get a unique instance of the class through static methods provided by the class;
2) Provide a static method within the class. When we call this method, we return the reference held by the class if it is not empty, and create an instance of the class and assign the reference of the instance to the reference held by the class if it is empty.
Note:
In multithreaded mode, care must be taken. If two threads call the create method at the same time while the unique instance is not yet created, they create one instance at the same time without detecting the existence of the unique instance, thus two instances are constructed, violating the singleton pattern’s instance uniqueness principle. The solution to this problem is to provide a mutex for variables that indicate whether or not the class has been instantiated (although this is less efficient).
1. Hungry (using static constants)
public class Singleton {
private final static Singleton INSTANCE = new Singleton();
private Singleton(){}
public static Singleton getInstance() {returnINSTANCE; }}Copy the code
2. Hungry (using static code blocks)
public class Singleton {
private static Singleton instance;
static {
instance = new Singleton();
}
private Singleton() {}
public static Singleton getInstance() {
returninstance; }}Copy the code
Synchronize code block double check
Public class Singleton {private static volatile Singleton; privateSingleton() {}
public static Singleton getInstance() {
if (singleton == null) {
synchronized (Singleton.class) {
if(singleton == null) { singleton = new Singleton(); }}}returnsingleton; }}Copy the code
4. Static inner classes
public class Singleton {
public static class Single{
public static final Singleton INSTANCE = new Singleton();
}
public Singleton() {
}
public static Singleton getInstance() {returnSingle.INSTANCE; }}Copy the code
Advantages of the singleton pattern:
- Because the singleton pattern has only one instance in memory, it reduces the memory overhead, especially when an object needs to be created and destroyed frequently, and performance cannot be optimized during creation or destruction.
- Because the singleton mode generates only one instance, it reduces the system performance overhead. When more resources are needed to generate an object, such as reading configuration or generating other dependent objects, a singleton can be generated directly when the application is started. (note the JVM garbage collection mechanism when using the singleton pattern in JavaEE)
- The singleton mode can avoid multiple resource usage, such as a write file operation. Because only one instance exists in memory, simultaneous write operations on the same resource file are avoided
- The singleton pattern can set up global access points in the system, optimize and share resource access, for example, you can design a singleton class, responsible for all data table mapping processing
Disadvantages of the singleton pattern:
- The singleton pattern generally has no interface and is difficult to extend
- The singleton pattern is bad for testing. In a parallel development environment, you can’t test until the singleton pattern is complete, and you can’t mock an object without an interface
Application scenarios of singleton mode:
- An environment that requires the generation of a unique sequence number
- You need a shared access point or shared data, such as a counter on a Web page, throughout the project
- Creating an object consumes too many resources, such as accessing IO and database resources
- Environments that need to define a large number of static constants and static methods (such as utility classes) can use the singleton pattern (which, of course, can be declared static)