Consider replacing the constructor with a static factory method
1. The static factory method here refers to a static method that returns an object that is referred to as a class, rather than a static factory method in design mode.
2, the advantages of static factory methods are:
A, using different method names can significantly indicate the difference between two static factory methods, unlike constructors, whose names can only be class names.
b, not every time the static factory method is called, a new object is reconstructed.
C, an object that can return a subtype of type.
D. When you create an instance of a parameterized type, the code becomes concise.
3, the disadvantages of static factory methods are:
A, if the class does not contain a public or protected constructor, it cannot be overridden (the existence of a static factory method reduces the need for the constructor to exist)
B, unlike other static methods, there is virtually no difference.
4. The customary name of the static factory method:
A, ValueOf (type conversion method)
b, of (with valueof, popular in Enumset)
C, getinstance
D, Newinstance (returns the new instance object)
E, getType (used in different classes, type of instance object returned)
F, NewType (same as GetType, returns the new instance object)
Second, when encountering multiple constructor parameters, consider using the builder
1. Overlapping constructor (telescoping constructor) mode: The first constructor contains only the necessary parameters, and the subsequent constructors overlay the optional parameters in turn.
2. JavaBeans mode: Parameterless constructor constructs the object and invokes the setter method to set the necessary parameters and optional parameters.
3, builder mode: Do not directly generate the desired object, let the client use the necessary parameters to invoke the constructor (or static factory), get a Builder object, and then the client calls a setter-like method on the Builder object, to set the optional parameters. Finally, the client calls the parameterless build method to generate the immutable object.
4. The builder that sets the parameters generates a good abstract factory. The client passes a builder to the method, which can create one or more objects for the client.
5, the disadvantages of the builder:
A, in order to create the object, you must first create a builder, resulting in performance loss
b, the use of the process is more verbose than the overlapping builder mode.
6, the use of time: the class of the constructor or static factory has a number of parameters, especially when the majority of parameters are optional, the builder mode is a good choice.
Third, using a private constructor or enumeration type to harden the Singleton property
1. A class that is instantiated only once, called Singleton, represents a system component that is inherently unique. Singleton makes its client-side testing very difficult.
2, the realization of the Sigleton three ways:
A, the public static member is a final domain (self-assured serialization, requires additional operations: such as transient all instance domains, providing readresolve methods, etc.)
B, public static Factory method (self-guaranteed serialization, requires additional operations: such as transient all instance domains, providing readresolve methods, etc.)
C, an enumeration type that contains only one element. (best method)
Iv. ability to harden non-instancing through private constructors
V. Avoid creating unnecessary objects
1, pay attention to avoid excessive problems, unless the object is heavyweight, or there is no need to maintain their own pool of objects.
Vi. elimination of expired object references
1. If your class is managing memory yourself, you should be wary of memory leaks.
2, another common source of memory leaks is the cache, using Weakhashmap can solve some problems
3. A third source of memory leaks is listeners and other callbacks. Weakhashmap can solve some problems.
Vii. avoiding the use of termination methods
1. The finalization method is not used unless it is intended to be a safety net, or to terminate non-critical local resources. Note that the end method chain does not execute automatically, and you need to call the Super.finalize method yourself. or for public non-final classes, consider using the summary method guardians.
Effective Java Learning notes creation and destruction objects