Why do you use builder in Java?

Asked 1 years ago, Updated 1 years ago, 110 views

When you create an object, you use a Builder pattern instead of a constructor. Why do you use this?

java builder

2022-09-21 15:35

1 Answers

The Static Factory method and its constructors are constrained by the lack of flexibility when there are more parameters to choose from.

Alternatives to generators with many parameters are the telescoping constructor pattern and the JavaBeans pattern. The telescoping generator pattern has the disadvantage of increasing the number of parameters making it difficult to write client code and poor readability of code, and the Java Bean pattern has the disadvantage of maintaining the object consistent and requiring additional efforts by programmers to keep it safe in the thread.

So we have a method that combines the safety of the telescoping generator pattern with the readability of the Java Beans pattern, which we call the builder pattern.

Instead of immediately generating the desired object, the client calls a constructor (or static factory method) with all the required parameters to obtain the builder object, and then calls the setter method of the builder object to set the value of the required selection parameters.

// Builder pattern
    public class NutritionFacts {
        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 Parameters
            private final int servingSize;
            private final int servings;
            // Select parameters - initializes to the default value.
            private int calories = 0;
            private int fat = 0;
            private int carbohydrate = 0;
            private int sodium = 0;
            public Builder(int servingSize, int servings) {
                this.servingSize = servingSize;
                this.servings = servings;
            }
            public Builder calories(int val) {
                calories = val;
                return this;
            }
            public Builder fat(int val) {
                fat = val;
                return this;
            }

            public Builder carbohydrate(int val) {
                carbohydrate = val;
                return this;
            }

            public Builder sodium(int val) {
                sodium = val;
                return this;
            }

            public NutritionFacts build() {
                return new NutritionFacts(this);
            }
        }

        private NutritionFacts(Builder builder) {
            servingSize = builder.servingSize;
            servings = builder.servings;
            calories = builder.calories;
            fat = builder.fat;
            sodium = builder.sodium;
            carbohydrate = builder.carbohydrate;
        }
    }

NutritionFacts is an invariant class and holds the default values of all parameters in one place. And the builder's setter methods return the builder's own object so that it can be called multiple times in a row.

NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).calories(100).
                    sodium(35).carbohydrate(27).build();
public interface Builder<T> {
        public T build();  
    }


2022-09-21 15:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.