Definition: Singleton mode The main role is to ensure that only one instance of a class Class is only in the Java application.
In many operations, such a single-threaded operation is required such as establishing a directory database connection.
Also, Singleton can be statically customized; so that multiple single classes can serve as a status warehouse as a status repository, for example, you want to discus the post counter, each time you browse a count, single class Can you keep this count, and you can use Synchronize's security to add 1. If you want to save this number permanently to the database, you can easily do it without modifying a single-state interface.
In addition, Singleton can also be unstable. The function of providing tools is provided, and Singleton mode provides us with this implementation. The benefits of using Singlet are also saving memory because it limits the number of instances, which is conducive to the Garbage Collection. We often see that in the factory model, the Class Loader is also implemented in the Singleton mode because the collateral is actually a resource.
How to use? General Singleton mode usually has several forms:
Public class singleton {private singleton ()} // define one instance in itself, is it very strange? / Note This is private only for internal calling private static singleton instance = new singleton (); // here provides a static method for external access to this Class, you can directly access public static singleton getInstance () {return instance;}}
The second form:
Public class singleton {private static singleleton instance = null; public static synchronized singleton getInstance () {// This method has been improved above, and does not have to generate an object each time, it is only the first time // uses instances, improved effectiveness! IF (instance == null) instance = new singleleton (); return instance;}}
Use Singleton.GetInstance () to access the single class.
The second middle form of the above is Lazy Initialization, that is, the initial Singleton when calling, will not be regenerated in the future.
Note that synchronized in the form of lazy initialization, this synchronized is important, if there is no synchronized, use getInstance () is possible to get multiple Singleton instances. There are many discussions involving Double-Checked Locking (DCL) about Lazy Initialization. It is interest in research.
The first form is generally considered to be more secure.
Precautions using Singleton: Sometimes in some cases, using Singleton does not reach the purpose of Singleton, if there are multiple Singleton objects being loaded simultaneously; in distributed systems such as EJB, pay attention to this The situation, because EJB is a cross server, cross JVM.
We have a slight analysis as a SERVICELOCATOR for Sun's pet store source (PET Store 1.3.1): There are two kinds in the PET Store, one is the ejb directory; one is the Web directory, we check these two serviceLocator Will find that the content is similar, all provide EJB query positioning services, but why do you want to separate? Carefully study the difference between these two ServiceLocator: Singleton mode for ServiceLocator in the Web, ServiceLocator belongs to resource positioning, and it should take the Singleton mode. However, in EJB, the Singleton mode has been lost, so ServiceLocator is divided into two, one is a web service, one is to serve EJB services. Singleton mode looks simple, the method is also very convenient, but it is really easy to use, it is very difficult, you need to have a considerable understanding of the concept of Java's class thread memory.