Add Comment
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();
Anonymous 29 October 2024 4:46:39
SDA