Builder model

intentions

  • The main purpose of the Builder pattern, also known as the generator pattern, is to enable users to build complete products step by step, allowing for the existence of an abstraction buffer layer and decoupling the product from the production process.

Builder mode character

There are four main categories of Builder mode characters:

  • Product: is the final generated object. Products built by different generators need not belong to the same class hierarchy or interface.
  • Concrete Builders: Provide different implementations of the construction process. Concrete generators can also construct products that do not follow a common interface.
  • Abstract Builder: The interface declares product construction steps common to all type generators.
  • Director: The class defines the order in which construction steps are invoked so that you can create and reuse specific product configurations.

– Client: used on one end of the product

The sample

  1. Purpose: Build a house using builder mode
  2. Analysis role: First of all, a house is a Product of ours. We need to build an abstract Builder to define some abstract methods for building a house. After that, we need to build Concrete Builders to realize the abstract methods defined by the abstract Builders. It is up to the commander to decide whether to produce the build.

The class diagram

Code implementation

House.java

/ * * *@ClassName: House
 * @Description: House -> product *@Author: a3818
 * @Date: 2021/6/3 his *@Version 1.0
 * @Copyright* * /
@Data
public class House {
    /** * Some attributes of House */
    /** ** color */
    private String color;
    /** ** */
    private String rebar;
    /** * House size */
    private String length;
    /** * house width */
    private String width;

    /** * defines the specific builder *@return* /
    public static AbstractHouseBuilder build(a){
        // Note that producers must be different to produce different products
        AbstractHouseBuilder abstractHouseBuilder =  new HouseBuilder();
        abstractHouseBuilder.setHouse(new House());
        returnabstractHouseBuilder; }}Copy the code

AbstractHouseBuilder.java

/ * * *@ClassName: AbstractHouseBudier
 * @Description: AbstractHouseBuilder -> Abstract producer *@Author: a3818
 * @Date: 2021/6/3 11:09
 * @Version 1.0
 * @Copyright* * /
@Data
public abstract class AbstractHouseBuilder {
    /** * production object */
    protected House house = null;

    /** * Define an abstract method that returns the AbstractHouseBuilder object */
    public abstract AbstractHouseBuilder color(String col);
    public abstract AbstractHouseBuilder rebar(String reb);
    public abstract AbstractHouseBuilder length(String len);
    public abstract AbstractHouseBuilder width(String wid);


}

Copy the code

HouseBuilder.java


/ * * *@ClassName: HouseBuild
 * @Description: HouseBuild -> Builder *@Author: a3818
 * @Date: 2021/6/3 o *@Version 1.0
 * @Copyright* * /
public class HouseBuilder extends AbstractHouseBuilder{

    @Override
    public AbstractHouseBuilder color(String col) {
        house.setColor(col);
        return this;
    }

    @Override
    public AbstractHouseBuilder rebar(String reb) {
        house.setRebar(reb);
        return this;
    }

    @Override
    public AbstractHouseBuilder length(String len) {
        house.setLength(len);
        return this;
    }

    @Override
    public AbstractHouseBuilder width(String wid) {
        house.setWidth(wid);
        return this; }}Copy the code

HouseBuild.java

/ * * *@ClassName: HouseBuilder
 * @Description: HouseBuilder -> Commander *@Author: a3818
 * @Date: "2021/6/3 *@Version 1.0
 * @Copyright* * /
@Data
@AllArgsConstructor
@NoArgsConstructor
public class HouseBuild {
    private AbstractHouseBuilder abstractHouseBuilder;
}

Copy the code

Client.java


/ * * *@ClassName: Client
 * @Description: client -> Client *@Author: a3818
 * @Date: 2021/6/3 pronounce *@Version 1.0
 * @Copyright* * /
public class Client {

    public static void main(String[] args) {
        AbstractHouseBuilder color = House.build().width("10 meters").rebar("Reinforced concrete").length("10 meters").color("White");
        AbstractHouseBuilder color2 = House.build().width("15 meters").rebar("Cement reinforcement").length("15 meters").color("Pink"); System.out.println(color.house); System.out.println(color2.house); }}Copy the code

The test results

House(color= white, rebar= rebar, length=10M, width =10(color= pink, rebar= concrete, length=15M, width =15M.)Copy the code