When encountering multiple constructor parameters, consider using the Builder -- objective Java Reading Notes.

Source: Internet
Author: User

When encountering multiple constructor parameters, consider using the Builder -- objective Java Reading Notes.

/*** If the constructor of a class or a static factory has multiple parameters, the Builder mode is a good choice when designing such a class, especially when most parameters are optional. * Compared with the traditional overlapping constructor mode, the client code in Builder mode is easier to read and write, and the Builder is more secure than the JavaBeans. ** @ Author Liu xiangfeng **/public class NutritionFacts {// All parameters: private final int servingSize; private final int servings; private final int calories; private final int fat; private final int sodium; private final int carbohydrate; public static class Builder {// required parameter. final ensures that it must be assigned private final int servingSize; private final int servings; // optional private int calories = 0; private int fat = 0; private int sodium = 0; private int carbohydrate = 0; /*** initialize required parameters in the constructor ** @ param servingSize * @ param servings */public Builder (int servingSize, int servings) {this. servingSize = servingSize; this. servings = servings;}/***** @ param val * assigns a value to the option * @ return Builder object to call the value assignment function in a chained manner */public Builder calories (int val) {calories = val; return this;} public Builder fat (int val) {fat = val; return this;} public Builder sodium (int val) {sodium = val; return this;} public Builder carbohydrate (int val) {carbohydrate = val; return this;}/*** create an object acquisition method and add the user (Builder) pass in the ** @ return NutritionFacts object */public NutritionFacts build () {return new NutritionFacts (this) ;}}/*** privatized constructor as a parameter, ensure that operations on the same object ** @ param builder is passed into the constructor to assign values */private NutritionFacts (Builder builder) {servingSize = builder. servingSize; servings = builder. servings; calories = builder. calories; fat = builder. fat; sodium = builder. sodium; carbohydrate = builder. carbohydrate;}/*** test method ** @ param args */public static void main (String [] args) {NutritionFacts cocaCola = new NutritionFacts. builder (240, 8 ). calories (100 ). sodium (35 ). carbohydrate (27 ). build ();}}

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.