Here is the “usual” implementation of this design pattern.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
package pt.joaobrito.builder; /** * This is the main class */ public class BuilderExample { public static void main(String[] args) { Vehicle v1 = new Client(new CarBuilder()).constructVehicle(); System.out.println(v1); Vehicle v2 = new Client(new BikeBuilder()).constructVehicle(); System.out.println(v2); Vehicle v3 = new Client(new CarBuilder()).constructVehicleWithoutWindowsOrSeats(); System.out.println(v3); Vehicle v4 = new Client(new BikeBuilder()).constructVehicleWithoutWindowsOrSeats(); System.out.println(v4); } } /** * The Client class has the responsability of building the vehicles */ class Client { private final VehicleBuilder vehicleBuilder; public Client(VehicleBuilder vehicleBuilder) { this.vehicleBuilder = vehicleBuilder; } /** * Builds a complete vehicle * @return a vehicle */ public Vehicle constructVehicle() { return vehicleBuilder.buildType().buildSeats().buildWindows().get(); } /** * Builds a vehicle without parts * @return a vehicle */ public Vehicle constructVehicleWithoutWindowsOrSeats() { return vehicleBuilder.buildType().get(); } } /** * This class is the abstract builder */ abstract class VehicleBuilder { protected Vehicle vehicle; public VehicleBuilder() { this.vehicle = new Vehicle(); } /** * Vehicle get method * @return the vehicle */ public Vehicle get() { return vehicle; } /** * The 3 following lines are the methods that must be implemented on the concrete builders * @return */ public abstract VehicleBuilder buildType(); public abstract VehicleBuilder buildSeats(); public abstract VehicleBuilder buildWindows(); } /** * This is a concrete builder for Bikes */ class BikeBuilder extends VehicleBuilder { @Override public VehicleBuilder buildType() { vehicle.setType("Bike"); return this; } @Override public VehicleBuilder buildWindows() { vehicle.setWindows(0); return this; } @Override public VehicleBuilder buildSeats() { vehicle.setSeats(2); return this; } } /** * This is the concrete builder for Cars */ class CarBuilder extends VehicleBuilder { @Override public VehicleBuilder buildType() { vehicle.setType("Car"); return this; } @Override public VehicleBuilder buildSeats() { vehicle.setSeats(5); return this; } @Override public VehicleBuilder buildWindows() { vehicle.setWindows(4); return this; } } /** * This is the vehicle class */ class Vehicle { private String type; private int seats; private int windows; public void setType(String type) { this.type = type; } public void setSeats(int seats) { this.seats = seats; } public void setWindows(int windows) { this.windows = windows; } /** * @return a printable vehicle */ @Override public String toString() { return "Vehicle[Type: " + type + "; Seats: " + seats + "; Windows: " + windows + "]"; } } |