Single-Case mode

Source: Internet
Author: User

Singleton mode (Singleton): Ensures that instances of a singleton class exist in a single instance of the entire application lifecycle, at any one time

A Hungry man type (recommended):

 Public classSingleton {//generate instance object when class loads, avoid problems when multi-threading//private methods to avoid direct access by external classes//A static property that guarantees that the instance object can be used inside the method provided to the external class invocation    Private StaticSingleton Singleton =NewSingleton (); //The constructor must be private//this way, you cannot use new to create an instance of the class externally.    PrivateSingleton () {}//static method to ensure that no object is generated when an external class is called//the only way to get an instance     Public StaticSingleton getinstance () {returnSingleton; }}

Lazy Type:

 Public classSingleton {//define a private static global variable to hold the unique instance of the class    Private StaticSingleton Singleton; //The constructor must be private//this way, you cannot use new to create an instance of the class externally.    PrivateSingleton () {}//define a global access point//set to static method//You can call the method outside of the class without instantiating it     Public StaticSingleton getinstance () {/*** So in the absence of the first heavy singleton = = NULL, it is also possible to implement a singleton mode?         So why do we need the first Singleton = = null? * Here is a performance issue, because for Singleton mode, New Singleton () only needs to be executed once, * and if there is no first Singleton = = NULL, each time a thread enters getinstance (), it will Perform a lock operation to achieve thread synchronization, * This is very performance-intensive, and if I add the first singleton = = NULL, * then only the first time, that is, Singleton ==null when the case is executed once the lock to         Thread synchronization, * and later, as long as the direct return to the Singleton instance is OK and no longer need to enter the lock statement block, so you can solve the performance problems caused by thread synchronization. */        //this is guaranteed to be instantiated only once, that is, instantiated on the first call, and no further instantiation of the call, the first singleton = = null        if(Singleton = =NULL) {                        synchronized(Singleton.class) {                                //Second heavy singleton = = NULL                if(Singleton = =NULL) {Singleton=NewSingleton (); }            }        }        returnSingleton; }}

Single-Case 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.