|
| 1 | +package org.codefx.demo.effective_java._02_builder_pattern; |
| 2 | + |
| 3 | +// NOTE DSL: |
| 4 | +// The common approach to a builder is to have a single class, on which `build()` can be called |
| 5 | +// at any time. If the provided information was incomplete, a `RuntimeException` is the result. |
| 6 | +// |
| 7 | +// The `CarFactory` in this class follows a more powerful/complex approach where each required |
| 8 | +// "setter" transitions to a new type of builder and only the last one has a `build()` method. |
| 9 | +// This way, other constructors/methods can use types for their parameters that represent a |
| 10 | +// specific "phase" of the build process. The progression from empty to complete builder is thus |
| 11 | +// encoded in the type system, which means the compiler can check errors that would otherwise |
| 12 | +// only be discovered at run time. |
| 13 | +public class TowardsDsl { |
| 14 | + |
| 15 | + public static class Car { |
| 16 | + |
| 17 | + private final Body body; |
| 18 | + private final Spoiler spoiler; |
| 19 | + private final Paint paint; |
| 20 | + private final Decal decal; |
| 21 | + private final Tires tires; |
| 22 | + |
| 23 | + private Car(WithPaintedBodyAndTires builder) { |
| 24 | + this.body = builder.body; |
| 25 | + this.spoiler = builder.spoiler; |
| 26 | + this.paint = builder.paint; |
| 27 | + this.decal = builder.decal; |
| 28 | + this.tires = builder.tires; |
| 29 | + } |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + public static class Body { } |
| 34 | + public static class ConvertibleBody extends Body { } |
| 35 | + |
| 36 | + public static class Spoiler { } |
| 37 | + public static class RoofSpoiler extends Spoiler { } |
| 38 | + public static class RearSpoiler extends Spoiler { } |
| 39 | + |
| 40 | + public static class Paint { } |
| 41 | + |
| 42 | + public static class Decal { } |
| 43 | + |
| 44 | + public static class Tires { } |
| 45 | + |
| 46 | + public static class CarFactory { |
| 47 | + |
| 48 | + // NOTE DSL: This method returns `WithBody` instead of a `CarFactory`. |
| 49 | + public WithBody body(Body body) { |
| 50 | + return new WithBody(body); |
| 51 | + } |
| 52 | + |
| 53 | + public WithConvertibleBody body(ConvertibleBody body) { |
| 54 | + return new WithConvertibleBody(body); |
| 55 | + } |
| 56 | + |
| 57 | + // NOTE self type: |
| 58 | + // There is no `build()` method here, because without body, the car is incomplete. |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + public static class WithBody { |
| 63 | + |
| 64 | + private final Body body; |
| 65 | + private Spoiler spoiler; |
| 66 | + |
| 67 | + public WithBody(Body body) { |
| 68 | + this.body = body; |
| 69 | + } |
| 70 | + |
| 71 | + public WithBody spoiler(Spoiler spoiler) { |
| 72 | + this.spoiler = spoiler; |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + public WithPaintedBody paint(Paint paint) { |
| 77 | + return new WithPaintedBody(body, spoiler, paint); |
| 78 | + } |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + public static class WithConvertibleBody { |
| 83 | + |
| 84 | + private final ConvertibleBody body; |
| 85 | + private RearSpoiler spoiler; |
| 86 | + |
| 87 | + public WithConvertibleBody(ConvertibleBody body) { |
| 88 | + this.body = body; |
| 89 | + } |
| 90 | + |
| 91 | + public WithConvertibleBody spoiler(RearSpoiler spoiler) { |
| 92 | + this.spoiler = spoiler; |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + public WithPaintedBody paint(Paint paint) { |
| 97 | + return new WithPaintedBody(body, spoiler, paint); |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + public static class WithPaintedBody { |
| 103 | + |
| 104 | + private final Body body; |
| 105 | + private final Spoiler spoiler; |
| 106 | + private final Paint paint; |
| 107 | + private Decal decal; |
| 108 | + |
| 109 | + public WithPaintedBody(Body body, Spoiler spoiler, Paint paint) { |
| 110 | + this.body = body; |
| 111 | + this.spoiler = spoiler; |
| 112 | + this.paint = paint; |
| 113 | + } |
| 114 | + |
| 115 | + public WithPaintedBody decal(Decal decal) { |
| 116 | + this.decal = decal; |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + public WithPaintedBodyAndTires tires(Tires tires) { |
| 121 | + return new WithPaintedBodyAndTires(body, spoiler, paint, decal, tires); |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + public static class WithPaintedBodyAndTires { |
| 127 | + |
| 128 | + private final Body body; |
| 129 | + private final Spoiler spoiler; |
| 130 | + private final Paint paint; |
| 131 | + private final Decal decal; |
| 132 | + private final Tires tires; |
| 133 | + |
| 134 | + public WithPaintedBodyAndTires(Body body, Spoiler spoiler, Paint paint, Decal decal, Tires tires) { |
| 135 | + this.body = body; |
| 136 | + this.spoiler = spoiler; |
| 137 | + this.paint = paint; |
| 138 | + this.tires = tires; |
| 139 | + this.decal = decal; |
| 140 | + } |
| 141 | + |
| 142 | + public Car build() { |
| 143 | + return new Car(this); |
| 144 | + } |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments