Hello I have a few questions in regards to the Build Pattern?
- Why are the instance variables in the main class private?
- Why is the inner class declared static??
- Why are the instance variables in the inner class private and repeated?
- We return the Builder Object within the methods what exactly is contained within this object?
-
Why is the main constructor private?
public class Plank { //1. Why are these instance variables private? private double widthInches; private double heightInches; private double thicknessInches; //2. Why is this class static? public static class Builder { //Why are these instance variables private and repeated? private double widthInches; private double heightInches; private double thicknessInches; public Builder widthInches(double inches) { widthInches = inches; //3. What is returned here what object is this referencing? return this; } public Builder heightInches(double inches) { heightInches = inches; return this; } public Builder thicknessInches(double inches) { thicknessInches = inches; return this; } public Plank build() { return new Plank(this); } } //4. Why is this constructor private? private Plank(Builder build) { widthInches = build.widthInches; heightInches = build.heightInches; thicknessInches = build.thicknessInches; }}
Aucun commentaire:
Enregistrer un commentaire