24-Single case design pattern

Singleton design pattern

The single-case design mode (multiple-case design mode) is mainly a design operation that controls the number of instantiated objects.

Singleton design

Due to certain requirements, only certain classes are required to provide an instantiated object. First, you should control the construction method, because all instantiated objects must be generated to call the construction method. If the construction method is "nothing", then naturally it cannot be generated. Instantiate the object.
If the constructor is privatized, the instantiated object cannot be initialized.
So we must find a way to generate an instantiated object for use.
1. Private access permissions: no external access but can be used inside the class.
2. Consider static attributes at this time
3. Attributes in the class should be used after encapsulation
4. Ensure

class Singleton{
    
    		//单例设计结构
	private static final Singleton INSTANCE = new Singleton();
	private Singleton();
	public static Singleton getInstance(){
    
    
		return INSTANCE;
	}
}

Some classes do not need to repeatedly generate objects.
Singleton design pattern: lazy man, hungry man. The previous definition belongs to the hungry style (the class object is automatically provided when the system is loaded) and the lazy style (the object is instantiated when it is used for the first time).
Design the singleton as a lazy man:

class Singleton{
    
    		//单例设计结构
	private static Singleton instance;
	private Singleton() {
    
    };
	public static Singleton getInstance(){
    
    
		if (instance == null){
    
    		//第一次使用
			instance = new Singleton();		//实例化对象
		}
		return INSTANCE;
	}
}

Interview: Write a Signleton program to illustrate the characteristics?

  • Lazy guy (considering thread synchronization issues) and bad guys write
  • Features: The construction method is privatized, and the class provides static methods to obtain instantiated objects. Regardless of external operations, there is always only one instantiated object.

Multi-case design

Singleton design is to keep only one instantiated object, and multi-case design refers to keeping multiple instantiated objects. For example, if a class describing gender is defined, there are only two objects (male and female).

class Color{
    
    
	private static final Color RED = new Color("red");
	private static final Color GREEN = new Color("Gre");
	private static final Color BLUE = new Color("Blu");

	private Color(String title){
    
    		//构造方法私有化
		this.title = title;
	}
	public static Color getInstance(String color){
    
    
		switch(color){
    
    
			case "red": return RED;
			case "Gre": return GREEN;
			case "Blu": return BLUE;
			default:return null;
		}
	}
	public String toString(){
    
    
		return this.title;
	}
}

Multi-case design and single-case design are essentially the same, and static methods must be provided internally to return instantiated objects.

Guess you like

Origin blog.csdn.net/MARVEL_3000/article/details/111400752