Single-Case mode

Source: Internet
Author: User

Singleton mode is one of the simplest forms in design mode. The main purpose of the singleton mode is to ensure that only one instance of a class is present in a Java application.

To achieve this, you can start by instantiating it from the client. Therefore, you need a mechanism that only allows you to generate a unique instance of an object class, "block" All access to the object that you want to generate. Use the factory method to restrict the instantiation process. This method should be a static method (class method), because it makes no sense to let an instance of the class generate another unique instance.

It is obvious that there are three main points of the singleton pattern; one is that a class can have only one instance, and the other is that it must create this instance on its own, and thirdly, it must provide this instance to the whole system on its own.

The general singleton pattern usually has three types:

The first form: The lazy type, is also the commonly used form.

A singleton instance is not created when the class is loaded. It is created only the first time the instance is requested, and the instance of the class is no longer created after the first time it is created.
1  Public classsingletonclass{2    Private StaticSingletonclass instance=NULL;3     Public StaticSingletonclass getinstance () {4      if(instance==NULL){5        synchronized(Singletonclass.class){6          if(instance==NULL)7Instance=new Singletonclass ();8 }9 }Ten      returninstance; One } A    PrivateSingletonclass () { - } -}

The second type: a hungry man.

1 Public static Class singleton{2     //defines an instance of itself within itself, for internal invocation only3private static final Singleton instance=new Singleton ();4     Private Singleton () {5       //dosomething6     }7     //This provides a static method for external access to this class, which can be accessed directly8     Public static Singleton getinstance () {9       return instance;Ten     } One}

The third form: The form of a double lock.

This singleton actually maintains an instance of a set of singleton classes that are stored in a map (the register), returned directly from the factory for an instance that has already been registered, and, for non-registered, first registered and then returned.
1 Public static Class singleton{2private static Singleton instance=NULL;3     Private Singleton () {4       //dosomething5     }6 Public static Singleton getinstance () {7       if(instance==NULL){8         synchronized(Singleton.class){9           if(NULL==instance) {TenInstance=new Singleton (); One           } A         } -       } -     return instance; the     } -}

This mode will synchronize the content below the if inside, improve the efficiency of the execution, do not have to synchronize every time to get the object, only the first time synchronization, created later is not necessary.

1  Public enum Singleton {2    INSTANCE; 3      Public void Whatevermethod () {4    }5 }

This approach is advocated by effective Java author Josh Bloch, which not only avoids multi-threaded synchronization problems, but also prevents deserialization from recreating new objects, which can be a very strong barrier, but Personally think that because of the 1.5 to join the enum characteristics, in this way to write inevitably people feel unfamiliar, in the actual work, I also rarely see someone write so.

http://devbean.blog.51cto.com/448512/203501/

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.