Singleton design pattern of six principles

  Introduction: continuous learning is the programmer's fate

A, singleton

  

  The so-called singleton design pattern is to take certain methods ensure that the entire software system, the presence of an object instance of a class only , and the class is only available method (static method) to obtain a object instance. Such as Hibernate's SessionFactory, it acts as a proxy data storage source, and is responsible for creating session objects. SessionFactory not lightweight

  Singleton following eight ways:

  

Second, the hungry man type (static constants)

public  class Singleton01 {
     // internally created instance of the class object of the present 
    Private Final static Singleton01 instance = new new Singleton01 ();
     // constructor privatization, not outside new new 
   Private Singleton01 () {}
    // provides a public static method, returns an instance of an object 
    Private  static Singleton01 the getInstance () {
        return instance;
    }

    public static void main(String[] args) {
        //测试
        Singleton01 instance1 = Singleton01.getInstance();
        Singleton01 instance2 = Singleton01.getInstance();
        System.out.println(instance1==instance2);
        System.out.println(instance1.hashCode());
        System.out.println(instance1.hashCode());
    }
}
Hungry man type (static constants)

analysis:

  (1) advantages: such an approach is relatively simple, that is, when the loading is completed the class is instantiated. To avoid the thread synchronization issues.

  (2) Disadvantages: class loading when instantiation is completed, do not achieve the effect of Lazy Loading. If you have never used this example from start to finish, it will result in a waste of memory

Third, the starving of formula (static code block)

public  class {Singleton02
     // create an object instance of this class internal 
    Private   static Singleton02 instance;
     // constructor privatization, not outside new new 
    Private Singleton02 () {}
     // static code block, create singletons 
    static {
        instance = new Singleton02();
    }
    // provides a public static method, returns an instance of an object 
    public  static Singleton02 the getInstance () {
         return instance;
    }
    public static void main(String[] args) {
        //测试
        Singleton02 instance = Singleton02.getInstance();
        Singleton02 instance2 = Singleton02.getInstance();
        System.out.println(instance == instance2); // true
        System.out.println("instance.hashCode=" + instance.hashCode());
        System.out.println("instance2.hashCode=" + instance2.hashCode());
    }
}
Starving formula (static code block)

analysis:

  (1) In this manner and similar to the manner described above, except that the process of the class is instantiated on static code block, the code is executed in the static code when loaded, initialize the instance of the class

  (2) Conclusion: singleton methods available, but may result in wasted memory

Fourth, the lazy man's (thread safe)

Guess you like

Origin www.cnblogs.com/rmxd/p/12635430.html