The of Java design pattern -- builder pattern

1. What is the builder model

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Separate the construction of a complex object from its representation, so that the same construction process can create different representations.

Speaking: decouple the process of constructing complex objects from the components that make up the objects. Just like saving a computer, no matter what brand of accessories, just You can install it; Similarly, the same accessories can be assembled in many ways. More directly, the same class constructs different objects through different internal attribute states.

2. Builder mode definition

In the builder mode, there are four definitions:

① Product: the class object we need to generate

② . Builder (Abstract builder class): build different module attributes for the class objects we need to generate, that is, disclose the attributes of the product class and hide other functions of the product class.

③ Concretebuilder (concrete builder class): implement all methods defined by the abstract class and return a group of built objects.

④ Director: determine the specific module attributes of the class object we build. In practical application, this role can be directly processed through the client without this role.

3. Instance

For example, when we buy a car, there are usually multiple versions of the same car. According to different configurations, it is divided into classic version, comfort version, luxury version, etc. for example, the classic version is a manual seat, and the comfort version is an automatic panoramic sunroof.

public class Car {
    // Car name
    private String name;
    // automatic catch
    private String automaticCatch;
    // Manual transmission
    private String manualTransmission;
    // Panoramic sunroof 
    private String panoramicSunroof;
    // Automatic seat
    private String automaticSeat;
    // Manual seat
    private String manualSeat;
    // Reversing image
    private String reversingImage;

    //Omit the get/set method
}

3.1 general implementation

public class GeneralTest {
    public Car getCarInstance(String carName){
        Car car = new Car();
        if("Classic Edition".equals(carName)){
            car.setName("Classic Edition");
            car.setManualTransmission("Manual transmission");
        }else if("Comfort Edition".equals(carName)){
            car.setName("Comfort Edition");
            car.setAutomaticCatch("automatic catch");
            car.setManualSeat("Manual seat");
        }else if("Deluxe Edition".equals(carName)){
            car.setName("Deluxe Edition");
            car.setAutomaticCatch("automatic catch");
            car.setAutomaticSeat("Automatic seat");
            car.setReversingImage("Reversing image");
            car.setPanoramicSunroof("Panoramic sunroof ");
        }else{
            throw new IllegalArgumentException("carName is error: carName="+carName);
        }
        return car;
    }
}

Note that it does not mean that the high version of the car must have all the configurations of the low version. For example, the comfortable version is equipped with manual seats, the luxury version is equipped with automatic seats, and there are cross configurations between different versions.

It can be seen from the above that we can create cars with corresponding configurations according to the car type, but in fact, there are many car configurations. Once there are many types, the above code will look bloated and difficult to maintain, so we use the builder mode to reconstruct.

3.2 realization of builder mode

① Create an abstract builder class

public abstract class CarBuilder {

    public abstract Car buildClassic();

    public abstract Car buildComfortable();

    public abstract Car buildLuxury();

}

② Create a specific builder class. Assign the implementation value to the abstract method of the abstract builder class to achieve the results we need.

public class CarConcreteBuilder extends CarBuilder{

    @Override
    public Car buildClassic() {
        Car car = new Car();
        car.setName("Classic Edition");
        car.setManualTransmission("Manual transmission");
        return car;
    }

    @Override
    public Car buildComfortable() {
        Car car = new Car();
        car.setName("Comfort Edition");
        car.setAutomaticCatch("automatic catch");
        car.setManualSeat("Manual seat");
        return car;
    }

    @Override
    public Car buildLuxury() {
        Car car = new Car();
        car.setName("Deluxe Edition");
        car.setAutomaticCatch("automatic catch");
        car.setAutomaticSeat("Automatic seat");
        car.setReversingImage("Reversing image");
        car.setPanoramicSunroof("Panoramic sunroof ");
        return car;
    }
}

③ Create our director class and guide us how to create objects

public class CarDirector {
    private CarBuilder carBuilder;

    public CarDirector(CarBuilder carBuilder){
        this.carBuilder = carBuilder;
    }

    public Car classicConstruct(){
        return carBuilder.buildClassic();
    }

    public Car comfortableConstruct(){
        return carBuilder.buildComfortable();
    }

    public Car luxuryConstruct(){
        return carBuilder.buildLuxury();
    }
}

④ , test

public class BuilderTest {
    public static void main(String[] args) {
        CarBuilder carBuilder = new CarConcreteBuilder();
        CarDirector carDirector = new CarDirector(carBuilder);
        Car classicCar = carDirector.classicConstruct();
        System.out.println(classicCar);
        Car comfortableCar = carDirector.comfortableConstruct();
        System.out.println(comfortableCar);
    }
}

In addition, there is a mutually exclusive relationship between different configurations. For example, if there is a manual gear, there can be no automatic gear. The relationship between attributes can also be set in the specific builder class CarConcreteBuilder.

4. Difference from factory mode

When we introduced the factory pattern, we said that the factory class is responsible for creating objects, and the builder pattern also allows the specific builder class to be responsible for creating objects. What's the difference between the two?

In fact, the factory pattern is used to create objects of different but related types (a group of subclasses inheriting the same parent class or interface). The given parameters determine which type of object to create. The builder mode is used to create a type of complex object. Different objects are created "customized" by setting different optional parameters.

There is an example that can be explained vividly:

When customers order in a restaurant, we use the factory model to make different foods according to users' different choices, such as pizza, hamburger and salad. For pizza, users can customize various ingredients, such as cheese, tomato and cheese. We make pizza according to different ingredients selected by users through the builder mode.

5. Advantages of builder mode

① . encapsulation

Using the builder mode can make the client do not need to know the details of the internal composition of the product. For example, in the example, we do not need to care about how the interior of each version of the Car is implemented, and the generated object type is Car.

② The builder is independent and easy to expand

In fact, for the CarConcreteBuilder class in the above example, there are three methods to construct classic, comfort and luxury cars respectively. The interior of each method is not affected, or we can directly split this class into three classes.

③ . easy to control detail risk

Since each builder's process is independent, the process can be more detailed without affecting other modules.

6. Builder mode usage scenario

① When the same method, different execution sequences and different event results are generated, the builder mode can be adopted.

② , multiple assemblies or parts can be assembled into one object, but the operation results are different, you can use this mode.

③ The product class is very complex, or the calling order in the product class is different, resulting in different efficiency. At this time, the builder mode is very appropriate.

Posted on Mon, 22 Nov 2021 05:44:25 -0500 by swr