(Java) singleton mode (hungry singleton mode vs lazy singleton mode)

What is a design pattern?

Design pattern is the code structure, programming style and thinking way to solve problems after summarizing and theorizing in a large number of practice. The design model eliminates our own thinking and exploration. The design model is like a classic chess score. In different chess games, we use different chess scores and "routines".

Classification of design patterns:
① Creation mode (5 kinds): factory method mode, abstract factory mode, singleton mode, builder mode and prototype mode.
② Structural mode (7): adapter mode, decorator mode, agent mode, appearance mode, bridge mode, combination mode and sharing mode.
③ Behavioral modes (11): strategy mode, template method mode, observer mode, iteration sub mode, responsibility chain mode, command mode, memo mode, status mode, visitor mode, mediator mode and interpreter mode,

1, What is singleton mode?

Usage Summary of singleton mode

Singleton design pattern: singleton design pattern is to ensure that there is absolutely only one instance of a class in any case and provide a global access point.

The model has three basic points:
① This class can only have one instance;
② You must create this instance yourself;
③ Provide public static methods to return internally created objects.

1. Hungry Han single instance mode

Advantages: no lock, high execution efficiency, and better user experience than lazy singleton mode
Disadvantages: class is initialized when it is loaded, which takes up memory space whether it is used or not
Recommendations:
It is suitable for scenarios with few singleton modes
If we will load the class after the program starts, the singleton implemented in hungry man mode is simple and practical;
If we are writing some tool classes, we give priority to the lazy mode, which can avoid being loaded into memory in advance and occupying system resources.

/**
 * Description: hungry Han style singleton mode
 * 
 * @date 2021 November 11, 2012 12:53:45 PM
 *
 */
public class SingletonTest {
	public static void main(String[] args) {
		Account account01 = Account.getInstance();
		Account account02 = Account.getInstance();
		System.out.println("account01 Your address is:"+account01);	//com.jd.exer.Account@7de26db8
		System.out.println("account02 Your address is:"+account02);	//com.jd.exer.Account@7de26db8
		System.out.println(account01==account02);			//The address value is the same: true
	}
}
//Hungry Han single case mode
class Account{
	//1. Constructor of privatization class
	private Account() {
		
	}
	//2. Create private static objects of internal classes
	private static Account instance= new Account();
	//3. Provide a public static method to return the created object
	public static Account getInstance() {
		return instance;
	}
}

Operation results:

2. Lazy singleton mode - double check lock

Compared with the single lock, the performance of the double check lock is improved, but the synchronized keyword is still used. In general, the lock must be locked. There is still a certain performance impact on the program performance, which is not optimal - > there is room for optimization
Recommendations:
If we will load the class after the program starts, the singleton implemented in hungry man mode is simple and practical;
If we are writing some tool classes, we give priority to the lazy mode, which can avoid being loaded into memory in advance and occupying system resources.

/**
 * Description: lazy singleton mode - double check lock
 * 
 * @date 2021 November 11, 2012 12:53:45 PM
 *
 */
public class SingletonTest01 {
	public static void main(String[] args) {
		Order order01 = Order.getInstance();
		Order order02 = Order.getInstance();
		System.out.println("account01 Your address is:"+order01);
		System.out.println("account02 Your address is:"+order02);
		System.out.println(order01==order02);	//The address value is the same: true	
	}
}
//Lazy singleton mode - double check lock
class Order{
	//1. Constructor of privatization class
	private Order() {
		
	}
	//2. Create private static objects of internal classes
	private static Order instance = null;
	//3. Declare the uninitialized current class object, provide public static methods, and return the created object
	public static Order getInstance() {
		if(instance == null) {
			instance = new Order();
		}
		return instance;
	}
}

Operation results:

Tags: Java Design Pattern Singleton pattern

Posted on Thu, 11 Nov 2021 04:20:35 -0500 by richrock