03. The method of using a private constructor implemented Singleton class properties or pieces

Foreword

"Effective Java" Chinese third edition, is a book about the basics of Java, this book was recommended to me more than once. Including my favorite garden blog blogger CJ May , he worked in his blog "to the Java program apes recommend some good books worth a visit" also been recommended. Deepen their memories, while in line to good people, decided after reading each chapter, write an essay. If you have to write the wrong place, where unclear statements, or other suggestions, I hope you can correct me message, thank you.

"Effective Java" Chinese third edition Read online links: https://github.com/sjsdfg/effective-java-3rd-chinese/tree/master/docs/notes

 

What is

Singleton : single embodiment, it refers to instantiate only one of the class that represents a stateless object (stateless object refers to the data can not be saved, there is no instance variables of an object, thread-safe).

 

Where use

Global use of the class . Use this time to avoid a single case of frequent creation and destruction, and to ensure that the only objects in memory, you can save memory. At the same time, because the public instance of a singleton, in favor of Java's garbage collection mechanism.

For example, we implement a function: the current number of a site online (website counter), we can use a global object to record.

 

How to achieve

  • Private construction method
public  class the Singleton {
     // private constructor with no arguments 
    Private the Singleton () {} 

    // singleton object 
    Private  static  Final the Singleton instance = new new the Singleton (); 

    // static factory method 
    public  static the Singleton the getInstance () {
         return instance; 
    } 
}

Explain why write:

  1. We make a class is instantiated only once, could not let it just to do new operation, so  Singleton  's no-argument constructor is private.
  2. instance of a static member Singletion.
  3. getInstance singleton object is to obtain a method (we can also use the no-argument constructor takes singleton object), is a getInstance static factory method, it is relative to the use of no-argument constructor takes singleton object, there are three advantages : a more flexible . For example, we now need to change each thread calls the method returns a unique instance (directly in the static factory method new instance (), but this time the need to remove the singleton object in the final modified keywords). The second advantage is that, if an application needs him, we can change it to a generic singleton factory. The third advantage is that a single case can be used by the method of reference (with the name of the first chapter study notes already explained the difference between no-argument constructor and static factory method).

To prevent singleton class becomes serializable, simply add implements Serializable  to the declaration is not enough. We must declare all instance fields as transient , and provides a readResolve method. Otherwise, whenever a serialized instance is deserialized, a new instance is created, the code is implemented as follows:

public  class the Singleton the implements the Serializable {
     // private constructor with no arguments 
    Private the Singleton () {} 

    // singleton object 
    Private  static  Final the Singleton instance = new new the Singleton (); 

    // static factory method 
    public  static the Singleton the getInstance () {
         return instance; 
    } 

    // the method is provided to reassign the object to deserialize obtained. 
    public Object readResolve () {
         return instance; 
    } 
}
  • Enum class declaration of a single element , the code is implemented as follows:
public enum Singleton02 {
    INSTANCE;
}

Single element enumeration class is the best way to achieve a single embodiment. Because unpaid provides serialization mechanism.

 

to sum up

This note will focus on three ways to say a single case, their priority is as follows:

Single element enumeration class > private constructor ( static factory mode )> private constructor ( using the constructor with no arguments )

We should figure out is where a single case , these three methods to create a single example of what advantages .

Guess you like

Origin www.cnblogs.com/gongguowei01/p/12174260.html
Recommended