Java design pattern learning (1): singleton mode

This article begins to learn design patterns.

  • Design Patterns

    First explain what is a design pattern?

A design pattern is a set of code design experience that has been used repeatedly and is known to most people.
The use of design patterns is to reusable code, make it easier for others to understand, ensure code reliability, and program reusability.

Personal understanding is a relatively standard solution to the general problems encountered in the software development process. It is the experience gained by the predecessors in the long-term trial and error.

Hope to learn one by one in the future. This article starts with singleton mode.

  • Singleton mode

    Definition of singleton mode:

Singleton Pattern (Singleton Pattern) is one of the simplest design patterns in Java. It provides an optimal way to create objects. This pattern involves a single class that is responsible for creating its own objects while ensuring that only a single object is created. This class provides a way to access its unique object, which can be accessed directly without instantiating objects of this class.

There are three points to note in this definition:

  1. A singleton class can only have one instance.
  2. The singleton class must create its own unique instance.
  3. The singleton class must provide this instance to all other objects.
  • Method to realize

Singleton mode is generally divided into lazy man and hungry man. Described below separately.

  1. Hungry Chinese style: One of the more commonly used methods is to initialize the class when the class is loaded. It is easy to use and wastes memory. This is a thread-safe, multi-threaded, and efficient way.
public class Singleton {
    
      
    private static Singleton instance = new Singleton();  
    private Singleton (){
    
    }  
    
    public static Singleton getInstance() {
    
      
    	return instance;  
    }  
}
  1. Lazy style: As the name implies, it is loaded when used and initialized.
    The following is the basic implementation, which is a lazy loading method that is not thread-safe and does not support multi-threading.
public class Singleton {
    
      
    private static Singleton instance;  
    private Singleton (){
    
    }  
  
    public static Singleton getInstance() {
    
      
	    if (instance == null) {
    
      
	        instance = new Singleton();  
	    }  
	    	return instance;  
	    }  
}

In this way, if multiple threads call the getInstance() method at the same time, after entering the if, multiple threads will create multiple Singletons.

In order to solve the problem of thread insecurity and not supporting multithreading, we can deal with it by locking, and then there is the following method.

public class Singleton {
    
      
    private static Singleton instance;  
    private Singleton (){
    
    } 
     
    public static synchronized Singleton getInstance() {
    
      
	    if (instance == null) {
    
      
	        instance = new Singleton();  
	    }  
	    	return instance;  
	    }  
}

A lock is added to the method name, in this case, the above problem can be solved perfectly!

However, at the same time new problems have arisen. This operation will lead to very low efficiency. Lock every time. In order to solve this problem, a new solution has appeared.

That is, DCL (double checked locking) double lock check.

This method uses a double lock check mechanism to ensure delayed loading, safety, and high performance in multi-threaded situations.

public class Singleton {
    
      
    private volatile static Singleton singleton;  
    private Singleton (){
    
    }  
    
    public static Singleton getSingleton() {
    
      
	    if (singleton == null) {
    
      
	        synchronized (Singleton.class) {
    
      
		        if (singleton == null) {
    
      
		            singleton = new Singleton();  
		        }  
	        }  
	    }  
	    return singleton;  
    }  
}

In addition to these, the enumeration method is more recommended.

Basically that's about singleton.

reference

Singleton mode fully explains
several implementation methods of singleton mode

Guess you like

Origin blog.csdn.net/A_Intelligence/article/details/109453296