Java第八设计模式:建造者设计模式

模式定义:
   将一个复杂对象的创建与表示分离,是的同样的构建过程可以创建不同的表示


应用场景:
   1.需要生成的对象具有复杂的内部结构。
   2.需要生成的对象内部属性本身相互依赖。
   3.与不可变对象(final)配合使用。


优点:
   1.建造者独立,易扩展。
   2.便于控制细节风险。

第一种,复杂的创建方式

public class BuilderTest {
    public static void main(String[] args) {

        //现在传的是默认建造者,如果有其余特殊的建造者,就重新定义一个ProductBuilder的实现类
        ProductBuilder defaultConcreteProductBuilder = new DefaultConcreteProductBuilder();
        Director director = new Director(defaultConcreteProductBuilder);
        Product product = director.makeProduct("productName", "companyName", "part1", "part2", "part3", "part4");
        System.out.println(product);

    }
}

interface ProductBuilder{
    void builderProductName(String productName);
    void builderCompanyName(String companyName);
    void builderPart1(String part1);
    void builderPart2(String part2);
    void builderPart3(String part3);
    void builderPart4(String part4);

    Product builde();
}

//默认的建造者
class DefaultConcreteProductBuilder implements ProductBuilder {
    private String productName;
    private String companyName;
    private String part1;
    private String part2;
    private String part3;
    private String part4;

    @Override
    public void builderProductName(String productName) {
        this.productName = productName;
    }

    @Override
    public void builderCompanyName(String companyName) {
        this.companyName = companyName;
    }

    @Override
    public void builderPart1(String part1) {
        this.part1 = part1;
    }

    @Override
    public void builderPart2(String part2) {
        this.part2 = part2;
    }

    @Override
    public void builderPart3(String part3) {
        this.part3 = part3;
    }

    @Override
    public void builderPart4(String part4) {
        this.part4 = part4;
    }

    @Override
    public Product builde() {
        return new Product(this.productName, this.companyName, this.part1, this.part2, this.part3, this.part4);
    }
}

//建造者
class Director {
    private ProductBuilder builder;

    public Director(ProductBuilder builder) {
        this.builder = builder;
    }

    public Director() {
    }

    public Product makeProduct(String productName, String companyName, String part1, String part2, String part3, String part4){
        builder.builderProductName(productName);
        builder.builderCompanyName(companyName);
        builder.builderPart1(part1);
        builder.builderPart2(part2);
        builder.builderPart3(part3);
        builder.builderPart4(part4);
        return builder.builde();
    }
}

//要建造的对象
class Product{
    private String productName;
    private String companyName;
    private String part1;
    private String part2;
    private String part3;
    private String part4;

    public Product() {
    }

    public Product(String productName, String companyName, String part1, String part2, String part3, String part4) {
        this.productName = productName;
        this.companyName = companyName;
        this.part1 = part1;
        this.part2 = part2;
        this.part3 = part3;
        this.part4 = part4;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    @Override
    public String toString() {
        return "Product{" +
                "productName='" + productName + '\'' +
                ", companyName='" + companyName + '\'' +
                ", part1='" + part1 + '\'' +
                ", part2='" + part2 + '\'' +
                ", part3='" + part3 + '\'' +
                ", part4='" + part4 + '\'' +
                '}';
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getPart1() {
        return part1;
    }

    public void setPart1(String part1) {
        this.part1 = part1;
    }

    public String getPart2() {
        return part2;
    }

    public void setPart2(String part2) {
        this.part2 = part2;
    }

    public String getPart3() {
        return part3;
    }

    public void setPart3(String part3) {
        this.part3 = part3;
    }

    public String getPart4() {
        return part4;
    }

    public void setPart4(String part4) {
        this.part4 = part4;
    }
}
第一种方式

第二种,简单点的创建方式,可结合不可变对象,更常用,可结合链式编程创建对象。

public class BuilderTestSimple {
    public static void main(String[] args) {
        Product2 product2 = new Product2.Builder().productName("xxx").companyName("yyy").part2("bbb").part3("ccc").part4("ddd").build();
        System.out.println(product2);
    }
}

class Product2{ //可以是final class
    private String productName; //可以是不可变的final
    private String companyName;
    private String part1;
    private String part2;
    private String part3;
    private String part4;

    public Product2(String productName, String companyName, String part1, String part2, String part3, String part4) {
        this.productName = productName;
        this.companyName = companyName;
        this.part1 = part1;
        this.part2 = part2;
        this.part3 = part3;
        this.part4 = part4;
    }

    public Product2() {
    }

    //用静态内部类的方式创建建造者
    static class Builder{
        private String productName;
        private String companyName;
        private String part1;
        private String part2;
        private String part3;
        private String part4;

        public Builder productName(String productName){
            this.productName = productName;
            return this;
        }

        public Builder companyName(String companyName){
            this.companyName = companyName;
            return this;
        }

        public Builder part1(String part1){
            this.part1 = part1;
            return this;
        }

        public Builder part2(String part2){
            this.part2 = part2;
            return this;
        }

        public Builder part3(String part3){
            this.part3 = part3;
            return this;
        }

        public Builder part4(String part4){
            this.part4 = part4;
            return this;
        }

        Product2 build(){
            return new Product2(this.productName, this.companyName, this.part1, this.part2, this.part3, this.part4);
        }

    }

    @Override
    public String toString() {
        return "Product2{" +
                "productName='" + productName + '\'' +
                ", companyName='" + companyName + '\'' +
                ", part1='" + part1 + '\'' +
                ", part2='" + part2 + '\'' +
                ", part3='" + part3 + '\'' +
                ", part4='" + part4 + '\'' +
                '}';
    }
}
第二种方式

你可能感兴趣的:(Java第八设计模式:建造者设计模式)