0%

设计模式之构建型模式(四):建造者模式

1. 建造者模式概述

  • 概念

    • 用于创建过程稳定,但配置多变的对象,主要通过链式调用生成不同的配置
    • 《设计模式》一书中的定义:将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示
  • 特点

    • 将类的构造方法设为私有,所以外部不能通过 new 构建出类的实例,只能通过 Builder 构建
    • 对于必须配置的属性,通过 Builder 的构造方法传入;可选的属性通过 Builder 的链式调用方法传入。如果不配置,将使用默认配置
  • 优点

    • 不用担心忘了指定某个配置,保证了构建过程是稳定的(必须配置的属性通过 Builder 的构造方法传入)
    • OkHttpRetrofit 等著名框架的源码中都使用到了建造者模式
  • Demo

    • 珍珠奶茶类

      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
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      	public class MilkTea {
      private final String type;
      private final String size;
      private final boolean pearl;
      private final boolean ice;

      private MilkTea() {}

      private MilkTea(Builder builder) {
      this.type = builder.type;
      this.size = builder.size;
      this.pearl = builder.pearl;
      this.ice = builder.ice;
      }

      public String getType() {
      return type;
      }

      public String getSize() {
      return size;
      }

      public boolean isPearl() {
      return pearl;
      }
      public boolean isIce() {
      return ice;
      }

      public static class Builder {

      private final String type;
      private String size = "中杯";
      private boolean pearl = true;
      private boolean ice = false;

      public Builder(String type) {
      this.type = type;
      }

      public Builder size(String size) {
      this.size = size;
      return this;
      }

      public Builder pearl(boolean pearl) {
      this.pearl = pearl;
      return this;
      }

      public Builder ice(boolean cold) {
      this.ice = cold;
      return this;
      }

      public MilkTea build() {
      return new MilkTea(this);
      }
      }
      }
    • 调用者

      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
      29
      30
      31
      32
      33
      34
      35
      36
      public class User {
      private void buyMilkTea() {
      MilkTea milkTea = new MilkTea.Builder("原味").build();
      show(milkTea);

      MilkTea chocolate =new MilkTea.Builder("巧克力味")
      .ice(false)
      .build();
      show(chocolate);

      MilkTea strawberry = new MilkTea.Builder("草莓味")
      .size("大杯")
      .pearl(false)
      .ice(true)
      .build();
      show(strawberry);
      }

      private void show(MilkTea milkTea) {
      String pearl;
      if (milkTea.isPearl())
      pearl = "加珍珠";
      else
      pearl = "不加珍珠";
      String ice;
      if (milkTea.isIce()) {
      ice = "加冰";
      } else {
      ice = "不加冰";
      }
      System.out.println("一份" + milkTea.getSize() + "、"
      + pearl + "、"
      + ice + "的"
      + milkTea.getType() + "奶茶");
      }
      }
-------------------- 本文结束感谢您的阅读 --------------------