Java---Design Patterns---Singleton

Solution: It is guaranteed that a class can only have one object in memory.

Ideas:

    1. If other programs can use new to create objects of this type at will, they cannot control the number. Therefore, do not let other programs use new to create objects of this class.
    2. Since other programs are not allowed to new objects of this class, the class must create an object within itself, otherwise the class will never be able to create an object.

    3. This class provides the created objects to the outside world (the entire system) for other programs to acquire and use.

step:

    1. Make the constructor in the class private.
    2. Create an object of this class in this class.
    3. Define a method whose return value type is this class type. Let other programs get the object of this class through this method.

Hungry Chinese:

/**
 * Time:2018/4/13
 * Description: Singleton---Hungry mode
 * @author Song Jinyu
 */
public class HungryManModel {
	// Load the instance when the class template is loaded.
	private static final HungryManModel HUNGRY_MAN_MODEL = new HungryManModel();
	//Private the constructor to implement a singleton
	private HungryManModel(){
	}
	//get instance
	public static HungryManModel getInstance(){
		return HUNGRY_MAN_MODEL;
	}
}

Lazy Man:

/**
 * Time:2018/4/13
 * Description: singleton---lazy mode
 * @author Song Jinyu
 */
public class LazyManModel {
	//The instance is not loaded when the class template is loaded, and when the get instance method is called, the object is instantiated
	private static LazyManModel lazyManModel = null;
	//Private the constructor for singleton implementation
	private LazyManModel(){
	}
	//Provide a method to get the instance
	//★★★ In the case of multiple threads, the synchronized keyword needs to be used to lock the method.
	public synchronized static LazyManModel getInstance() {
		//If it is the first time to call the get instance method, create an object, otherwise return lazyManModel directly
		if (lazyManModel==null) {
			lazyManModel = new LazyManModel();
		}
		return lazyManModel;
	}
}

Singleton deformation---multiple cases

Unlimited number of "singleton + cache":

    Cache is used frequently in programming and plays a very important role. It can help programs to exchange space for time. It is usually designed as a space shared by the entire application. Now it is required to implement a class that uses cache to store singleton objects.
    Note: The cache can store multiple objects of this type, each object is identified by a key value, and the same singleton object is accessed when the key value is the same.

import java.util.HashMap;
import java.util.Map;
/**
 * Time:2018/4/14
 * Description: multiple cases---unlimited number
 * @author Song Jinyu
 */
public class MultiCase {
	//As soon as the class template is loaded, a map container is created
	private static Map<String, MultiCase> multiCases = new HashMap<String, MultiCase>();
	//Look at the project requirements, whether it is necessary to seal the constructor, this example uses the sealed constructor
	private MultiCase(){
	}
	//Provide an object instance that can be obtained by key
	public static MultiCase getInstanceByKey(String key) {
		//First get a value from the container through the key
		synchronized (key) {
			MultiCase value = multiCases.get(key);
			//If it is null, it means that the object has not been instantiated, that is, the first time to obtain the instance through this key value
			if (value==null) {
				value = new MultiCase();
				multiCases.put(key, value);
			}
			return value;
		}
	}

}

Limit the number of "singleton + cache + control instance number":

    The singleton implementation of the above cache is made into a shared space that can control the number of objects for the entire application to use. A specified number of objects are maintained in the cache. The key value of each object is specified internally by the class, and one of the objects is directly returned when there is an external request.
    Description: It is equivalent to maintaining a specified number of object pools. When the number of requests exceeds the total number of controls, it starts to be used repeatedly.

import java.util.HashMap;
import java.util.Map;
/**
 * Time:2018/4/14
 * Description: multiple cases---unlimited number
 * @author Song Jinyu
 */
public class MultiCase2 {
	//As soon as the class template is loaded, a map container is created
	private static Map<Integer, MultiCase2> multiCase2s = new HashMap<Integer, MultiCase2>();
	//Set the maximum number of instances in the container
	private final static int MAX = 3;
	private static int num = 0;//Number of used
	// block the constructor
	private MultiCase2(){
	}
	//Provide a method that can get an object instance externally
	public static MultiCase2 getInstance(){
		MultiCase2 value = multiCase2s.get(num);
		if (value==null) {
			value = new MultiCase2();
			multiCase2s.put(num, value);
		}
		num++;
		if (num>MAX) {
			//Control according to the project, in this example, clear 0 directly
			num = 0;
		}
		return value;
	}
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325386126&siteId=291194637