美文网首页
建造者模式

建造者模式

作者: Stroman | 来源:发表于2018-10-13 12:38 被阅读33次

它有啥用

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

类图

建造者模式.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();
    }
}

相关文章

  • Builder Pattern in Java

    建造者模式:建造者模式定义建造者模式应用场景实现案例Jdk中的建造者模式建造者模式的优点建造者模式的缺点 建造者模...

  • 设计模式(4) 建造者模式

    什么是建造者模式 经典建造者模式的优缺点 对建造者模式的扩展 什么是建造者模式 建造者模式将一个复杂的对象的构建与...

  • 建造者模式(部件构造)

    目录 建造者模式的理念 从 POJO 到建造者模式的思考 怎么来实现建造者模式 建造者模式在Android源码中的...

  • 【设计模式】之建造者Builder模式

    建造者模式 什么是建造者模式? 建造者模式属于创建型模式的一员,可以控制对象的实例化过程。建造者模式简化了复杂对象...

  • 建造者模式

    一、建造者模式介绍 二、建造者模式代码实例

  • 建造者模式

    建造者模式 首先,建造者模式的封装性很好。使用建造者模式可以有效的封装变化,在使用建造者模式的场景中,一般产品类和...

  • 建造者模式:深入理解建造者模式 ——组装复杂的实例

    目录: 一 建造者模式介绍 1.1 定义 1.2 为什么要用建造者模式(优点)? 1.3 哪些情况不要用建造者模式...

  • 设计模式之建造者模式

    设计模式之建造者模式 Intro 简介 建造者模式: 建造者模式隐藏了复杂对象的创建过程,它把复杂对象的创建过程加...

  • 一、设计模式(构建模式)——03建造模式与原型模式

    建造者模式 建造型模式用于创建过程稳定,但配置多变的对象。 建造模式的实现要点: 在建造者模式中,指挥者是直接与客...

  • 创建型模式:建造者模式

    个人公众号原文:创建型模式:建造者模式 五大创建型模式之四:建造者模式。 简介 姓名 :建造者模式 英文名 :Bu...

网友评论

      本文标题:建造者模式

      本文链接:https://www.haomeiwen.com/subject/wlqoaftx.html