关于jav:Java中的建造者模式

7次阅读

共计 3016 个字符,预计需要花费 8 分钟才能阅读完成。

建造者模式简介

  • 模式属于创立型模式,它提供了一种创建对象的最佳形式。
  • 应用多个简略的对象一步一步构建成一个简单的对象。
  • 一个 Builder 类会一步一步结构最终的对象。该 Builder 类是独立于其余对象的。

指挥者模式

产品类 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='提前第三步工序'}
正文完
 0