Builder model and its application in the StringBuilder

preamble

浅谈 String、StringBuffer、StringBuilder

Builder mode

basic introduction

  • Also known as the builder pattern generator mode, a mode of constructing the object. It can be a complex construction process abstracted object (abstract class), so that different implementations of this abstract process can construct different manifestations (attributes) of the object
  • The builder pattern is a step by step to create a complex object, which allows the user can build them only by specifying the type and content of complex objects, users do not need to know the specific details of the interior of the building

Four role model builder

  • Product (Product role): a concrete product objects
  • Builder (Builder abstract): Create a Product object individual components specified interface / abstract class
  • ConcreteBuilder (particularly builder): implement the interface, build and assemble various components
  • Director (director): build an object using the Builder interface. It is primarily used for creating a complex object. It mainly has two functions, one is: the isolation of the production process and the customer object, the second is: responsible for controlling the production process of product object

Code

First define a Product category (Products)

package com.java.springtest.improve;

// 产品 ——》 Product
public class House {

    private String base;
    private String wall;
    private String roofed;

    public String getBase() {
        return base;
    }

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

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

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

Then define a builder

package com.java.springtest.improve;

// 抽象的建造者
public abstract class HouseBuilder {

    protected House house = new House();

    // 将建造的流程写好,抽象方法
    public abstract void buildBase();
    public abstract void buildWall();
    public abstract void roofed();

    // 建造房子,然后将产品(房子)return
    public House buildHouse() {
        return house;
    }
}

The construction process of an ordinary house

package com.java.springtest.improve;

public class CommonHouse extends HouseBuilder {
    @Override
    public void buildBase() {
        System.out.println("普通房子打地基");
    }

    @Override
    public void buildWall() {
        System.out.println("普通房子砌墙");
    }

    @Override
    public void roofed() {
        System.out.println("普通房子的屋顶");
    }
}

Tower builders

package com.java.springtest.improve;

/**
 * @author Woo_home
 * @create by 2020/2/17
 */
public class HighBuilder extends HouseBuilder {
    @Override
    public void buildBase() {
        System.out.println("高楼打地基");
    }

    @Override
    public void buildWall() {
        System.out.println("高楼砌墙");
    }

    @Override
    public void roofed() {
        System.out.println("高楼的透明屋顶");
    }
}

Director

package com.java.springtest.improve;

// 指挥者,这里动态地去指定制作流程,返回产品
public class HouseDirector {

    HouseBuilder houseBuilder = null;

    // 构造器传入 houseBuilder
    public HouseDirector(HouseBuilder houseBuilder) {
        this.houseBuilder = houseBuilder;
    }

    // 通过 setter 传入 houseBuilder
    public void setHouseBuilder(HouseBuilder houseBuilder) {
        this.houseBuilder = houseBuilder;
    }`在这里插入代码片`

    // 如何处理建造房子的流程,交给指挥者
    public House constructHouse() {
        houseBuilder.buildBase();
        houseBuilder.buildWall();
        houseBuilder.roofed();
        return houseBuilder.buildHouse();
    }
}

Client implementation

package com.java.springtest.improve;

public class Client {
    public static void main(String[] args) {
        // 盖普通房子
        CommonHouse commonHouse = new CommonHouse();
        // 准备创建房子的指挥者
        HouseDirector houseDirector = new HouseDirector(commonHouse);
        // 完成盖房子,返回产品
        houseDirector.constructHouse();

        System.out.println("--------------------------------------------");

        // 盖高楼
        HighBuilder highBuilder = new HighBuilder();

        // 重置建造者
        houseDirector.setHouseBuilder(highBuilder);

        // 完成盖房子,返回产品
        houseDirector.constructHouse();
    }
}

Here Insert Picture Description

Application Builder mode in the StringBuilder

Before we look at the following code, you go

package com.java.springtest.improve;

public class BuilderTest {
    public static void main(String[] args) {
        StringBuilder builder = new StringBuilder("Hello World");
        System.out.println(builder);
    }
}

Output:
Here Insert Picture Description
This code is very simple Ha (as if to say equal did not say), do not worry, step by step. We point to open source StringBuilder
Here Insert Picture Description
found StringBuilder class inherited a AbstractStringBuilder, we look like opening the AbstractStringBuilder found AbstractStringBuilder achieve a Appendable Interface
Here Insert Picture Description

The builder pattern of source code analysis in StringBuilder

  • Appendable interface defines a plurality append method (abstract method), i.e. Appendable abstract builders, defines an abstract method
  • AbstractStringBuilder realized Appendable interface method, AbstractStringBuilder here is already on the builder, but can not be instantiated
  • StringBuilder that is acting as the commanding role, but also serves as a concrete builder, to achieve the construction method is done by AbstractStringBuilder, while StringBuilder inherited AbstractStringBuilder
He published 196 original articles · won praise 961 · views 190 000 +

Guess you like

Origin blog.csdn.net/Woo_home/article/details/104362776