Mat*_*sis 0 java design-patterns
您好,我对构建模式有几个问题?
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;
}
}
Run Code Online (Sandbox Code Playgroud)