Java Implementation Builder Pattern
The Builder pattern is a creative design pattern that aims to separate the construction process of a complex object from its representation, so that the same construction process can create different representations.
Applicable scenario:
When the construction process of an object is complex and requires multiple steps to be executed in a specific order, the builder pattern can be used. For example, the construction process of a car includes multiple steps such as creating a body, installing an engine, and installing tires, each with a specific sequence.
When the same construction process can create different representations, it is also suitable to use the builder pattern. For example, the construction process of a car can create different types such as sedans, SUVs, sports cars, etc.
Benefits:
The benefits of the builder mode include:
1. Decouple the construction process and representation, making the code structure clearer.
2. Different presentation objects can be created by controlling different builders.
3. The builder pattern can reuse the same build process to build different objects.
Here is a simple Java code example:
//Product class, which is the complex object to be constructed
class Car {
private String engine;
private String tire;
public void setEngine(String engine) {
this.engine = engine;
}
public void setTire(String tire) {
this.tire = tire;
}
@Override
public String toString() {
return "Car{" +
"engine='" + engine + '\'' +
", tire='" + tire + '\'' +
'}';
}
}
//Abstract builder class, defining abstract methods for the construction process
abstract class CarBuilder {
protected Car car;
public void createCar() {
car = new Car();
}
public abstract void buildEngine();
public abstract void buildTire();
public Car getCar() {
return car;
}
}
//Specific builder classes, specific methods for implementing the construction process
class SedanCarBuilder extends CarBuilder {
@Override
public void buildEngine() {
car.setEngine("Sedan Engine");
}
@Override
public void buildTire() {
car.setTire("Sedan Tire");
}
}
class SUVCarBuilder extends CarBuilder {
@Override
public void buildEngine() {
car.setEngine("SUV Engine");
}
@Override
public void buildTire() {
car.setTire("SUV Tire");
}
}
//Commander class, responsible for building objects
class Director {
private CarBuilder carBuilder;
public void setCarBuilder(CarBuilder carBuilder) {
this.carBuilder = carBuilder;
}
public Car construct() {
carBuilder.createCar();
carBuilder.buildEngine();
carBuilder.buildTire();
return carBuilder.getCar();
}
}
//Client code
public class BuilderPatternExample {
public static void main(String[] args) {
Director director = new Director();
CarBuilder sedanCarBuilder = new SedanCarBuilder();
director.setCarBuilder(sedanCarBuilder);
Car sedanCar = director.construct();
System.out.println("Sedan Car: " + sedanCar);
CarBuilder suvCarBuilder = new SUVCarBuilder();
director.setCarBuilder(suvCarBuilder);
Car suvCar = director.construct();
System.out.println("SUV Car: " + suvCar);
}
}
Output results:
Sedan Car: Car{engine='Sedan Engine', tire='Sedan Tire'}
SUV Car: Car{engine='SUV Engine', tire='SUV Tire'}
In the above example, Car is the complex object to be constructed, and CarBuilder is the abstract builder class that defines the abstract methods for the construction process. SedanCarBuilder and SUVCarBuilder are specific builder classes that implement specific methods for the construction process. Director is the director class responsible for building objects. The client code constructs different types of Car objects by setting different builders.