Definition: The main function of the Singleton pattern is to ensure that in Java applications, only one instance of a class exists.
In many operations, such as establishing a directory database connection, such a single-threaded operation is required.
Also, singleton can be state-ized; in this way, multiple singleton classes can be provided to the outside as a state repository. For example, if you want a post counter in the forum, you need to count each time you browse it. Singleton classes Can you keep this count and automatically increase 1 by synchronize safely. If you want to save this number to the database permanently, you can easily do it without modifying the single-state interface.
In addition, Singleton can also be stateless. Provides tool-like functions,
The Singleton model provides us with the possibility of implementing this way. The advantage of using Singleton is that it can save memory, because it limits the number of instances and is conducive to Java garbage collection.
We often see that class loaders are also implemented in Singleton mode in factory mode, because the loaded class actually belongs to resources.
How to use singleton mode
The general Singleton pattern usually has several forms:
The code copy is as follows:
public class Singleton {
private Singleton(){}
//Isn’t it strange to define an instance of yourself inside yourself?
//Note that this is private only for internal calls
private static Singleton instance = new Singleton();
// Here is a static method for external access to this class, which can be accessed directly
public static Singleton getInstance() {
return instance;
}
}
The second form:
The code copy is as follows:
public class Singleton {
private static Singleton instance = null;
public static synchronized Singleton getInstance() {
//This method is improved over the above. You don't need to generate objects every time, it's just the first time
//Create instances during use, improving efficiency!
if (instance==null)
instance=new Singleton();
return instance;
}
}
Singleton.getInstance() can be used to access singleton classes.
The second form above is lazy initialization, which means that the initial Singleton is called for the first time and there is no need to be generated in the future.
Note that synchronized in the lazy initialization form is important. If there is no synchronized, it is possible to get multiple Singleton instances using getInstance(). There are many discussions about lazy initialization about double-checked locking (DCL), and those interested in it will further study it.
It is generally believed that the first form is safer.
Things to note when using singleton mode
Sometimes in some cases, using Singleton cannot achieve the purpose of Singleton. If multiple Singleton objects are loaded by different class loaders at the same time; this situation should be paid attention to when used in distributed systems such as EJB, because EJB It is cross-server and cross-JVM.
Let’s take the ServiceLocator of SUN’s pet store source code (Pet Store 1.3.1) as an example and analyze it briefly:
In the Pet Store, there are two types of ServiceLocator, one is in the EJB directory and the other is in the WEB directory. When we check these two ServiceLocators, we will find that the content is similar, and they both provide EJB query and positioning services, but why separate them? After careful study of these two ServiceLocators, I found the difference: ServiceLocator adopts the Singleton mode in WEB, ServiceLocator belongs to resource positioning, so of course, the Singleton mode should be used. However, in EJB, the Singleton model has lost its function, so ServiceLocator is divided into two types, one is for WEB services and the other is for EJB services.
The Singleton pattern looks simple and is very convenient to use, but it is very difficult to use it well. You need to have a considerable understanding of Java class thread memory and other concepts.