Java design pattern of the single state mode (singleton mode) Introduction _java

Source: Internet
Author: User
Tags garbage collection

Definition: The main purpose of the singleton mode is to ensure that only one instance of class classes exists in a Java application.

In many operations, such as the creation of a directory database connection requires such a single-threaded operation.

Also, Singleton can be stateful; In this way, multiple single state classes can be used as a state warehouse to provide services outside, for example, you want the forum post counters, each browsing once need to count, the single state can keep the count, and can synchronize security automatically add 1 , if you want to permanently save this number to the database, you can do it conveniently without modifying the interface of a single state.

In addition, Singleton can also be stateless. Provides functionality for tool nature,

The singleton model provides us with the possibility of this implementation. The benefit of using Singleton is that it can save memory because it limits the number of instances and facilitates Java garbage collection (garbage collection).

We often see that the class loader in the factory pattern is also implemented in the singleton mode, because the loaded class actually belongs to the resource as well.

How to use a single case pattern

General singleton patterns usually have several forms:

Copy Code code as follows:

public class Singleton {
Private Singleton () {}
Is 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 ();
This provides a static method for external access to this class, which can be accessed directly from the
public static Singleton getinstance () {
return instance;
}
}

Second form:
Copy Code code as follows:

public class Singleton {
private static Singleton instance = NULL;
public static synchronized Singleton getinstance () {
This method is better than the previous one, without having to generate objects every time, just for the first time
Generate an instance when using, improve efficiency!
if (instance==null)
Instance=new Singleton ();
return instance;
}
}

Use Singleton.getinstance () to access a single State class.

The second form above is lazy initialization, which means that the initial singleton of the first call is not to be generated anymore.

Note that the synchronized in the lazy initialization form is important, and if there is no synchronized, then it is possible to get multiple getinstance instances with Singleton (). There are many discussions on the singleton of lazy initialization, which involve double-checked locking (DCL), which are further studied by interested persons.

The first form is generally considered to be more secure.

Using a single case pattern considerations

Sometimes in some cases, the use of Singleton does not achieve the purpose of Singleton, such as having multiple singleton objects loaded at the same time by different class loaders, and this is also noted in distributed systems such as EJBS, because EJBs are cross-server and JVM-wide.

We take a look at Sun's pet store source code (pet store 1.3.1) Servicelocator for a little analysis:

There are two kinds of servicelocator in pet store, one is under the EJB directory; one is the web directory, we check these two servicelocator will find the content is similar to provide the EJB query location services, but why to separate? Careful study of the two servicelocator to discover the difference: Servicelocator in the web to take the singleton mode, servicelocator belong to the resource positioning, it is natural to use the singleton mode. But in EJB, the singleton mode has lost its effect, so servicelocator is divided into two kinds, one for Web services and one for EJB service.

The

Singleton mode looks simple, easy to use, but really good to use, it's not easy, it needs to be quite familiar with the concepts of Java's class thread memory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.