Java-based Singleton Mode

Source: Internet
Author: User

Why the singleton mode is required:

First, let's clarify a problem, that is, in some cases, we only need one object,

For example, a computer can be connected to several printers, but only one printer on this computer can be,

The Singleton mode can be used to prevent two print jobs from being output to the printer at the same time,

That is, during the entire printing process, I only have one printer instance.

 

To put it simply, the singleton mode (also called Singleton mode) is used to ensure that during the entire application lifecycle,

At any time point, only one single-instance exists (or not ).

 

Singleton class (lazy)

Namespace Singleton
{
Public class Singleton
{
// Define a Private Static global variable to save the unique instance of this class
Private Static Singleton;

/// <Summary>
/// The constructor must be private.
/// In this way, new cannot be used externally to create instances of this class.
/// </Summary>
Private Singleton ()
{
}

/// <Summary>
/// Define a Global Access Point
/// Set it to a static method
/// This method can be called without instantiation outside the class
/// </Summary>
/// <Returns> </returns>
Public static Singleton getinstance ()
{
// It can be instantiated only once.
// It is instantiated during the first call.
// Future calls will not be instantiated
If (Singleton = NULL)
{
Singleton = new Singleton ();
}
Return Singleton;
}
}
}

Test code:

Class singletontest
{
Public static void main (string [] ARGs)
{
Singleton. Singleton singletonone = singleton. Singleton. getinstance ();
Singleton. Singleton singletontwo = singleton. Singleton. getinstance ();

If (singletonone. Equals (singletontwo ))
{
Console. writeline ("singletonone and singletontwo represent the same instance ");
}
Else
{
Console. writeline ("singletonone and singletontwo represent different instances ");
}

Console. readkey ();
}
}

As shown in the preceding results, although I have accessed getinstance () twice, I only access the same instance,

In other words, since the constructor is set to private in the above Code,

Therefore, you cannot use new to instantiate an instance outside the singleton class. You can only access the singleton class by accessing getinstance,

Getinstance () ensures that only one Singleton instance exists in the following way:

First, this Singleton class will create an instance when getinstance () is called for the first time, and encapsulate the instance reference in its own class,

Then, when getinstance () is called, the system will determine whether the singleton instance exists. If so, the instance will not be created.

Instead, the instance of the previously generated class is called. In this way, only one instance exists in the entire application.

Here we will summarize the features of the singleton mode:

First, the singleton mode enables the class to have only one instance at any time of the program lifecycle,

Then, the single-instance constructor is private. If an external program wants to access this single-instance class,

You must use getinstance () to request (note the request) to obtain the instance of this Singleton class.

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.