The Builder pattern separates the construction of a complex object from its representation. This allows the same construction process to create different representations of the object.
// The Car class represents a car with various properties such as make, model, year, color, and engine. It uses a builder object to initialize these properties.
class Car {
constructor(builder) {
this.make = builder.make;
this.model = builder.model;
this.year = builder.year;
this.color = builder.color;
this.engine = builder.engine;
}
// Returns a string representation of the car
toString() {
return `${this.year} ${this.make} ${this.model} in ${this.color} with a ${this.engine} engine`;
}
}
The CarBuilder
class helps in building a Car
object step by step. It allows setting various properties of the car and then building the final Car
object.
// CarBuilder class helps in building a Car object step by step
class CarBuilder {
constructor(make, model) {
this.make = make;
this.model = model;
}
// Sets the year of the car
setYear(year) {
this.year = year;
return this;
}
// Sets the color of the car
setColor(color) {
this.color = color;
return this;
}
// Sets the engine type of the car
setEngine(engine) {
this.engine = engine;
return this;
}
// Builds and returns a Car object
build() {
return new Car(this);
}
}
- Fluent Interface: The
CarBuilder
class uses a fluent interface, allowing method chaining. Each setter method returns the builder object itself (this
), enabling chaining multiple method calls in a single statement. - Separation of Concerns: The builder pattern separates the construction of a complex object (
Car
) from its representation, allowing the same construction process to create various representations. - Immutability: Once a
Car
object is created, its properties are immutable (cannot be changed), ensuring the integrity of the object.
const car = new CarBuilder("Toyota", "Camry")
.setYear(2021)
.setColor("Red")
.setEngine("V6")
.build();
// Output the car details
console.log(car.toString());
In this example, the Product
class is constructed using the Builder
class. The Builder
class provides methods to set optional parameters and a build()
method to create the Project
object. The Builder pattern is useful for constructing complex objects with many optional parameters.