Design pattern -- single example design pattern JAVA version

Singleton design pattern

Before learning design patterns, let's first understand design patterns, which are divided into three types, a total of 23 design patterns

  1. Creation mode: Singleton mode, abstract factory mode, prototype mode, builder mode and factory mode.
  2. Structural mode: adapter mode, bridge mode, decorator mode, combination mode, appearance mode, sharing mode and agent mode.
  3. Behavioral mode: template method mode, command mode, visitor mode, iterator mode, observer mode, mediator mode, memo mode, Interpreter mode, state mode, policy mode, responsibility chain mode (responsibility chain mode).

What is singleton mode?

The so-called singleton design pattern is to take certain methods to ensure that there can only be one object instance for a class in the whole software system, and the class only provides one object instance. (static method)
Usage scenario: objects that need to be created and destroyed frequently, objects that take too much time and waste too much resources when creating objects, tool objects, and objects that frequently access databases or files (such as data sources, session factories, etc.)

Application example of hungry Han formula (static constant)

  1. Constructor Privatization (prevent object creation through NEW)
  2. Class
  3. Provide a static public method to the outside to get the object.
class Singleton{

		//1. Privatization of constructor
		private Singleton(){
		}
		
		//2. Create object instances within this class
		private final static Singleton instance = new Singleton();

		//Provide a static public method to the outside to get the object.
		public static Singleton  getInstance(){
			return instance;
		}

}

Advantages and disadvantages:

  1. Advantages: the writing method is simple, and the instantiation is completed when the class is loaded. Thread synchronization problems are avoided.
  2. Disadvantages: instantiation occurs when the class is loaded, which does not achieve the effect of lazy loading. If this instance is not used all the time, it will cause a waste of memory.
  3. Conclusion: available, but may cause memory waste.

Hungry Chinese style (static code block) application example

class Singleton{

		//1. Privatization of constructor
		private Singleton(){
		}
		
		//2. Create object instances within this class
		private static Singleton instance;

		//Creating singleton objects in static code blocks
		static{
			instance = new Singleton();
		}
		//3. Provide a static public method to get the object.
		public static Singleton  getInstance(){
			return instance;
		}

}

Advantages and disadvantages: the same as above

Lazy (thread unsafe) application example

class Singleton{

		//1. Privatization of constructor
		private Singleton(){
		}
		
		//2. Create object instances within this class
		private static Singleton instance;

		//3. Provide a static public method to get the object.
		public static Singleton  getInstance(){
			if(instance==null){
				instance = new Singleton();
			}
			return instance;
		}

}

Advantages and disadvantages:

  1. Advantages: it has the effect of lazy loading.
  2. Disadvantages: however, the thread is not safe (multiple instances are easily generated in if), and can only be used in a single thread.

Lazy (thread safe synchronization method) application example

class Singleton{

		//1. Privatization of constructor
		private Singleton(){
		}
		
		//2. Create object instances within this class
		private static Singleton instance;

		//3. Provide a static public method to get the object.
		public static synchronized Singleton  getInstance(){
			if(instance==null){
				instance = new Singleton();
			}
			return instance;
		}

}

Advantages and disadvantages:

  1. Advantages: solve thread insecurity.
  2. Disadvantages: low efficiency. Thread synchronization is required every time getInstance() is called.
  3. Conclusion: it is not recommended in practical development.

Double check application example

class Singleton{

		//1. Privatization of constructor
		private Singleton(){
		}
		
		//2. Create object instances within this class
		private static volatile Singleton instance;

		//3. Provide a static public method to get the object.
		public static Singleton  getInstance(){
			if(instance==null){
				synchronized  (Singleton.class){
					if(instance==null){
						instance = new Singleton();		
					}
				}
			}
			return instance;
		}

}

Advantages and disadvantages:

  1. Advantages: solve the problem of thread insecurity and low efficiency.
  2. Conclusion: this method is recommended.

Static internal class application instance

class Singleton{

		private Singleton(){
		}
		
		private static class  SingletonInstance{
			private static final Singleton instance = new Singleton();
		}
		public static Singleton  getInstance(){
			return SingletonInstance.instance;
		}
}

Advantages and disadvantages:

  1. Advantages: the mechanism of class loading is used to solve the problem of thread insecurity, and the static internal class solves the problem of lazy loading.
  2. Conclusion: it is recommended to use.

Enumerate application instances

enum Singleton{
		INSTANCE;
		public void funtion(){
			//casual
		}
}

//use
Singleton instance = Singleton.INSTANCE;

Advantages and disadvantages:

  1. Advantages: the singleton mode is implemented with the help of enumeration added in JDK1.5. Resolve threading issues and prevent deserialization and re creation of objects.
  2. Conclusion: it is recommended to use.

Tags: Java Design Pattern

Posted on Wed, 24 Nov 2021 09:55:41 -0500 by jonnyw6969