Today, let's take building a house as an example and tell you about the Java builder model

Posted by imnsi on Fri, 04 Mar 2022 19:46:44 +0100

Absrtact: Builder Pattern, also known as Builder Pattern, is an object construction pattern. It can abstract the construction process of complex objects (abstract categories), so that different implementation methods of this abstract process can construct objects with different representations (attributes).

This article is shared from Huawei cloud community< [Java design mode] explain the builder mode (generator mode) with the case of building a house >Author: I am a cabbage.

Now we need to build houses. The process is piling, building walls and capping. There are all kinds of houses, such as ordinary houses, high-rise buildings, villas. Although the process of all kinds of houses is the same, the requirements are not the same. Please write a program to complete the requirements

Traditional way

1. Class diagram analysis

2. Code analysis

First create an abstract class AbstractHouse, which has the functions of laying foundation, building walls, encapsulating roofs and building houses

public abstract class AbstractHouse {
    //Laying foundation
    public abstract void buildBasic();
    //Build a wall
    public abstract void buildWalls();
    //Encapsulated roof
    public abstract void roofed();
    //Start building a house
    public void build() {
        buildBasic();
        buildWalls();
        roofed();
    }
}

Then I choose the house I want to build. I choose an ordinary house, which corresponds to common house

public class CommonHouse extends AbstractHouse {
    @Override
    public void buildBasic() {
        System.out.println("Ordinary houses began to lay foundations");
    }
    @Override
    public void buildWalls() {
        System.out.println("Ordinary houses have begun to build walls");
    }
    @Override
    public void roofed() {
        System.out.println("Ordinary houses are ready to be capped");
    }
}

Finally, write a class Client to detect the results

public class Client {
    public static void main(String[] args) {
        CommonHouse commonHouse = new CommonHouse();
        commonHouse.build();
    }
}

3. Code results

4. Problem analysis

The advantages are easy to understand, simple and easy to operate.

The disadvantage is that the designed program structure is too simple, there is no cache layer object, and the program expansion and maintenance are not good. In other words, this design scheme encapsulates the product (i.e. house) and the process of creating the product (i.e. house building process), and the coupling is enhanced.

Builder pattern

1. Basic introduction

Builder Pattern, also known as Builder Pattern, is an object construction pattern. It can abstract the construction process of complex objects (abstract categories), so that different implementation methods of this abstract process can construct objects with different representations (attributes).

Builder mode is to create a complex object step by step. It allows users to build complex objects only by specifying their types and contents. Users do not need to know the specific internal construction details.

2. Four roles

  • Product role: a specific product object.
  • Builder (Abstract builder): create an interface / abstract class specified by each part of a Product object.
  • ConcreteBuilder: implement interfaces, build and assemble various components.
  • Director: build an object that uses the Builder interface. It is mainly used to create a complex object. It has two main functions: one is to isolate the production process of customers and objects; the other is to control the production process of product objects

3. Class diagram analysis

4. Code analysis

First create a House class House, which has the functions of laying foundation, building walls and encapsulating the roof

public class House {
    private String baise;
    private String wall;
    private String roofed;
    public String getBaise() {
        return baise;
    }
    public void setBaise(String baise) {
        this.baise = baise;
    }
    public String getWall() {
        return wall;
    }
    public void setWall(String wall) {
        this.wall = wall;
    }
    public String getRoofed() {
        return roofed;
    }
    public void setRoofed(String roofed) {
        this.roofed = roofed;
    }
}

Then create the abstract builder HouseBuilder

public abstract class HouseBuilder {
    public House house = new House();
    //Write the construction process and abstract method
    public abstract void buildBasic();
    public abstract void buildWalls();
    public abstract void roofed();
    //After building the house, return the product (house)
    public House buildHouse() {
        return house;
    }
}

Then create the conductor HouseDirector, dynamically specify the production process and return to the product

public class HouseDirector {
    HouseBuilder houseBuilder = null;
    //Null parameter constructor
    public HouseDirector() {
    }
    //Pass in houseBuilder through setter
    public void setHouseBuilder(HouseBuilder houseBuilder) {
        this.houseBuilder = houseBuilder;
    }
    //How to deal with the process of building a house and leave it to the commander
    public House constructorHouse() {
        houseBuilder.buildBasic();
        houseBuilder.buildWalls();
        houseBuilder.roofed();
        return houseBuilder.buildHouse();
    }
}

I just built an ordinary house. Now I want to build a high-rise building

public class HighBuilding extends HouseBuilder {
    @Override
    public void buildBasic() {
        System.out.println("High rise houses began to lay foundations");
    }
    @Override
    public void buildWalls() {
        System.out.println("The walls of high-rise houses have begun to be built");
    }
    @Override
    public void roofed() {
        System.out.println("The high-rise house is ready to be capped");
    }
}

Finally, write a class to test

public class Client {
    public static void main(String[] args) {
        //The commander ready to create the house
        HouseDirector houseDirector = new HouseDirector();
        //Build a tall building
        HighBuilding highBuilding = new HighBuilding();
        //Reset builder
houseDirector.setHouseBuilder(highBuilding);
        //Finish building the house and return to the product (high-rise building)
        houseDirector.constructorHouse();
    }
}

5. Code result

6. Precautions

  • Each specific builder is relatively independent and has nothing to do with other specific builders. Therefore, it is convenient to replace or add new specific builders. Users can get different product objects by using different specific builders
  • You can more finely control the product creation process. The creation steps of complex products are decomposed into different methods, which makes the creation process clearer and more convenient to use programs to control the creation process
  • Adding a new specific builder does not need to modify the code of the original class library. The commander class is programmed for the abstract builder class, which is convenient for system expansion and conforms to the "opening and closing principle"
  • The products created by the builder mode generally have more in common and their components are similar. If there are great differences between products, it is not suitable to use the builder mode, so its scope of use is limited.
  • If the internal changes of the product are complex, it may lead to the need to define many specific builder classes to realize this change, resulting in the system becoming very large. Therefore, in this case, it is necessary to consider whether to choose the builder mode

summary

So what's the difference between the abstract factory model and the builder model?

Abstract factory mode realizes the creation of product families. A product family is a series of products: product combinations with different classification dimensions. Adopting abstract factory mode does not need to care about the construction process, but only about what products and factories are produced by. The builder model requires the product to be built according to the specified blueprint. Its main purpose is to produce a new product by assembling spare parts.

 

Click follow to learn about Huawei's new cloud technology for the first time~

Topics: Java Design Pattern