The Builder design pattern is a creational pattern that separates the construction of a complex object from its representation, allowing the same construction process to create different representations of the object. The pattern is particularly useful when dealing with objects that have multiple, optional parameters or configurations.
In the Builder pattern, a separate Builder class is used to construct the object step-by-step, providing methods for each optional parameter or configuration. The Builder class is responsible for constructing the final object, and provides a method for returning the constructed object to the client.
Here is an example implementation of the Builder pattern in Java:
public class Car {
private String make;
private String model;
private int year;
private boolean hasSunroof;
private boolean hasSpoiler;
private Car(Builder builder) {
this.make = builder.make;
this.model = builder.model;
this.year = builder.year;
this.hasSunroof = builder.hasSunroof;
this.hasSpoiler = builder.hasSpoiler;
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
public boolean hasSunroof() {
return hasSunroof;
}
public boolean hasSpoiler() {
return hasSpoiler;
}
public static class Builder {
private String make;
private String model;
private int year;
private boolean hasSunroof;
private boolean hasSpoiler;
public Builder(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
public Builder hasSunroof(boolean hasSunroof) {
this.hasSunroof = hasSunroof;
return this;
}
public Builder hasSpoiler(boolean hasSpoiler) {
this.hasSpoiler = hasSpoiler;
return this;
}
public Car build() {
return new Car(this);
}
}
}
public class Client {
public static void main(String[] args) {
Car car1 = new Car.Builder("Toyota", "Camry", 2020)
.hasSunroof(true)
.hasSpoiler(false)
.build();
Car car2 = new Car.Builder("Ford", "Mustang", 2021)
.hasSunroof(true)
.hasSpoiler(true)
.build();
}
}
In this example, we have a Car class that has five private member variables: make, model, year, hasSunroof, and hasSpoiler. The Car class also has a private constructor that takes a Builder object as its argument. The Builder object is used to construct the Car object step-by-step.
The Car class also has a nested Builder class that has methods for setting the optional member variables. The Builder class also has a build() method that returns the constructed Car object.
In the Client class, we create two Car objects using the Builder pattern. We create a Car object named car1 with a make of “Toyota”, a model of “Camry”, a year of 2020, a sunroof, and no spoiler. We create a Car object named car2 with a make of “Ford”, a model of “Mustang”, a year of 2021, a sunroof, and a spoiler.
The Builder pattern provides several advantages. First, it separates the construction of the object from its representation, allowing the same construction process to create different representations of the object. Second, it provides a way to construct objects step-by-step, which can be useful when dealing with complex objects that have many optional parameters or configurations. Third, it can improve code readability and maintainability by providing a clean, intuitive interface for constructing objects.