Brief introduction of builder mode

Keywords: Java

This paper introduces the builder pattern and java code implementation demo. The content is for reference only. Please point out the shortcomings in time and welcome to exchange and discuss.

Builder pattern

Builder pattern is a kind of creation pattern, which hides the specific process of building objects.

scene

Suppose you need a house, and building a house requires a lot of process and work, but you don't need to care about it. You just need to tell the contractor the house you want, and the contractor will build the house. This scenario can use builder mode.

Example

Suppose the house consists of foundation, frame, wall and top.

public class House {

    private String base;
    private String frame;
    private String wall;
    private String top;

    public String getBase() {
        return base;
    }

    public void setBase(String base) {
        this.base = base;
    }

    public String getFrame() {
        return frame;
    }

    public void setFrame(String frame) {
        this.frame = frame;
    }

    public String getWall() {
        return wall;
    }

    public void setWall(String wall) {
        this.wall = wall;
    }

    public String getTop() {
        return top;
    }

    public void setTop(String top) {
        this.top = top;
    }

    @Override
    public String toString() {
        return "House{" +
                "base='" + base + '\'' +
                ", frame='" + frame + '\'' +
                ", wall='" + wall + '\'' +
                ", top='" + top + '\'' +
                '}';
    }
}

House building abstract class

abstract class HouseBuilder {

    protected House house = new House();

    public abstract void buildBase();

    public abstract void buildFrame();

    public abstract void buildWall();

    public abstract void buildTop();
}

Bungalow construction

public class BungalowHouseBuilder extends HouseBuilder {

    @Override
    public void buildBase() {
        house.setBase("Cottage Foundation");
    }

    @Override
    public void buildFrame() {
        house.setFrame("Cottage framework");
    }

    @Override
    public void buildWall() {
        house.setWall("Bungalow wall");
    }

    @Override
    public void buildTop() {
        house.setTop("Top of Bungalow");
    }
}

Villa construction

public class VillaHouseBuilder extends HouseBuilder {

    @Override
    public void buildBase() {
        house.setBase("Villa Foundation");
    }

    @Override
    public void buildFrame() {
        house.setFrame("Villa framework");
    }

    @Override
    public void buildWall() {
        house.setWall("Villa wall");
    }

    @Override
    public void buildTop() {
        house.setTop("Villa top");
    }
}

Director of building construction

public class HouseBuildDirector {

    private HouseBuilder houseBuilder;

    public HouseBuildDirector(HouseBuilder houseBuilder) {
        this.houseBuilder = houseBuilder;
    }

    public House buildHose() {

        houseBuilder.buildBase();
        houseBuilder.buildFrame();
        houseBuilder.buildWall();
        houseBuilder.buildTop();
        return houseBuilder.house;
    }
}

Inform the contractor to build a bungalow.

BungalowHouseBuilder bungalowHouseBuilder = new BungalowHouseBuilder();
HouseBuildDirector houseBuildDirector = new HouseBuildDirector(bungalowHouseBuilder);
House house = houseBuildDirector.buildHose();

System.out.println(house.toString());

output

House{base='Cottage Foundation', frame='Cottage framework', wall='Bungalow wall', top='Top of Bungalow'}

Builder mode structure

The builder mode is mainly divided into two modules: Builder and Director.

Builder is responsible for building each module.

Director is responsible for building each module.

Posted by castor_troy on Mon, 30 Dec 2019 08:33:07 -0800