Consider using static methods instead of constructors
? A class can provide its clients through a static factory method, rather than through a constructor.
Advantage:
? 1. They have a name that can be expressed more clearly by name as the behavior of the created object.
? 2. It is not necessary to return a new object each time they are called. This allows immutable classes to use pre-built instances, or to cache the built-in instances and reuse them.
? 3. They can return objects of any subtype of the original return type. The API can return an object without making the object's class public. Hide the implementation details in this way.
? 4. When creating parameterized instances, they make the code concise.
Disadvantages:
? 1. If the class does not contain a public or protected constructor, it cannot be quilt-like.
? 2, they are not actually any different from other static methods.
Second, consider using the builder when you encounter multiple constructor parameters.
Dafa Color _ ticket Platform rental address one: "hubawl.com" Fox PA Source Forum address two: "Bbscherry.com"
The overlapping constructor pattern is feasible, but when there are many parameters, the client code is difficult to write and difficult to read. A long string of parameters of the same type can cause some subtle errors. If the client accidentally reverses the order of two of these parameters, the compiler will not error, but the program will run with incorrect behavior.
? In this case, another alternative is to use the JavaBean mode. Set the necessary parameters through the setter method. Unfortunately, the model itself has very important shortcomings. Because the construction process is divided into several invocations, JavaBean may be in an inconsistent state during construction. Another disadvantage associated with this is that this mode prevents classes from being made immutable.
? Fortunately, there is a third alternative, builder mode. Instead of generating the desired object directly, let the client call the constructor with the necessary parameters, generate a Builder object, and then invoke a setter-like method on the builder object to set each related value. Finally, the client uses the parameterless builder method to generate the immutable object. This method is more flexible and easy to read, and can increase the constraint condition for the parameter. It also has its own shortcomings, such as in some very performance-oriented circumstances, its overhead may be problematic. This pattern is more verbose than an overlapping constructor, so it is used only when a number of parameters are available, especially when most parameters are optional.
Third #, hardening the Singleton property with a private constructor or enum type
?
The ability of non-instantiation is strengthened by the private constructor.
? For a class that cannot be instantiated, its constructor becomes private. For example, classes that contain only static methods and static fields.
V. Avoid creating unnecessary objects
? This article does not mean "we should avoid creating objects as much as possible". Because the constructors of small objects only do a small amount of display work, creating and retrieving actions is very inexpensive. Conversely, it is not a good idea to avoid creating objects by maintaining the object pool, unless the objects in the pool are heavyweight (a typical example is a database connection).
Six *, elimination of expired object references
?
Seven #, avoid using the finalization method
Effective java-Create Dafabet lottery ticket platform for lease and destroy objects