Radio

一个小小程序员

0%

建造者模式

建造者模式

建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

一个 Builder 类会一步一步构造最终的对象。该 Builder 类是独立于其他对象的。

StringBuilder就是典型的建造者模式

示例演示:
  1. 这次我们的类会变的复杂一些,我们还是老规矩,先创建一个Organism接口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    package test.builder;

    /**
    * 所有生物的标识
    */
    public interface Organism {
    //所有生物共有的一个功能,有自己的沟通方式
    String say();
    }
  2. 我们先不创建people和animal,我们先创建一个条目,让这个条目来融合Organism,同时我们新增两个属性

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package test.builder;

    public abstract class PeopleItem implements Item {
    @Override
    public Organism organism() {
    return new People();
    }

    @Override
    public abstract String name();

    @Override
    public abstract Integer Height();
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package test.builder;

    public abstract class AnimalItem implements Item {
    @Override
    public Organism organism() {
    return new Animal();
    }

    @Override
    public abstract String name();

    @Override
    public abstract Integer Height();
    }
  3. 两个抽象的条目创建好了,我们只实现其中的Organism,名字和身高,我们让具体类去实现,我们创建两个人,两个动物分别实现名字和身高。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package test.builder;

    public class PeopleItemOne extends PeopleItem {

    @Override
    public String name() {
    return "my name is peopleOne";
    }

    @Override
    public Integer Height() {
    return 100;
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package test.builder;

    public class PeopleItemTwo extends PeopleItem {

    @Override
    public String name() {
    return "my name is peopleTwo";
    }

    @Override
    public Integer Height() {
    return 200;
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    package test.builder;

    public class AnimalItemOne extends AnimalItem {
    @Override
    public String name() {
    return "my name is animalOne";
    }

    @Override
    public Integer Height() {
    return 50;
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    package test.builder;

    public class AnimalItemTwo extends AnimalItem {
    @Override
    public String name() {
    return "my name is animalTwo";
    }

    @Override
    public Integer Height() {
    return 100;
    }
    }
  4. 我们创建一个家庭成员的类,让他帮我们归纳一下哪些人,哪些动物属于哪个家庭,做一下统计

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    package test.builder;

    import java.util.ArrayList;
    import java.util.List;

    public class Family {
    private List<Item> items = new ArrayList<>();

    public void addItem(Item item) {
    items.add(item);
    }

    public Integer getHeight() {
    Integer height = 0;
    for (Item item : items) {
    height += item.Height();
    }
    return height;
    }

    public void showItems() {
    for (Item item : items) {
    System.out.println("Item: " + item.name());
    System.out.println("say:" + item.organism().say());
    System.out.println("height:" + item.Height());
    }
    }
    }
  5. 方便起见,我们创建两个家庭,分别设置一些成员

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    package test.builder;

    public class FamilyBuilder {

    public Family familyOne() {
    Family family = new Family();
    family.addItem(new PeopleItemOne());
    family.addItem(new PeopleItemTwo());
    family.addItem(new AnimalItemOne());
    return family;
    }

    public Family familyTwo() {
    Family family = new Family();
    family.addItem(new PeopleItemTwo());
    family.addItem(new AnimalItemTwo());
    return family;
    }
    }
  6. 完成上述步骤,我们就可以开始演示了

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    package test.builder;

    public class BuilderDemo {
    public static void main(String[] args) {
    FamilyBuilder familyBuilder = new FamilyBuilder();
    Family familyOne = familyBuilder.familyOne();
    familyOne.showItems();
    System.out.println("身高和:" + familyOne.getHeight());
    System.out.println("---------------------------------");
    Family familyTwo = familyBuilder.familyTwo();
    familyTwo.showItems();
    familyTwo.getHeight();
    System.out.println("身高和:" + familyTwo.getHeight());
    }
    }

    运行结果:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    Item: my name is peopleOne
    say:people say
    height:100
    Item: my name is peopleTwo
    say:people say
    height:200
    Item: my name is animalOne
    say:animal say
    height:50
    身高和:350
    ---------------------------------
    Item: my name is peopleTwo
    say:people say
    height:200
    Item: my name is animalTwo
    say:animal say
    height:100
    身高和:300
说明:

优点:1、建造者独立,易扩展。 2、便于控制细节风险。

缺点: 1、产品必须有共同点,范围有限制。 2、如内部变化复杂,会有很多的建造类。

欢迎关注我的其它发布渠道