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 ();}}