[design mode] - explain the multiple implementation methods of single case mode

Singleton mode

 

Singleton Pattern is one of the simplest design patterns in Java. This type of design pattern is a creation pattern, which provides the best way to create objects.

 

This pattern involves a single class that is responsible for creating its own objects, while ensuring that only a single object is created. This class provides a way to access its unique object, which can be accessed directly without instantiating the object of this class.

Be careful:

  • 1. A singleton class can only have one instance.
  • 2. A singleton class must create its own unique instance.
  • 3. The singleton class must provide this instance to all other objects.

 

1. Lazy, unsafe thread

 

/**
* This is the most basic way of implementation. The biggest problem of this implementation is that it does not support multithreading. Because there is no lock synchronized, it is not strictly a singleton mode.
* In this way, lazy loading is very obvious. It does not require thread safety and cannot work normally in multithreading.
*/
public
class Singleton { private static Singleton instance;
//Let the constructor be private,So that the class is not instantiated
private Singleton (){} public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }

 

2. Lazy and thread safe

/**
* This method has a good lazy loading, which can work well in multithreading. However, the efficiency is very low, 99% of the cases do not need synchronization.
* Advantage: the first call is initialized to avoid memory waste.
* Disadvantages: you must lock synchronized to ensure single instance, but locking will affect efficiency.
*/
public
class Singleton { private static Singleton instance;
//Let the constructor be private,So that the class is not instantiated
private Singleton (){} public static synchronized Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }

3. Hungry Chinese style

/**
* This method is quite common, but it is easy to generate garbage objects.
* Advantage: without lock, the execution efficiency will be improved.
* Disadvantages: class is initialized when it is loaded, which wastes memory.
*/
public
class Singleton { private static Singleton instance = new Singleton();
//Let the constructor be private,So that the class is not instantiated
private Singleton (){} public static Singleton getInstance() { return instance; } }

4. Double check lock / double check lock

/**
* This method adopts double lock mechanism, which is safe and can maintain high performance in the case of multithreading.
*/
public
class Singleton { private volatile static Singleton singleton;
//Let the constructor be private,So that the class is not instantiated
private Singleton (){} public static Singleton getSingleton() { if (singleton == null) { synchronized (Singleton.class) { if (singleton == null) { singleton = new Singleton(); } } } return singleton; } }

5. Enumeration

/**
* This implementation method has not been widely used, but it is the best way to implement the singleton pattern. It is more concise, automatically supports serialization mechanism, and absolutely prevents multiple instantiations.
* This method is advocated by Josh Bloch, author of Effective Java. It can not only avoid multithreading synchronization problems, but also automatically support serialization mechanism to prevent deserialization from re creating new ones.
* Object to prevent multiple instances.
*/
public
enum Singleton { INSTANCE; public void whateverMethod() { } }

Experience: generally, the first and the second lazy way are not recommended, and the third hungry way is recommended. If deserialization is involved in creating objects, try the fifth enumeration. If there are other special requirements, you can consider using the fourth double check lock mode.

Reference source: Rookie tutorial single example mode

Tags: PHP Java

Posted on Sat, 02 Nov 2019 14:55:21 -0700 by mclordgt