Effective java-Create Dafabet lottery ticket platform for lease and destroy objects

Source: Internet
Author: User
Tags protected constructor ticket

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.