Design pattern (+)-----Single-Case mode

Source: Internet
Author: User

Singleton mode (singleton) definition

Ensure that a class has only one instance and provides a global access point to access him.

Example

The first of these methods

 PackageCom.csdhsm.pattemdesign.singleton;/*** @Title: Singleton1.java * @Description: The first method is only applicable when the thread is not secure on multiple threads *@author: Han * @date: June 27, 2016 morning 10:37:07*/   Public classSingleton1 {Private StaticSingleton1 Singleton;  Public StaticSingleton1 getinstance () {if(Singleton = =NULL) {Singleton=NewSingleton1 (); }        returnSingleton; }}

The second method of

 PackageCom.csdhsm.pattemdesign.singleton;/*** @Title: Singleton2.java * @Description: The second method is secure in multithreaded, but synchronous lock is a very time-consuming operation, the method is inefficient *@author: Han * @date: June 27, 2016 morning 10:39:09*/   Public classSingleton2 {Private StaticSingleton2 Singleton;  Public StaticSingleton2 getinstance () {//Add a sync lock to avoid thread insecurity        synchronized(singleton) {if(Singleton = =NULL) {Singleton=NewSingleton2 (); }        }        returnSingleton; }}

The third method of

 PackageCom.csdhsm.pattemdesign.singleton;/*** @Title: Singleton3.java * @Description: The third method, double if judgment, once the object is instantiated, it will no longer enter the synchronization lock, greatly improve efficiency, but the code is slightly more complex *@author: Han * @date: June 27, 2016 morning 10:41:31*/   Public classSingleton3 {Private StaticSingleton3 Singleton;  Public StaticSingleton3 getinstance () {if(Singleton = =NULL) {            synchronized(singleton) {if(Singleton = =NULL) {Singleton=NewSingleton3 (); }            }        }        returnSingleton; }}

Fourth method

 PackageCom.csdhsm.pattemdesign.singleton;/*** @Title: Singleton4.java * @Description: The fourth method, the typical a hungry man load, when the class is initialized, instantiates the object, this method does not have thread safety, * but when  Another static method in the class is accessed, which will cause the JVM to initialize the instance, and the most obvious disadvantage is that if the method within the constructor is time-consuming, the loading process will be longer. * @author: Han * @date: June 27, 2016 morning 10:43:43*/   Public classSingleton4 {Private StaticSingleton4 Singleton =NewSingleton4 ();  Public StaticSingleton4 getinstance () {returnSingleton; }}

Fifth method

 PackageCom.csdhsm.pattemdesign.singleton;/*** @Title: Singleton5.java * @Description: The fifth method, introducing an intermediate container, when calling other static methods in Singleton5, * Insta NCE does not rush to instantiate, but when used, if the Singletonholder class is not loaded, it will be loaded, * play the role of lazy loading, so this method is a lazy singleton, and did not use to synchronized synchronization block. * @author: Han * @date: June 27, 2016 morning 10:46:38*/   Public classSingleton5 {Private Static classsingletonholder{StaticSingleton5 singleton5 =NewSingleton5 (); }        PrivateSingleton5 () {};  PublicSingleton5 getinstance () {returnsingletonholder.singleton5; }}
Summarize

If it is a simple single-class design, for general applications, the code in the construction method does not involve reading configuration, remote invocation, initialization of the IOC container, and so on for a long time, you can use the fourth method of loading. Instead, you need to use the fifth instance method, which is instantiated when used, delaying loading.

Design pattern (+)-----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.