@[toc]

introduce

  1. Builder Pattern, also called generator Pattern, is an object construction Pattern, which can abstract the construction process of replication, so that different implementation methods of this abstract process can construct different attributes
  2. The Builder pattern creates a replica object step by step, allowing the user to build the replica object simply by specifying its type and content, without specifying the details

  • Product(Product role): A concrete Product object
  • Builder: Creates an unspecified interface/abstract class for a Product object
  • ConcreteBuilder: Implement interfaces, construct and assemble components
  • Diretor (director) : To build an object using the Builder interface, it is mainly used to create a complex object, it mainly has two functions, one is: the separation of the customer and the object’s birth process, the other is: responsible for controlling the production process of the product object.

case

Building needs:

  1. Houses need to be built, and the process is piling, walls, and roofing
  2. All kinds of houses, such as ordinary room, high-rise, villa, all kinds of house process although the same, but not the same requirements

Create an abstract class AbstractHouse

public abstract class AbstractHouse {

    / / play ground
    public abstract void buildBasic(a);

    / / build by laying bricks or stones wall
    public abstract void buildWalls(a);

    / / cap
    public abstract void roofed(a);

    public void build(a) { buildBasic(); buildWalls(); roofed(); }}Copy the code

Building a normal house

public class CommonHouse extends AbstractHouse{
    @Override
    public void buildBasic(a) {
        System.out.println("Lay the foundation...");
    }

    @Override
    public void buildWalls(a) {
        System.out.println("Build a wall...");
    }

    @Override
    public void roofed(a) {
        System.out.println("Capping..."); }}Copy the code

Test the Clinet

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

Traditional solution

  1. Advantages easy to understand, simple
  2. The design of the program structure, too simple, there is no design of the cache layer object, the expansion and maintenance of the program is not good, that is to say, this design, the coupling degree enhanced

Use the Builder pattern

New House Products

@Data
public class House {
    private String baise;
    private String wall;
    private String roofed;

}
Copy the code

An abstract class HouseBuilder

public abstract class HouseBuilder {

    protected House house = new House();

    / / play ground
    public abstract void buildBasic(a);

    / / build by laying bricks or stones wall
    public abstract void buildWalls(a);

    / / cap
    public abstract void roofed(a);

    // Build a house
    public House buildHouse(a) {
        returnhouse; }}Copy the code

Ordinary houses

public class CommonHouse extends HouseBuilder {
    @Override
    public void buildBasic(a) {
        System.out.println("Foundation 5m...");
    }

    @Override
    public void buildWalls(a) {
        System.out.println("Wall 5m...");
    }

    @Override
    public void roofed(a) {
        System.out.println("Capped at 5m..."); }}Copy the code

Tall buildings

public class HignBuilding extends HouseBuilder{
    @Override
    public void buildBasic(a) {
        System.out.println("High-rise foundation 100m");
    }

    @Override
    public void buildWalls(a) {
        System.out.println("High-rise wall 100m");
    }

    @Override
    public void roofed(a) {
        System.out.println("Tall buildings capped at 100m"); }}Copy the code

commander

public class HouseDirector {
    HouseBuilder houseBuilder = null;

    // Constructor passed in
    public HouseDirector(HouseBuilder houseBuilder){
        this.houseBuilder = houseBuilder;
    }

    / / setter incoming
    public void setHouseBuilder(HouseBuilder houseBuilder){
        this.houseBuilder = houseBuilder;
    }


    // How to handle the process of building a house
    public House constructHouse(a){
        houseBuilder.buildBasic();
        houseBuilder.buildWalls();
        houseBuilder.roofed();
        returnhouseBuilder.buildHouse(); }}Copy the code

test

public class Client {
    public static void main(String[] args) {
        // An ordinary house
        CommonHouse commonHouse = new CommonHouse();
        HouseDirector houseDirector = new HouseDirector(commonHouse);
        House house = houseDirector.constructHouse();

        HignBuilding hignBuilding = new HignBuilding();
        HouseDirector houseDirector2 = newHouseDirector(hignBuilding); houseDirector2.constructHouse(); }}Copy the code

Advantages and disadvantages

Advantages:

1) Encapsulation. The client does not have to know the details of the product's internal composition. 2) Easy to control detailed risks. The build process can be progressively refined without any impact on other modules.Copy the code

Disadvantages:

1) If the internal changes are complex, there will be many builder classes. 2) Products must have something in common and have a limited scope.Copy the code

Applicable scenarios:

1) Multiple components or parts can be assembled into an object, but produce different results. 2) when the object needs to be generated with complex internal structure.Copy the code


The Builder pattern is used in our JDK in java.lang.StringBuilder



Github Demo address: ~ ~ ~ portal ~ ~ ~

Personal blog address: blog.yanxiaolu.cn /