建造者模式 普通式


package com.life.design.builder;

public abstract class Builder {
    public abstract void buildPartA();
    public abstract void buildPartB();
    public abstract void buildPartC();
    public abstract Product getProduct();
}
package com.life.design.builder;

public class ConcreteBuilder extends Builder {
    private Product product;

    public ConcreteBuilder() {
        product = new Product();
    }

    @Override
    public void buildPartA() {
        product.setPartA("地基");
    }

    @Override
    public void buildPartB() {
        product.setPartB("扎钢筋");
    }

    @Override
    public void buildPartC() {
        product.setPartC("浇筑");
    }

    @Override
    public Product getProduct() {
       return product;
    }
}
package com.life.design.builder;

public class Product {
    private String partA = "地基";
    private String partB = "扎钢筋";
    private String partC = "浇筑";

    public String getPartA() {
        return partA;
    }

    public void setPartA(String partA) {
        this.partA = partA;
    }

    public String getPartB() {
        return partB;
    }

    public void setPartB(String partB) {
        this.partB = partB;
    }

    public String getPartC() {
        return partC;
    }

    public void setPartC(String partC) {
        this.partC = partC;
    }

    @Override
    public String toString() {
        return "Product{" +
                "partA='" + partA + '\'' +
                ", partB='" + partB + '\'' +
                ", partC='" + partC + '\'' +
                '}';
    }
}
package com.life.design.builder;

public class Director {
    public Product construct(Builder builder) {//也可用通过setter设置Builder
        builder.buildPartA();
        builder.buildPartB();
        builder.buildPartC();
        return builder.getProduct();
    }
}