设计模式-建造者模式

- 建造者模式

1.需要生成的对象具有复杂的内部结构。

2.需要生成的对象内部属性本身相互依赖。

3.与不可变对象配合使用。

 

/**
 * 建造者模式
 */
public class Producttest2 {
    public static void main(String[] args) {
//        Product product = new Product.Builder().productName("productA").companyName("AT").part1("part1").part2("part2").build();
        Product.Builder builder = new Product.Builder().productName("productA").companyName("AT").part1("part1").part2("part2");
        Product product = builder.part4("part4").build();
        System.out.println(product);

    }
}

class Product{
    private final String productName;
    private final String companyName;
    private final String part1;
    private final String part2;
    private final String part3;
    private final String part4;

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



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

    //.....


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

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

    }
}

 

posted @ 2021-05-18 23:22  炼金术士0z  阅读(45)  评论(0编辑  收藏  举报