Five ways to implement Java's singleton single-case pattern

Source: Internet
Author: User

In the interview often asked the Java singleton model, the problem can be very good to examine the candidate's understanding of knowledge points. Singleton mode requires that only one instance object exists when the system is running.

Let's take a detailed look at the key points of knowledge, and introduce five implementations, along with their pros and cons.

The simplest method is to initialize the individual instance when the class is loaded.

first , define a singleton class (no special, just a name):

1  Public class singleton{

second , you need to define class variables to save the singleton objects:

1 Private Static New Singleton ();

Here are two points to note:

    1. Private: The variable cannot be accessed elsewhere except within the Singleton class;

    2. Static: Make sure it is a statically class variable, so it can be accessed by the class name without an instance;

again , you need to explicitly define the constructor and set it to private so that the compiler will give an error if new is outside the Singleton class, even if it is a subclass of Singleton:

1 Private Singleton () {}

finally , we define the acquisition method of the Singleton object,

1  Public Static Singleton getinstance () {2     return instance; 3 }

It is also important to note:

    1. Public: This can be called externally;

    2. Static: This way the outside can be accessed by the class name, otherwise the singleton object is not obtained;

second, use lazy loading. on the basis of method one, we need to modify several code:

first , the static class variable should be initialized to null:

1 Private Static Singleton instance = NULL;

second , the getinstance () method needs to assume the task of generating the instance:

1  Public Static Singleton getinstance () {2     if (Instance = = NULL) 3         New Singleton (); 4     return instance; 5 }

third, consider the problem of multithreading. method Two is still possible to produce multiple instances in multi-threaded situations, we need to use synchronization to avoid multithreading problems. as follows, use the Synchronized keyword before the getinstance () method:

1  Public Static synchronized Singleton getinstance () {

Iv. consider performance issues. the Synchronized keyword decorates the getinstance () method, which causes all threads calling the GetInstance () method to compete for the same lock, even if the singleton object has been generated well. Here's how double check is used:

1  Public StaticSingleton getinstance () {2     if(Instance = =NULL) {3         synchronized(Singleton.class) {4         if(Instance = =NULL)5Instance =NewSingleton ();6         }7     }8     returninstance;9}

v. A more streamlined approach. based on the characteristics of the static inner class, we can use its lazy loading mechanism to simplify the implementation:

1  Public Static Singleton getinstance () {2     return nestedclass.instance; 3 }45privatestaticclass  nestedclass{ 6     Private Static New Singleton (); 7 }

Five ways to implement Java Singleton Singleton mode

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.