程序员地瓜哥的小屋

Java中的建造者模式

2021-12-28  本文已影响0人  CodingDGSun

建造者模式简介

指挥者模式

产品类Product
//产品
public class Product {
    private String one;
    private String two;
    private String three;

    public String getOne() {
        return one;
    }

    public void setOne(String one) {
        this.one = one;
    }

    public String getTwo() {
        return two;
    }

    public void setTwo(String two) {
        this.two = two;
    }

    public String getThree() {
        return three;
    }

    public void setThree(String three) {
        this.three = three;
    }

    @Override
    public String toString() {
        return "Product{" +
                "one='" + one + '\'' +
                ", two='" + two + '\'' +
                ", three='" + three + '\'' +
                '}';
    }
}
抽象类Builder
//抽象的建造者
public abstract class Builder {
    abstract void one();//第一步工序

    abstract void two();//第二步工序

    abstract void three();//第三步工序

    abstract Product getProduct();//完工,得到产品
}
子类Worker
//具体的建造者:工人
public class Worker extends Builder {
    private Product product;

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

    @Override
    void one() {
        product.setOne("第一步工序");
        System.out.println("第一步工序");
    }

    @Override
    void two() {
        product.setTwo("第二步工序");
        System.out.println("第二步工序");
    }

    @Override
    void three() {
        product.setThree("第三步工序");
        System.out.println("第三步工序");
    }

    @Override
    Product getProduct() {
        return product;
    }
}
指挥者类
//指挥者
public class Director {
    //指挥工人生成产品
    public Product build(Builder builder){
        builder.one();
        builder.two();
        builder.three();

        return builder.getProduct();
    }
}
测试类
public class Test {
    public static void main(String[] args) {
        //指挥者
        Director director = new Director();
        //指挥工人生产产品
        Product product = director.build(new Worker());
        System.out.println(product);

        /**
         * 输出结果:
         * 第一步工序
         * 第二步工序
         * 第三步工序
         * Product{one='第一步工序', two='第二步工序', three='第三步工序'}
         */
    }
}
测试结果
第一步工序
第二步工序
第三步工序
Product{one='第一步工序', two='第二步工序', three='第三步工序'}

内部类模式

产品类Product
//产品
public class Product {
    private String one = "第一步工序";
    private String two = "第二步工序";
    private String three = "第三步工序";

    public String getOne() {
        return one;
    }

    public void setOne(String one) {
        this.one = one;
    }

    public String getTwo() {
        return two;
    }

    public void setTwo(String two) {
        this.two = two;
    }

    public String getThree() {
        return three;
    }

    public void setThree(String three) {
        this.three = three;
    }

    @Override
    public String toString() {
        return "Product{" +
                "one='" + one + '\'' +
                ", two='" + two + '\'' +
                ", three='" + three + '\'' +
                '}';
    }
}
抽象类Builder
//抽象的建造者
public abstract class Builder {
    abstract Builder one(String mes);//第一步工序

    abstract Builder two(String mes);//第二步工序

    abstract Builder three(String mes);//第三步工序

    abstract Product getProduct();//完工,得到产品
}
子类Worker
//具体的建造者:工人
public class Worker extends Builder {
    private Product product;

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

    @Override
    Builder one(String mes) {
        product.setOne(mes);
        return this;
    }

    @Override
    Builder two(String mes) {
        product.setTwo(mes);
        return this;
    }

    @Override
    Builder three(String mes) {
        product.setThree(mes);
        return this;
    }

    @Override
    Product getProduct() {
        return product;
    }
}
测试类
public class Test {
    public static void main(String[] args) {
        //工人
        Worker worker = new Worker();
        //生产产品
        Product product = worker.getProduct();
        System.out.println(product.toString());
        /**
         * 输出结果:
         * Product{one='第一步工序', two='第二步工序', three='第三步工序'}
         */

        //链式编程
        product = worker.one("第一步额外工序").three("提前第三步工序").getProduct();
        System.out.println(product);
        /**
         * 输出结果:
         * Product{one='第一步额外工序', two='第二步工序', three='提前第三步工序'}
         */
    }
}
测试结果
Product{one='第一步工序', two='第二步工序', three='第三步工序'}
Product{one='第一步额外工序', two='第二步工序', three='提前第三步工序'}
上一篇下一篇

猜你喜欢

热点阅读