建造者模式

它有啥用

它用于解决,通过变化无常的方式,来组织相对不变的构成要素,形成一个整体对象的问题。再强调一遍,变化的是方式,不变的是构成要素。
这种模式就是把方式和构成要素分开处理了。
其中指挥员负责不同的组装方式。
其实这就好比木炭和金刚石都是碳元素的单质,只是因为分子结构不同结果形成了不同的物质一样。

类图

建造者模式.png

效果

组件0由SomeBuilder制造
组件1由SomeBuilder制造
组件2由SomeBuilder制造
生产产品
组件2由SomeBuilder制造
组件1由SomeBuilder制造
组件0由SomeBuilder制造
生产产品

Process finished with exit code 0

使用

package com.company;

public class Main {

    public static void main(String[] args) {
    // write your code here
        Director0 director0 = new Director0();
        director0.firstKindOfProduct(new SomeBuilder());

        Director1 director1 = new Director1();
        director1.secondKindOfProduct(new SomeBuilder());
    }
}

通用建造者

package com.company;

public interface CommonBuilder {
    void makeComponent0();
    void makeComponent1();
    void makeComponent2();
    Product gainProduct();
}

产品

package com.company;

public class Product {
    private String component0;
    private String component1;
    private String component2;

    public String getComponent0() {
        return component0;
    }

    public void setComponent0(String component0) {
        this.component0 = component0;
    }

    public String getComponent1() {
        return component1;
    }

    public void setComponent1(String component1) {
        this.component1 = component1;
    }

    public String getComponent2() {
        return component2;
    }

    public void setComponent2(String component2) {
        this.component2 = component2;
    }
}

某个具体的建造者

package com.company;

public class SomeBuilder implements CommonBuilder {
    private Product product;

    public SomeBuilder() {
        this.product = new Product();
    }

    @Override
    public void makeComponent0() {
        this.product.setComponent0("组件0由SomeBuilder制造");
        System.out.println(this.product.getComponent0());
    }

    @Override
    public void makeComponent1() {
        this.product.setComponent1("组件1由SomeBuilder制造");
        System.out.println(this.product.getComponent1());
    }

    @Override
    public void makeComponent2() {
        this.product.setComponent2("组件2由SomeBuilder制造");
        System.out.println(this.product.getComponent2());
    }

    @Override
    public Product gainProduct() {
        System.out.println("生产产品");
        return this.product;
    }
}

指挥者

package com.company;

public class Director0 {
    public Product firstKindOfProduct(CommonBuilder builder) {
        builder.makeComponent0();
        builder.makeComponent1();
        builder.makeComponent2();
        return builder.gainProduct();
    }
}

另一个指挥者

package com.company;

public class Director1 {
    public Product secondKindOfProduct(CommonBuilder builder) {
        builder.makeComponent2();
        builder.makeComponent1();
        builder.makeComponent0();
        return builder.gainProduct();
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 那些海水里的石头,几乎每一块都有独特的形状和花纹,点点滴滴,丝丝缕缕,俯身其中,令人沉醉,然而,当人贪婪地把他们全...
    紫色风铃8089阅读 179评论 0 0
  • 最近关注了一下河间一中的周报,觉得河间一中的变化越来越大,发展也越来越好。恩,很自豪。 最近讲到了“哲将翘材”。这...
    爱青狮的木小姐阅读 176评论 0 0
  • 当我睁开双眼时,我发现我躺在一艘船上,这是一艘很普通的木质船,但是当我真正向远处望时,我发现我在一片沙漠中,而这艘...
    jpy1002阅读 264评论 0 0
  • 九月的季节对于军训中的学子来说,是一个闷热的季节,除去烈暑酷日,汗流浃背的六月,九月的军训是每一个学子的必经之...
    山绵阅读 1,420评论 0 3