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

New Comment

Comment
You can use these formatting tags: [b]bold[/b] [i]italic[/i] [u]underline[/u] [url]www.example.com[/url] [code]some code[/code] [quote]quoted text[/quote] [list]one list item per line[/list]