singleton pattern

Singleton pattern is a commonly used software design pattern. In its core structure it contains only a special class called a singleton. The singleton pattern can ensure that in the system, a class that applies this pattern has only one instance of a class. That is, a class has only one object instance.

gist

Obviously there are three main points of the singleton pattern: one is that a class can only have one instance; the other is that it must create this instance by itself; the third is that it must provide this instance to the entire system by itself.
From the perspective of specific implementation, it is the following three points: firstly, the singleton pattern class only provides a private constructor; secondly, the class definition contains a static private object of the class; thirdly, the class provides a static public The function is used to create or get its own static private object.

Advantages and disadvantages

 

Advantage
1. The instance-controlled
singleton pattern prevents other objects from instantiating their own copies of the singleton object, thus ensuring that all objects have access to a unique instance.
Second, flexibility
Because the class controls the instantiation process, the class can flexibly change the instantiation process.
Disadvantage
1. Although the amount of overhead
is small, if you have to check whether an instance of the class exists every time an object requests a reference, there will still be some overhead. This problem can be solved by using static initialization.
Second, possible development confusion
When using singleton objects (especially objects defined in class libraries), developers must remember that they cannot use the new keyword to instantiate objects. Because library source code may not be accessible, application developers may unexpectedly find themselves unable to instantiate this class directly.
Third, the object lifetime
does not solve the problem of deleting a single object. In languages ​​that provide memory management (such as those based on the .NET Framework), only a singleton class can cause an instance to be deallocated because it contains a private reference to the instance. In some languages ​​(such as C++), other classes can delete object instances, but this can lead to dangling references in singleton classes.

 

Design ideas

①Private the constructor to ensure that objects cannot be created using the new keyword.
②Instantiate an object inside the class and return it through a static method.

 

Hungry singleton pattern

* Disadvantage: When the class is loaded, the instantiation occupies system resources in advance
 * Advantage: thread safety

 

   class Singleton{
	private Singleton() {
		System.out.println("Singleton mode");
	}

	 * Disadvantage: When the class is loaded, the instantiation occupies system resources in advance
	 * Advantage: thread safety
	private static Singleton singleton=new Singleton();
	public static Singleton getSingleton() {
			return singleton;
	}

   Lazy singleton pattern

      * Advantages: Solve the problem that the hungry Chinese style occupies system resources in advance when the class is loaded
	 * Disadvantage: not thread safe      
	 * Use synchronized synchronization blocks to lock methods to ensure lazy singletons and thread safety
	 * Disadvantage: The method needs to be locked every time, and there is only one thread at a time, which is inefficient
	 * synchronized locks a method or code block, allowing only one thread to access at the same time.
    class Singleton{
	 private Singleton() {
		System.out.println("Singleton mode");
	    }
	 * Advantages: Solve the problem that the hungry Chinese style occupies system resources in advance when the class is loaded
	 * Disadvantage: not thread safe      
	 * Use synchronized synchronization blocks to lock methods to ensure lazy singletons and thread safety
	 * Disadvantage: The method needs to be locked every time, and there is only one thread at a time, which is inefficient
	 * synchronized locks a method or code block, allowing only one thread to access at the same time.
		private static Singleton singleton=null;
	public static synchronized Singleton getSingleton() {
			if (singleton==null) {
				singleton=new Singleton();
				}	
	 			   return singleton;
}

  

 Double Locked Lazy Mode

  * Thread lock is performed only when the first singleton is null
  * When the subsequent singleton part is null, there is no need for thread lock, allowing multiple threads to take the singleton at the same time

    class Singleton{
	 private Singleton() {
		System.out.println("Singleton mode");
	    }	
	private static Singleton singleton=null;
	public static  Singleton getSingleton() {
		if (singleton==null) {
			synchronized(Singleton.class){
				if (singleton==null) {
					singleton=new Singleton();
				}
			}
		}
	                             return singleton;
}        

  

 Static inner class implements singleton

   * Solved the problem of hungry man-style occupying resources in advance and lazy man-style thread insecurity
class Singleton{
	 private Singleton() {
		System.out.println("Singleton mode");
	    }
      * Solved the problem of hungry man-style occupying resources in advance and lazy man-style thread insecurity
      class Singleton{
	     private Singleton() {
		  System.out.println("Singleton mode");
	      }
	     private static class SSingleton{
		  private static Singleton singleton=new Singleton();
	    }
  	
	public static Singleton getSingleton() {
		return SSingleton.singleton;
	}

  

 

Guess you like

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