JAVA- singletons several implementations

 

First, what is the singleton

    

    Single Example: to ensure only one instance of a class, and it provides access to a global access point.

    Singleton pattern is one of a common software design pattern, the only aim is to ensure the presence of instances throughout the application class only.

    For example, we at system startup, you need to load some common configuration information for the entire lifecycle of the application are visible and unique, then the need to design a singleton. Such as: spring container, session factory, a cache, a database connection pool and the like.

 

Second, how to ensure the only instance

         1) to prevent externally initiated

    2) class by itself instantiated

    3) ensure that once instantiated

    4) provide external method to obtain an instance of

    5) Thread Safety

Third, the comparison of several models of simple interest

 

(1) starving formula

"Because hungry, so eat immediately, without delay", while instantiated in a static private variable defined classes.

public class Singleton {
    private static final Singleton singleton = new Singleton();

    private Singleton() {
    }

    public static Singleton getInstance() {
        return singleton;
    }

}

① declare a static private class variables, and immediately instantiate ensure instantiated once

② private configured to prevent external instantiated (by reflection can be instantiated, such a case is not considered)

③ provide the public getInstance () method for obtaining the singleton instance external

Benefits: thread-safe; Gets an instance of fast Disadvantages: class loader instance that is initialization, memory waste.

 

2) lazy style

"The people lazy, and so on for you when it is used to instantiate" lazy loading.

public class Singleton {

private static Singleton singleton = null;

public class Singleton {
    private static Singleton singleton = null;

    private Singleton() {
    }

    public static Singleton getInstance() {
        if (singleton == null) {
            singleton = new Singleton();
        }
        return singleton;
    }

}

Advantages: In the process of acquiring instance, initialization of example, to save system resources

Disadvantages: ① while getting instance, more initialization, loading speed will slow down, the Department can affect system

② get every instance of non-null check should be carried out, a large overhead

③ non-thread safe, when multiple threads access getInstance (), may cause multiple instances.

 

Next, the above example to thread safety improvement:

(1) synchronization lock:

public class Singleton {
    private static Singleton singleton = null;

    private Singleton() {
    }

    public synchronized static Singleton getInstance() {
        if (singleton == null) {
            singleton = new Singleton();
        }
        return singleton;
    }
    
}

Advantages: thread-safe, Cons: Gets an instance of a time to be locked, resource-intensive, in fact, as long as the examples have been generated after the acquisition will not have to lock up.

(2) double-checked lock:

public class Singleton {
    private static Singleton singleton = null;

    private Singleton() {
    }

    public static Singleton getInstance() {
        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }

}

Advantages: thread-safe, double check to ensure that only synchronization, high efficiency shortcomings before uninitialized instance: Examples of non-empty or judgment, consuming certain resources.

(3) static inner classes:

public class Singleton {
    private Singleton() {

    }

    private static class SingletonHolder {
        private static final Singleton singleton = new Singleton();
    }

    public static Singleton getInstance() {
        return SingletonHolder.singleton;
    }

}

Advantages: not only avoids the synchronization performance cost, but also can delay loading.

(4) enumeration:

public  enum the Singleton { 
    INSTANCE; 
    public  void the init () { 
        System.out.println ( "Resource Initialization" ); 
    } 
}

Natural thread-safe to prevent reflection generated instance.

 

Fourth, the singleton advantages and disadvantages:

Advantages: there is only one instance of the class, save system resources; the need for frequent destruction of objects created using a single mode of embodiment can improve system performance. 

Disadvantages: can not be instantiated outside the (new), who do not know which method to call call to get confused when instances, especially when looking at the source code when not.

 

Guess you like

Origin www.cnblogs.com/cat520/p/11259400.html