|
| 1 | +package creational.builder; |
| 2 | + |
| 3 | +import lombok.Getter; |
| 4 | + |
| 5 | +@Getter |
| 6 | +public class House { |
| 7 | + private final String material; |
| 8 | + private final String structure; |
| 9 | + private final String roof; |
| 10 | + private final boolean hasGarage; |
| 11 | + private final boolean hasSwimmingPool; |
| 12 | + private final boolean hasGarden; |
| 13 | + |
| 14 | + public House(Builder builder) { |
| 15 | + this.material = builder.material; |
| 16 | + this.structure = builder.structure; |
| 17 | + this.roof = builder.roof; |
| 18 | + this.hasGarage = builder.hasGarage; |
| 19 | + this.hasSwimmingPool = builder.hasSwimmingPool; |
| 20 | + this.hasGarden = builder.hasGarden; |
| 21 | + } |
| 22 | + |
| 23 | + @Override |
| 24 | + public String toString() { |
| 25 | + return "House{" + |
| 26 | + "material='" + material + '\'' + |
| 27 | + ", structure='" + structure + '\'' + |
| 28 | + ", roof='" + roof + '\'' + |
| 29 | + ", hasGarage=" + hasGarage + |
| 30 | + ", hasSwimmingPool=" + hasSwimmingPool + |
| 31 | + ", hasGarden=" + hasGarden + |
| 32 | + '}'; |
| 33 | + } |
| 34 | + |
| 35 | + public static class Builder { |
| 36 | + private final String material; |
| 37 | + private final String structure; |
| 38 | + private final String roof; |
| 39 | + private boolean hasGarage; |
| 40 | + private boolean hasSwimmingPool; |
| 41 | + private boolean hasGarden; |
| 42 | + |
| 43 | + public Builder(String material, String structure, String roof) { |
| 44 | + this.material = material; |
| 45 | + this.structure = structure; |
| 46 | + this.roof = roof; |
| 47 | + } |
| 48 | + |
| 49 | + public Builder hasGarage(boolean hasGarage) { |
| 50 | + this.hasGarage = hasGarage; |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + public Builder hasSwimmingPool(boolean hasSwimmingPool) { |
| 55 | + this.hasGarage = hasSwimmingPool; |
| 56 | + return this; |
| 57 | + } |
| 58 | + |
| 59 | + public Builder hasGarden(boolean hasGarden) { |
| 60 | + this.hasGarden = hasGarden; |
| 61 | + return this; |
| 62 | + } |
| 63 | + |
| 64 | + public House build() { |
| 65 | + return new House(this); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments