Thousands of miles short step, thousands of rivers; Make a little progress every day, and one day you’ll be a big man

All source code is here :github.com/z573419235/…

Hey, guys, count on a star

preface

The builder mode is used to instantiate a complex entity class. When you instantiate a class with a lot of construction parameters, you can use the Builder mode to simplify the instantiation process. In the previous articles on factory model, we talked about buying a car. That was simply a factory car. We didn’t care how the factory was built. But the actual factory to build a car needs steering wheel, engine, frame, tires and other parts, and different brands of car parts are different, although the parts are different, but the way of building the car is basically the same steps, at this time you can use the builder model to build a car;

Builder mode consists of four elements: product, abstract Builder, concrete Builder and commander

Tuhao friend driving factory

After buying a car last time, my tuhao friend found that making and selling cars made a lot of money, so he decided to get involved in the automobile industry. He really has a good business mind, and deserves to be my tuhao friend who talks like crazy and speaks at an amazing speed.

One day, I went to ask him for some information about automobiles. He told me the general structure of automobiles:

/ * *

* The automotive product category defines what constitutes a vehicle

* * /


@Data

public class Car {

    / * *

* the steering wheel

* * /


    private String steering;

    / * *

* engine

* * /


    private String engine;

    / * *

* frame

* * /


    private String frame;

    / * *

* the tyres

* * /


    private String tire;

    / * *

* Show the car specs

* * /


    public String show(a) {

        return "{" +

                "steering='" + steering + '\' ' +

                ", engine='" + engine + '\' ' +

                ", frame='" + frame + '\' ' +

                ", tire='" + tire + '\' ' +

                '} ';

    }

}

Copy the code

Really is roughly ah, cheat me don’t know car is, tell me 4 things, this who don’t know ah, hum! Tuhao friends busy explanation: this is not for easy to understand!! Ha-ha-ha-tuhao friends embarrassed and do not break polite smile!

Calculate calculate calculate, did not dispute with you, you again and I tell next your car factory make car mode!! He said that at the beginning he was too tired to build a car and had to do everything by himself. Later, he recruited two experts, one responsible for BMW and one responsible for Mercedes. What car I want now, I just command who makes the car. Easy as pie;

He introduced me to two of his experts:

/ * *

* BMW builder

* * /


public class BMWBuilder extends AbstractBuild {

    @Override

    void buildEngine(a) {

        car.setEngine("BMW engine.");

    }



    @Override

    void buildSteering(a) {

        car.setSteering("BMW steering Wheel.");

    }



    @Override

    void buildFrame(a) {

        car.setFrame("A BMW frame.");

    }



    @Override

    void buildTire(a) {

        car.setTire("BMW tires.");

    }

}

Copy the code
/ * *

* Mercedes car builder

* * /


public class BenzBuilder extends AbstractBuild {

    @Override

    void buildEngine(a) {

        car.setEngine("Mercedes engine.");

    }



    @Override

    void buildSteering(a) {

        car.setSteering("Mercedes Steering Wheel.");

    }



    @Override

    void buildFrame(a) {

        car.setFrame("Mercedes-benz frame.");

    }



    @Override

    void buildTire(a) {

        car.setTire("Mercedes Tires.");

    }

}

Copy the code

Both of them follow thisAbstractBuildRules for the construction of automobiles:

/ * *

* The abstract builder defines how to build a car

* * /


abstract class AbstractBuild {

    / * *

* The product is car

* * /


    protected Car car=new Car();

    / * *

* Build engines

* * /


    abstract void buildEngine(a);

    / * *

* build tire

* * /


    abstract void buildSteering(a);

    / * *

* build frame

* * /


    abstract void buildFrame(a);

    / * *

* build tire

* * /


    abstract void buildTire(a);

    / * *

* Get a finished vehicle

* * /


    public Car getCar(a){

        return this.car;

    }

}

Copy the code

My tuhao friend also told me how he directed them to build cars:

/ * *

* All builders should listen to the contractor and build whatever he tells you to build

* * /


public class Boss {



    public static Car builderCar(AbstractBuild build){

        build.buildEngine();

        build.buildFrame();

        build.buildSteering();

        build.buildTire();

        return build.getCar();

    }

}

Copy the code

After he this turn show off, feel although long person touch dog kind, do things really a set of, ha ha ha ha!!

Then he showed me how cars are made… .

conclusion

Actually builders models and factories or the like, model builders in the builder is equivalent to the factory in the factory pattern, but is the core of the builder can control sequence, such as the construction of the local tyrants boss can control the construction workers above order, can control they are first made tires or build engine, this is the builder pattern meaning;

If the builder pattern is paired with the template method pattern, the builder class encapsulates a template method and opens it up to the boss, who can directly control that class, then it’s no different from the factory pattern, right