JAVA design mode: Singleton)

Source: Internet
Author: User

Singleton design ensures that each class has only one instance and provides a global access point for this instance.

Unlike static members in the tool class, the singleton class is generally used to save application state data, which may be accessed or modified in all parts of the application.

Several implementation methods of Singleton mode.

[Java]
Public class Singleton {
Private static Singleton instance = new Singleton ();
 
Public static Singleton getInstance (){
Return instance;
}
 
/** Don't let anyone else instantiate this class */
Private Singleton (){
}
}

This method is easy to implement and ensures the uniqueness of the instance. The disadvantage is that the instance must be loaded before use, and the instance will always be loaded first regardless of whether the single-instance class is actually used, this seems quite inappropriate, so there is a Lazy Initialization mode.

[Java]
Public class Singleton {
Private static Singleton instance = null;
 
Private Singleton (){
 
}
 
Public static Singleton getInstance (){
If (instance = null ){
Instance = new Singleton ();
}
Return instance;
}
}

This method can achieve lazy loading, but when multiple threads enter the getInstance method at the same time, multiple instances may be generated, which obviously violates the original intention of the singleton mode. To avoid this situation, consider adding the synchronization mechanism.
[Java]
Public class Singleton {
Private static Singleton instance = null;
 
Private Singleton (){
}
 
Synchronized static public Singleton getInstance (){
If (instance = null ){
Instance = new Singleton ();
}
Return instance;
}
}

This method ensures that there is only one instance while loading it lazily, but synchronizing the entire getInstance method will consume the performance of thread synchronization. Www.2cto.com
[Java]
Public class Singleton {
Private static Singleton instance;
 
Private Singleton (){
}
 
Public static Singleton getInstance (){
If (instance = null ){
Synchronized (Singleton. class ){
If (instance = null ){
Instance = new Singleton ();
}
}
}
Return instance;
}
}

The above method is the so-called Double-check Locking mode, that is, the Double check and lock mode. Currently, it seems perfect.
By fancy888

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.