Java design Pattern--Singleton mode

Source: Internet
Author: User

Quoted from Baidu Encyclopedia:

Singleton mode is a kind of common software design pattern. In its core structure, it contains only a special class called a singleton class.

The singleton mode can ensure that there is only one instance of a class in the system, and the instance is easy to be accessed by the outside world, thus it is convenient to control the number of instances and save system resources.

Singleton mode is the best solution if you want to have only one object for a class in the system.

Singleton mode is actually a relatively easy to understand design pattern, let's look at a more famous definition:

The initial definition of the singleton pattern appears in design mode (Eddisonvis, 1994): "Guarantees that a class has only one instance, and provides a global access point to access it." ”

According to this definition, we know that there are two requirements for implementing a singleton pattern, and we can interpret them as: a class that conforms to a singleton pattern can only instantiate an immutable object, that is, multiple instantiations of the class are given the same object, and the class can be instantiated at the global scope. For the moment, we don't care about its implementation, but first of all, what is the point of doing so? The point is that it ensures uniqueness and, to a certain extent, is a resource-saving function. For example, we use the same QQ account on the same computer to log on is a failure, because you do not need two exactly the same chat tool, they have the same interface, receive the same message, this is repeated, is meaningless. And it wastes system resources. Therefore, this is the meaning of the existence of a singleton pattern.

So how should a singleton model be implemented? In general, there are three implementations of the following:

Lazy Type:

1 //1, lazy-type2 3  Public classSingletonpattern {4 5     //declaring a private, static instance reference6     Private StaticSingletonpattern Singletonpattern =NULL;7 8     //set the parameterless construction method to private to prevent the external class from creating an instance by constructing the method9     PrivateSingletonpattern () {Ten     } One  A      Public StaticSingletonpattern Getsingletonpattern () { -         if(Singletonpattern = =NULL) { -Singletonpattern =NewSingletonpattern (); the         } -         returnSingletonpattern; -     } -}

A Hungry man type:

1 //2. A Hungry man type2 3  Public classTest {4 5     //implementing singleton patterns in an inner class6      Public Static classSingletonpattern {7 8         Private Static FinalSingletonpattern Singletonpattern =NewSingletonpattern ();9 Ten         //the same is the private construction method One         PrivateSingletonpattern () { A         } -  -          Public StaticSingletonpattern Getsingletonpattern () { the             returnSingletonpattern; -         } -     } -}

Double Lock type:

1 //3. Double Lock type2 3  Public classTest {4 5     //implementing singleton patterns in an inner class6      Public Static classSingletonpattern {7 8         Private StaticSingletonpattern Singletonpattern =NULL;9 Ten         //to set a private construction method One         PrivateSingletonpattern () { A         } -  -          Public StaticSingletonpattern Getsingletonpattern () { the             if(Singletonpattern = =NULL) { -                 //ensure thread synchronization -                 synchronized(Singletonpattern.class) { -                     if(Singletonpattern = =NULL) { +Singletonpattern =NewSingletonpattern (); -                     } +                 } A             } at             returnSingletonpattern; -         } -     } -}

The above three forms, some thread-safe, some threads are unsafe. Some are implemented through an external class, and some are implemented through an inner class. In short, the specific use of the situation has to be specific analysis.

This article has not been analyzed in place or even the wrong place, but also please correct me!

This article cited the source: single-Case model _ Baidu Encyclopediaterm created by: 20071522110

Java design Pattern--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.