Java Design pattern------4, Singleton mode

Source: Internet
Author: User

Singleton mode is an object-creation pattern that uses singleton mode to ensure that only one instance object is generated for a class. That is, in the entire program space, there is only one instance object in the class.

In fact, Gof's definition of a singleton pattern is to guarantee a class that only one instance exists and provides a global access method that can be accessed by that instance.

  

  So why should we use a singleton model?

This is because at the time of application development, we often have the following requirements:

1. Share the same resource or manipulate the same object between multiple threads, such as a servlet environment.

2. Use global variables throughout the program space to share resources.

3, in the large-scale system, in order to consider the performance, need to save the creation time of objects and so on.

Because singleton mode guarantees that only one instance object is generated for a class, the singleton pattern will be useful in these cases.

The singleton pattern is divided into several situations

  1, a Hungry man type (when the class is loaded, the initialization is completed, so the class load is slow, but the speed of getting objects is fast, and can not be delayed loading)

 Public class Person {    publicstaticfinalnew person ();     // Privatization of constructor functions    Private Person () {}     // provides a global static method     Public Static Person Getperson () {        return person ;    }}

  

2, Next is the lazy type (when the class is loaded without initialization, can delay loading)

Lazy type can be divided into two kinds, a thread-safe, a thread insecure

  (1) Lazy type (thread unsafe, but high efficiency)

 Public classPerson { Public StaticPerson person =NULL; //privatization of constructor functions    PrivatePerson () {}//provides a global static method     Public StaticPerson Getperson () {if(Person = =NULL) { person=NewPerson (); }        returnPerson ; }}

(2) lazy (thread-safe, but inefficient)

1  Public classPerson {2      Public StaticPerson person =NULL;3 4     //privatization of constructor functions5     PrivatePerson () {}6 7     //provides a global static method8      Public Static synchronizedPerson Getperson () {9         if(Person = =NULL){Tenperson =NewPerson (); One         } A         returnPerson ; -     } -}

3, Static internal class (can delay loading)

 This approach is an upgrade of the A Hungry man style, which also takes advantage of the classloder mechanism to ensure that only one thread is initialized instance, while the deferred load is implemented

1  Public classPerson {2     Private Static classpersonholder{3         Private Static FinalPerson person =NewPerson ();4     }5 6     //privatization of constructor functions7     PrivatePerson () {}8 9     //provides a global static methodTen      Public Static FinalPerson Getperson () { One         returnPersonholder.person; A     } -}

4, double check (for lazy-type upgrade, more efficient)

 Public classPerson { Public StaticPerson person =NULL; //privatization of constructor functions    PrivatePerson () {}//provides a global static method     Public StaticPerson Getperson () {if(Person = =NULL){            synchronized(person.class){                if(Person = =NULL) { person=NewPerson (); }            }        }        returnPerson ; }}

This write, only the new instance of the code into the synchronization lock, in order to ensure that the thread security in the synchronization lock plus a judgment, although it seems more cumbersome, but the synchronization of the content will only be executed once, after the execution, after the outer layer after the if judgment, will not be executed, so no more blocking. The program is also more efficient to run.

Java Design pattern------4, 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.