An introduction to the Singleton pattern of Java design patterns

  • 2020-04-01 03:41:20
  • OfStack

Definition: the Singleton pattern is primarily used to ensure that only one instance of a Class Class exists in a Java application.

Many operations, such as establishing a directory database connection, require such single-threaded operations.

Also, singletons can be stateful; In this way, multiple singleton class together could as to serve as a unique repository of state, for example, you want to the counter in the BBS posts, I visit a need to count, singleton class can maintain the count, and the safety of the synchronize automatically add 1, if you take the number permanent saved to the database, you can not change under the condition of singleton interface convenient to do so.

On the other hand, singletons can also be stateless. Provide tool-like functionality,

The Singleton pattern provides us with the possibility to do just that. The benefit of using singletons is also that they save memory because they limit the number of instances, which is good for Java garbage collection.

We often see class loaders in the factory pattern implemented with the Singleton pattern as well, because the classes that are loaded actually belong to the resource as well.

How to use the singleton pattern

The general Singleton pattern typically takes several forms:


public class Singleton {
    private Singleton(){}
 //Isn't it strange to define an instance of yourself within yourself? < br / >  //Notice that this is private for internal calls only
 private static Singleton instance = new Singleton();
 //A static method is provided for external access to this class, which can be accessed directly by
 public static Singleton getInstance() {
  return instance;   
 }
}

The second form:

public class Singleton {
 private static Singleton instance = null;
 public static synchronized Singleton getInstance() {
     //This method is an improvement over the previous one, instead of generating the object every time, just the first time
     //When used to generate instances, improve efficiency! < br / >      if (instance==null)
      instance = new Singleton();
  return instance;
    }
}

Singleton.getinstance () is used to access the Singleton class.

The second form above is lazy initialization, which means that the initial Singleton on the first call does not have to be regenerated later.

Note the synchronization in lazy initialization form, which is important, and if there is no synchronization, it is possible to get multiple Singleton instances using getInstance(). There has been a lot of discussion about double-checked locking (DCL) for lazy initialization singletons and those interested in exploring it further.

The first form is generally considered to be safer.

Considerations for using the singleton pattern

Sometimes a Singleton does not serve the purpose of a Singleton, such as having multiple Singleton objects loaded by different class loaders at the same time. It is also important to note this when using ejbs in distributed systems such as ejbs, because ejbs are cross-server, cross-jvm.

Let's take SUN's Pet Store source code (Pet Store 1.3.1) ServiceLocator as an example for a little analysis:

There are two kinds of servicelocators in the Pet Store, one is under the EJB directory; One is under the WEB directory. If we examine the two servicelocators, we will find that they are similar in content. They both provide query location services for ejbs, but why separate them? A closer look at the two servicelocators reveals the difference: the ServiceLocator in the WEB takes the Singleton pattern, and the ServiceLocator is a resource locator, so the Singleton pattern should be used. But in ejbs, the Singleton pattern has lost its usefulness, so the ServiceLocator is split into two types, one oriented to WEB services and one oriented to EJB services.

The Singleton pattern may seem simple and easy to use, but to really use it well is not easy and requires a fair understanding of Java concepts such as class thread memory.


Related articles: