Design mode Singleton (single)

xiaoxiao2021-04-05  318

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 kindleton () {}

// Is it very strange to define an instance inside yourself? // Note this is Private only for internal calls

Private static singleleton instance = new singleleton ();

/ / Here is a static method for accessing the Class of 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, do not have to generate an object each time, only the first // uses an instance, improve the efficiency! 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.

Further deeply refer to:

Double-checked Locking and The Singleton Pattern

WHEN is a singleton not a singleton?

转载请注明原文地址:https://www.9cbs.com/read-131993.html

New Post(0)