Design Model 6, singleton

Singleton pattern in the interview and broader application of a design pattern - not the corresponding service, as long as the global business requirements only generate business objects may be used a simple interest

Example 1

public  class the Singleton {
     Private  static the Singleton instance = new new the Singleton (); // privatization static variables, external access is prohibited 
    Private the Singleton () {} // privatization configuration parameters to prevent externally generated instance 
    / * * 
     * Function: <br > 
     * <opening method: obtain the current instance of the object unique> 

     * @return: 
     * @Since: 1.0.0 
     * @author: 
     * @date: 
     * / 
    public  static the Singleton the getInstance () {
         return instance; 
    } 

}

Characteristics of the examples described above - typically single embodiment mode starving

1, when the class is loaded on initialized, more waste of memory,

2, thread-safe - it is based classloader mechanism avoids the synchronization problem multithreading

3, simple, high efficiency

Example Two (for less memory consumption)

public  class the Singleton {
     Private  static the Singleton instance; // privatization static variables, external access is prohibited - None initialized 
    Private the Singleton () {} // privatization configuration parameters to prevent externally generated instance 
    / ** 
     * Function: <br> 
     * <determines whether the first instance is empty, empty instance is created, otherwise it returns directly> 

     * @return : 
     * @Since : 1.0.0 
     * @author: 
     * @date: 
     * / 
    public  static the Singleton the getInstance () {
         iF (instance == null ) { 
            instance = new new the Singleton (); 
        } 
        return instance;
    }

}

Features - single idler embodiment mode

1, if you are using initialization, reduce memory consumption

2, there will be security thread in a multithreaded situation - because in a multi-threaded thread safety can not be guaranteed, so strictly speaking is not a singleton

3, simple, high efficiency

Example Two examples 3-- unresolved issues

public  class the Singleton {
     Private  static the Singleton instance; // privatization static variables, external access is prohibited - None initialized 
    Private the Singleton () {} // privatization configuration parameters to prevent externally generated instance 

    public  static  the synchronized    the Singleton the getInstance () {
         IF ( == instance null ) { 
            instance = new new the Singleton (); 
        } 
        return instance; 
    } 

}

Features:

1, the first call was initialized, to avoid wasting memory.

2, the lock must be synchronized in order to ensure a single case, but the lock will affect the efficiency.

Example 4,

public  class the Singleton {
     Private  static the Singleton instance; // privatization static variables, external access is prohibited - None initialized 
    Private the Singleton () {} // privatization configuration parameters to prevent externally generated instance 
  
    public  static   the Singleton the getInstance () {
         IF (instance == null ) {
             the synchronized . (the Singleton class ) {
                 IF (instance == null ) { 
                    instance = new new the Singleton (); 
                } 
            } 
        } 
        return instance; 
    }

}

Feature

1, thread-safe, high efficiency - less locking mechanism will be synchronized to within a block of code method to achieve, so only enter when the locking mechanism in the first initialization, affect efficiency

2, lazy loading, saving memory

 Example 5

public class Singleton {  
    private static class SingletonHolder {  
    private static final Singleton INSTANCE = new Singleton();  
    }  
    private Singleton (){}  
    public static final Singleton getInstance() {  
    return SingletonHolder.INSTANCE;  
    }  
}

Feature

1, this approach can achieve the same effect double check the lock mode, but the implementation is simpler. Initialization of the static field using a delay, this mode should be used instead of double lock detection mode. This embodiment applies only to the static field, the double lock detection mode can be used when necessary to delay the initialization of instance fields

Explanation:

In this way the use of the same classloader mechanisms to ensure that only one thread at initialization instance, with the first ways it is different: The first three ways as long as the Singleton class is loaded, the instance is instantiated (not reached lazy loading effect), but this approach is being loaded Singleton class, instance may not be initialized. Because SingletonHolder class is not actively used, only by explicitly calling getInstance method will explicitly load SingletonHolder class to instantiate instance

Example 6-- see on rookie tutorial, do not quite understand - Reference Links: https://www.runoob.com/design-pattern/singleton-pattern.html

public enum Singleton {  
    INSTANCE;  
    public void whateverMethod() {  
    }  
}

Explanation:

This implementation has not been widely adopted, but this is the best way to achieve single-mode embodiment. It is more compact, automatic support serialization mechanism to prevent multiple instances of the absolute.
This approach is advocated Effective Java author Josh Bloch way, it can not only avoid multi-thread synchronization problems, but also automatically supports serialization mechanism to prevent deserialization re-create a new object, absolutely prevent multiple instantiation. However, only joined the enum properties after JDK1.5, write in this way can not help but feel strange, in practice, rarely used.
Not by reflection attack to call the private constructor.

 

Summary on the rookie:

In general, the second type is not recommended and three kinds of lazy, it is recommended to use one kind of a first embodiment hungry man. To achieve clear only when lazy loading effect, will use the first five kinds of ways to register. If it comes to deserialize create an object, you can try sixth enumeration method. If you have other special needs, you can consider using a fourth double check the lock mode

Guess you like

Origin www.cnblogs.com/yutf/p/11498766.html