Builder mode of design mode

Basic introduction

  1. Also known as generator pattern, it is an object construction pattern. It can abstract the complex construction process (abstract category), so that different implementation methods of this abstract factory can construct objects with different representations (properties)
  2. 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 specify the internal specific construction process

Four roles

  1. Product role: a specific product object
  2. Builder (Abstract builder): create an interface specified by each part of a Product object
  3. Concrete Builder: implement interfaces, build and assemble various components
  4. Director: build an object using the Builder interface, which 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

scene

There are several steps to build a house, and each house has different ways. First, look at the realization under the traditional mode

Traditional mode
First, create a House abstract class to abstract the process and methods of House construction

public abstract class AbstractHouse {

    public abstract void buildBasic();
    public abstract void buildWalls();
    public abstract void roofed();
	// Steps to build a house
    public void build(){
        buildBasic();
        buildWalls();
        roofed();
    }

}

Create a concrete implementation

public class CommonHouse extends AbstractHouse{
    @Override
    public void buildBasic() {
        System.out.println("Laying foundation for ordinary house");
    }

    @Override
    public void buildWalls() {
        System.out.println("Ordinary house walling");
    }

    @Override
    public void roofed() {
        System.out.println("Ordinary house capping");
    }
}

Client operation

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

The advantages are that it is easy to understand, easy to operate, 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 is encapsulated with the process of creating products, and the coupling is enhanced
Solution: decouple the product from the product construction process = "builder mode"

realization


First look at this url map. If you can't understand it, please learn from Baidu by yourself.

code implementation
First create a product class

public class House {
    private String basic;

    private String wall;
    
    private String roofed;

    public String getBasic() {
        return basic;
    }

    public void setBasic(String basic) {
        this.basic = basic;
    }

    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;
    }
}

Create a builder

public abstract class HouseBuilder {
    protected House house = new House();

    public abstract void builderBasic();
    public abstract void builderWalls();
    public abstract void builderRoofed();

    public House build(){
        return house;
    }
}

Create specific builders

public class CommonHouse extends HouseBuilder{
    @Override
    public void builderBasic() {
        System.out.println("Laying foundation for ordinary house");
    }

    @Override
    public void builderWalls() {
        System.out.println("Ordinary house walling");
    }

    @Override
    public void builderRoofed() {
        System.out.println("The roof of an ordinary house");
    }
}
public class HighBuilding extends HouseBuilder{
    @Override
    public void builderBasic() {
        System.out.println("The foundation of high-rise building is 100m");
    }

    @Override
    public void builderWalls() {
        System.out.println("Building wall 20 m");
    }

    @Override
    public void builderRoofed() {
        System.out.println("The roof of a tall building");
    }
}

Create Commander

public class HouseDirector {

    private HouseBuilder houseBuilder;
    // Constructor incoming

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

    public HouseBuilder getHouseBuilder() {
        return houseBuilder;
    }

    // set method passed in
    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 constractHouse(){
        houseBuilder.builderBasic();
        houseBuilder.builderWalls();
        houseBuilder.builderRoofed();
        return houseBuilder.build();
    }
}

Client operation

public class Cilent {
    public static void main(String[] args) {
        // Ordinary house
        CommonHouse commonHouse = new CommonHouse();
        // The commander ready to create the house
        HouseDirector houseDirector = new HouseDirector(commonHouse);
        // Return product
        House house = houseDirector.constractHouse();
    }
}

Precautions and details

  1. The client does not need to know the details of the internal composition of the product, and decouples the product itself from the product creation process, so that the same creation process can create different product objects
  2. 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
  3. You can more finely control the product creation process. Decomposing the steps of complex product creation process into different methods can not only make the creation process clearer, but also make it easier to use programs to control the creation process
  4. 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

Tags: Java Design Pattern

Posted on Sun, 12 Sep 2021 17:29:30 -0400 by DataRater