Second: Consider using the builder when you encounter multiple constructor parameters

Source: Internet
Author: User
Tags sodium

Static factories and constructors have one common limitation: they do not scale well to a large number of optional parameters.

If a nutritional ingredient is labeled, there are two fields that are required and four fields are optional.

1. Overlapping constructor mode

A constructor with only the necessary parameters is provided, the second constructor has an optional parameter, the third constructor has two optional arguments, and so on, until the last constructor contains all the optional parameters.

Cons: Who as the first optional parameter is an issue, because the constructor that contains two optional arguments must pass the value to the first optional parameter, causing the need to set many parameters that you do not want to set, such as an extreme example nutritionfacts n = new Nutritionfacts ( 240,8,0,0,0,10), only the last optional parameter we want to set, but we have to set the previous three optional parameters.

2.JavaBean mode

Create an object from a parameterless constructor, and then call the setter method to set each necessary parameter.

This makes it easy to create an instance, and the resulting code is easier to read because the setter's method name will tell you which parameter is set.

Disadvantage: The construction process is divided into several calls, in the construction process can not guarantee the consistency of JavaBean.

3.Builder mode

Instead of generating the desired object directly, let the customer call the constructor with all the necessary parameters, get a Builder object, and then the client invokes a setter-like method to set each optional parameter on the Builder object, and finally, The client calls the parameterless build method to generate the immutable object.

 Public classnutritionfacts {Private intservingsize;//requiredPrivate intservings;//requiredPrivate intcalories;//optionalPrivate intfat;//optionalPrivate intsodium;//optionalPrivate intcarbohyrate;//optional Public Static classBuilder {//Static builder classPrivate Final intservingsize; Private Final intservings; Private intCalories = 0; Private intFat = 0; Private intSodium = 0; Private intCarbohyrate = 0;  PublicBuilder (intServingsize,intservings) {The//required parameter must be supplied in the constructor This. servingsize =servingsize;  This. servings =servings; }
Optional parameters are assigned in a setter-like method PublicBuilder Calories (intval) {Calories=Val; return This; } PublicBuilder Fat (intval) {Fat=Val; return This; } PublicBuilder Sodium (intval) {Sodium=Val; return This; } PublicBuilder Carbohydrate (intval) {Carbohyrate=Val; return This; }
No parameter build method to generate the target instance Publicnutritionfacts Build () {return NewNutritionfacts ( This); } } Privatenutritionfacts (Builder Builder) {//The builder instance is used as the constructor parameter, Buidler automatically populates an unassigned optional field servingsize=builder.servingsize; Servings=builder.servings; Calories=builder.calories; Fat=Builder.fat; Sodium=Builder.sodium; Carbohyrate=builder.carbohyrate; } }

Instantiate nutritionfacts:

New Nutritionfacts.builder (8). Calories (+). Sodium (+). Carbohydrate () build ();

Notice that we don't assign a value to the Fat field, and builder automatically fills us with the default value of 0

The construction process is now on a single statement without the disadvantage of the JavaBean pattern, with the readability of the JavaBean pattern, and the disadvantage that overlapping constructor patterns have to be set for parameters that you do not want to set.

Disadvantages of the builder mode:

In order to create an object, you must first create its constructor, which may not be appropriate in a very performance-oriented situation. The builder mode is longer than the code that overlaps the constructor pattern, so it is used only when there are many parameters, such as 4 or more parameters.

Second: Consider using the builder when you encounter multiple constructor parameters

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.