Objective java Reading Notes the first consideration is to use the static factory method instead of the constructor. Objective Reading Notes

Source: Internet
Author: User
Tags protected constructor

Objective java Reading Notes the first consideration is to use the static factory method instead of the constructor. Objective Reading Notes

For a class, the most common method for clients to obtain a senior instance is to provide a common constructor. There is also a way to release your hair, and you should also have a place in the toolbox of every programmer. Class can provide a common static factory method, which only returns the static method of the class instance.

Class can provide its client (object) through the static factory method class, rather than through the constructor. The advantages of doing so are:

1. the major difference between the static factory method and the constructor is that they have names. for example, the BigInteger returned by the constructor BigInteger (int, int, Random) may be a prime number. the static factory method of probablePrime is clearly clearer. When a class requires multiple constructors with the same signature, the static factory method is used instead of the constructor, and names are carefully selected to highlight the differences between them.

2. The second advantage between the static factory method and the constructor is that you do not have to create a new object every time you call them. This is not specific. You can refer to the order example mode.

3. The third advantage of the static factory method and constructor is that they can return any child type objects of the original return type. This technology applies to the interface-based framework. When using this static method, the client even needs to reference the objects to be returned through the interface, rather than referencing the objects to be returned through its implementation class. This is a good habit.

4. The fourth advantage of static factory methods is that they make code more concise when creating parameterized instances.

Disadvantages:

The main disadvantage of the static factory method is that, if the class does not contain a common or protected constructor, it cannot be quilt-type.

The second disadvantage of static factory methods is that they are actually no different from other static methods. In the API documentation, they are not clearly identified as the constructor. Therefore, it is very difficult to find out how to instantiate a class for classes that provide static factory methods rather than constructors. However, you can make up for this disadvantage by paying attention to static factories in class or interface annotations and following standard naming conventions. The following are some common names:

ValueOf

Of

GetInstance

NewInstance

GetType

NewType

In short, static factory methods and constructor are all useful. We need to understand their respective interpretations. Static factories are usually more suitable. Therefore, the first response should not be to provide a public constructor, rather than a static factory.


Programming + introduction to English [English syntax]

1. parameters to a constructor //// to a constructor to modify Parameters and translate it into parameters of the constructor.
2. in and of themselves // translated into, itself, themselves refers to parameters
3. being returned // modifies the object, which means the object to be returned, which can be omitted in Chinese.
4. The if clause starts to guide a conditional adverbial clause. (in and of themselves can be ignored in the inserted language first) to the end of the being return clause. The main clause starts from a static factory ...... At the beginning, and connects two sentences, removing the modifier. The two parallel sentences connected are a static factory is easier to use and resulting client code is easier to read. The verbs in the two sentences are the same. The verbs in the second sentence can be omitted for conciseness.
I try to translate it. I may not translate Java terms accurately in C/C ++. The overall translation is as follows:
Unlike the constructor, the advantage of the static factory method is that it has a name. The parameters of the constructor themselves do not describe the returned objects. It is easier to use than the static factory with well-selected names, and the final client code is easy to read.


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.