Singleton Mode 123

Source: Internet
Author: User

Singleton mode The thing to do is simply to ensure that an object is globally unique.
Is that right? To see the standard
Singleton mode (Singleton pattern): Ensures that a class has only one instance, and instantiates itself and provides this instance to the system as a whole.
This class is called a singleton class, and it provides a method for global access. Singleton mode is an object-creation pattern.
It seems that I didn't define it all right.
The first guarantee class has only one instance
The second class itself instantiates itself (meaning that no other class can be new to itself)
Third method of providing global access
OK then I'm writing the first kind of singleton.
The first kind of singleton
public class singleton{    private static Singleton sl;    Private Singleton () {       System.out.println ("Initialize");    }    public static Singleton getinstance () {    if (sl==null)         sl=new Singleton ();     return SL;    }        public static void Main (string[] args) {        Singleton sl=singleton.getinstance ();        Singleton sl2=singleton.getinstance ();    }}
The result of the operation is to display an initialization
But there's a problem here. If multiple threads run this code at the same time, when the first thread runs to Sl=new Singleton () (This sentence has not been executed), the second thread checks for SL or NULL, and the second thread comes in ... Then there are multiple objects.

So there are

The second kind of singleton
/*** * A hungry man mode */public class singleton2{    private final static Singleton2 sl2=new Singleton2  ();    Private Singleton2 () {        System.out.println ("Initialize");    }    public static Singleton2 getinstance () {        return SL2;    }    public static void Main (string[] args) {        Singleton2 sl2=singleton2.getinstance ();        Singleton2 sl3=singleton2.getinstance ();    }}
This singleton mode, when we load the class, we put the class variable new out. This way we call it the A hungry man pattern.
The first a hungry man problem solves the above threading problem. When I first started loading, I had a class variable and there was only one, and naturally there was no threading problem.
But there's a performance problem, and no matter how I use this singleton class, it's always loaded in memory, so performance is a problem.
It says that this pattern is called a Hungry man mode, which naturally has a non-a Hungry man mode (the term is called lazy mode). But here I don't really want to talk to you about lazy mode, too complicated, and not how to use it.
Is there a single case that solves the first threading problem while maintaining performance (in fact, the second a Hungry man mode is good, that one class, how much performance can be dragged? Even if there are 10, 100 is not too much)? And it's not as complicated as the "lazy pattern" I'm talking about?
The answer is yes. Yes!
Inner class mode
public class singleton3{    private static class hold{        private  final static Singleton3 sl3=new Singleton3 (); c3/>}    Private Singleton3 () {    System.out.println ("Initialize");    }    public static Singleton3 getinstance () {          return hold.sl3;    }    public static void Main (string[] args) {             Singleton3 sl3=singleton3.getinstance ();         Singleton3 sl4=singleton3.getinstance ();         System.out.println (SL3==SL4);    }}
For the analysis of internal classes and static,final, we do not speak here for the time being.
The inner class solves several of the problems mentioned above.
But it also raises a question that is not a problem.

Inner classes are supported by Java, but not necessarily in other languages.

Resources
http://blog.csdn.net/lovelion/article/details/7420883

Singleton Mode 123

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.