class Car {
private String color;
private String engineType;
private String transmission;
// Private constructor
private Car(CarBuilder builder) {
this.color = builder.color;
this.engineType = builder.engineType;
this.transmission = builder.transmission;
}
public static class CarBuilder {
public CarBuilder setColor(String color) {
this.color = color;
return this;
public CarBuilder setEngineType(String engineType) {
this.engineType = engineType;
public CarBuilder setTransmission(String transmission) {
this.transmission = transmission;
public Car build() {
return new Car(this);
// Usage:
Car car = new Car.CarBuilder()
.setColor("Red")
.setEngineType("V8")
.setTransmission("Automatic")
.build();
Reply To: SDA
class Car {
private String color;
private String engineType;
private String transmission;
// Private constructor
private Car(CarBuilder builder) {
this.color = builder.color;
this.engineType = builder.engineType;
this.transmission = builder.transmission;
}
public static class CarBuilder {
private String color;
private String engineType;
private String transmission;
public CarBuilder setColor(String color) {
this.color = color;
return this;
}
public CarBuilder setEngineType(String engineType) {
this.engineType = engineType;
return this;
}
public CarBuilder setTransmission(String transmission) {
this.transmission = transmission;
return this;
}
public Car build() {
return new Car(this);
}
}
}
// Usage:
Car car = new Car.CarBuilder()
.setColor("Red")
.setEngineType("V8")
.setTransmission("Automatic")
.build();
Anonymous 29 October 2024 4:46:39