lundi 14 septembre 2015

What makes up a Builder Patern?

Hello I have a few questions in regards to the Build Pattern?

  1. Why are the instance variables in the main class private?
  2. Why is the inner class declared static??
  3. Why are the instance variables in the inner class private and repeated?
  4. We return the Builder Object within the methods what exactly is contained within this object?
  5. 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