Java design pattern: Singleton mode "Singleton pattern"

Source: Internet
Author: User



Singleton mode is very simple, that is, in the constructor and add a constructor, access permissions are private, this pattern is simple, but the risk of simple, risk? What risk? In a B/s project, each HttpRequest request to the Java EE container creates a thread, and each thread creates the same singleton object.

/**
* I ' m glad to share my knowledge.
* China's history is generally a dynasty an emperor, there are two emperors words, must pk out an emperor out
*/
Public Classemperor {
Private staticemperor emperor= null; Define an emperor to put there, and then give this emperor the name
Privateemperor () {
Secular and moral restraint you, the purpose is not to let you produce a second emperor
}
public static emperorgetinstance () {
if (emperor== null) {//If the emperor has not yet defined, then set a
Emperor= Newemperor ();
}
return emperor;
}
What's the emperor's name?
public static voidemperorinfo () {
System.out.println ("I am the Emperor xxx ...");
}
}

Then define the minister:
/**
* I ' m glad to share my knowledge.
* The minister is to face the emperor every day, see the Emperor today and yesterday, the day before, it is a problem!
*/
@SuppressWarnings ("All")
Public Classminister {
/**
* @param args
*/
public static Voidmain (string[] args) {
First day
Emperor emperor1=emperor.getinstance ();
Emperor1.emperorinfo (); What was the name of the emperor I saw on the first day?
Next day
Emperor emperor2=emperor.getinstance ();
Emperor.emperorinfo ();
Third Day
Emperor emperor3=emperor.getinstance ();
Emperor2.emperorinfo ();
Three days see the emperor is the same person, honored!
}
}



Singleton mode is very simple, that is, in the constructor and add a constructor, access permissions are private, this pattern is simple, but the risk of simple, risk? What risk? In a B/s project, each HttpRequest request to the Java EE container after the creation of a thread, each thread to create the same singleton object, how to do? OK, let's write a generic singleton, and then analyze:

/**
* I ' m glad to share my knowledge.
* General-purpose single-case mode
*/
@SuppressWarnings ("All")
Public Classsingletonpattern {
Private Staticsingletonpattern singletonpattern= null;
Limit cannot directly produce an instance
Privatesingletonpattern () {
}
Publicsingletonpattern getinstance () {
if (this.singletonpattern== null) {//If there are no instances yet, create a
This.singletonpattern= Newsingletonpattern ();
}
return this.singletonpattern;
}
}


Assuming that there are now two threads A and thread B, thread a executes to This.singletonpattern = new Singletonpattern () and is requesting memory allocations, which may take 0.001 microseconds, within these 0.001 microseconds, thread B executes to I F (This.singletonpattern = = null), are you saying this is true or false? Is true, then what then? Thread B also goes down, so in memory there are two instances of Singletonpattern, to see if there is a problem?

What if you were to get a serial number or create a signal resource for this single case? Business logic is confusing! Data consistency check failed! The most important thing is that you can not see any problem from the code, this is the most deadly! Because this situation basically you can not reproduce, shudder, then how to modify? With a lot of options, I'll say a simple, thorough solution to the problem:

/**
* I ' m glad to share my knowledge.
* General-purpose single-case mode
*/
@SuppressWarnings ("All")
Public Classsingletonpattern {
private static Finalsingletonpattern singletonpattern= new
Singletonpattern ();
Limit cannot directly produce an instance
Privatesingletonpattern () {
}
Public synchronized Staticsingletonpattern getinstance () {
return singletonpattern;
}
}


Direct new An object is passed to the member variable of the class Singletonpattern, when you want getinstance () to return directly to you, solve the problem!

Java design pattern: Singleton mode "Singleton pattern"

Related Article

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.