Singleton pattern analysis of Java design patterns

  • 2020-04-01 04:21:39
  • OfStack

This article illustrates a singleton pattern of Java design patterns. Share with you for your reference, as follows:

The Singleton Pattern (Singleton Pattern) is a relatively simple Pattern defined as follows:

Ensure a class has only one instance, and provide a global point of access to it.

Singleton pattern, a very simple pattern. In fact, in android development, the singleton pattern is used in many places, such as some tool classes, Json data parsing classes, local database operation classes, and so on.
Singleton pattern general code:


public class Singleton { 
  private static final Singleton singleton = new Singleton(); 
  //Restrict the generation of multiple objects
  private Singleton() { 
  } 
  //Get the instance object by changing the method
  public static Singleton getInstance() { 
    return singleton; 
  } 
  //Other methods in the class are as static as possible
  public static void doSomething() { 
  } 
}

I hope this article has been helpful to you in Java programming.


Related articles: