Design mode Singleton (single)

xiaoxiao2021-03-06  70

Synual definition: Singleton mode The main role is to ensure that a class class Class exists in a Java application.

In many operations, such a single-threaded operation is required such as establishing a directory database connection.

Also, Singleton can be status; so that multiple single classes can serve as a status warehouse as a status warehouse, for example, you want toyum's posts

Numerals, each time you browse, you need to count, the single class can keep this count, and can save the security of SYNCHRONIZE. If you want to save this number permanently to

Database, you can easily do it without modifying a single-state interface.

In addition, Singleton can also be unstable. Provide the functionality of tools,

Singleton mode provides us with this implementation. The benefits of using Singlet are also to save memory because it limits the number of instances, which is beneficial to JAV

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 is improved above, and it is not necessary to generate an object each time, it is only the first // use of an instance, and it improves 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 lazy initialization, this synchronized is very important, if there is no synchronized, use getInstance ()

It is possible to get multiple Singleton instances. There are many discussions involving Double-Checked Locking (DCL) about Lazy Initialization.

Interest in fun.

The first form is generally considered to be more secure.

Using Singleton Precautions: Sometimes in some cases, using Singleton does not reach Singleton's purpose, if you have multiple Singleton objects being loaded simultaneously; in EJB

Use in distributed systems should also pay attention to this 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 almost the content,

Both provide EJB query positioning services, but why do you want to separate? Carefully study the difference between these two ServiceLocator: ServiceLocator in the Web

Take the Singleton mode, ServiceLocator belongs to resource positioning, and you should use the Singleton mode. But in EJB, Singleton mode has lost

For use, ServiceLocator is divided into two kinds, one for web services, one is for 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.

Summary: If your application is based on the container, the Singleton mode can be used less or not, you can use a related alternative technology.

Further deeply refer to:

Double-checked Locking and The Singleton Pattern

WHEN is a singleton not a singleton?

How to design a design model See "Java Practical System Development Guide" in specific projects

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

New Post(0)