A single-instance pattern of Java design patterns

Source: Internet
Author: User

First, Introduction

The singleton mode is one of the Java23 design patterns, and the main function of the singleton mode is to ensure that only one instance of a class is present in a Java application.

Second, the characteristics

The singleton mode has the following characteristics:
1, the Singleton class can have only one instance.
2, the Singleton class must create its own unique instance.
3. The Singleton class must provide this instance to all other objects.

Iii. Advantages and Disadvantages

Advantagesfirst, instance controlSingleton mode prevents other objects from instantiating copies of their own singleton objects, ensuring that all objects have access to unique instances.Second, flexibilityBecause classes control the instantiation process, classes can flexibly change the instantiation process. Disadvantagesfirst, the costAlthough the number is small, there will still be some overhead if you want to check for instances of the class every time the object requests a reference. This problem can be resolved by using static initialization.Ii. Possible confusion of developmentWhen using singleton objects, especially those defined in a class library, developers must remember that they cannot useNewKeyword to instantiate the object. Because library source code may not be accessible, application developers may unexpectedly find themselves unable to instantiate this class directly.Iii. Object LifetimeThe problem of deleting a single object cannot be resolved. In a language that provides memory management, such as a. NET framework-based language, only a singleton class can cause an instance to be deallocated because it contains a private reference to the instance. In some languages, such as C + +, other classes can delete object instances, but this results in a floating reference in a singleton class.

Iv. form

The general singleton pattern usually has three forms: the first form: The lazy type, is also the commonly used form.
1  Public classSingleton {2 3     Private StaticSingleton instance =NULL;4     //the construction method is privatized so that other classes cannot get the Singleton object through New Singleton ()5     PrivateSingleton () {6 7     }8     //add synchronized to achieve thread safety9      Public Static synchronizedSingleton getinstance () {Ten         if(Instance = =NULL) { OneInstance =NewSingleton (); A         } -         returninstance; -     } the}

Second form: A Hungry man type

1  Public classSingleton2 {2     3     Private StaticSingleton2 instance =NewSingleton2 ();//Instantiate at the same time as the instance is defined4 5     PrivateSingleton2 () {6 7     }8     //get an instance directly9      Public StaticSingleton2 getinstance () {Ten         returninstance; One     } A}

The third form: The form of a double lock.

1  Public classSingleton {2 3     Private StaticSingleton instance =NULL;4 5     PrivateSingleton () {6 7     }8 9      Public StaticSingleton getinstance () {Ten         if(Instance = =NULL) { One             synchronized(Singleton.class){ A                 if(Instance = =NULL) { -Instance =NewSingleton (); -                 } the             } -         } -         returninstance; -     } +}

A single-instance pattern of Java design patterns

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.