Builder一个对象

一般新建对象我们都是直接new一下

public class GirlFriend {private String name;private int age;// 省略 getter & setter ...public static void main(String[] args) {GirlFriend myGirlFriend = new GirlFriend();myGirlFriend.setName("小美");myGirlFriend.setAge(18);}}没问题,老铁!但如果对象的属性太多,咋办?

public class GirlFriend {private String name;private int age;private int bust;private int waist;private int hips;private List hobby;// 等等等等 ...// 省略 getter & setter ...public static void main(String[] args) {GirlFriend myGirlFriend = new GirlFriend();myGirlFriend.setName("小美");myGirlFriend.setAge(18);myGirlFriend.setBust(33);myGirlFriend.setWaist(23);// 等等等等 ...}}

而Java8通用Builder适用于所有类,不需要改造原来类,不需要 lombok 插件支持。

先看看使用姿势:

public class GirlFriend {// 省略属性 ...// 省略 getter & setter ...

    // 为了演示方便,加几个聚合方法
    public void addHobby(String hobby) {
        this.hobby = Optional.ofNullable(this.hobby).orElse(new ArrayList<>());
        this.hobby.add(hobby);
    }
    public void addGift(String day, String gift) {
        this.gift = Optional.ofNullable(this.gift).orElse(new HashMap<>());
        this.gift.put(day, gift);
    }
    public void setVitalStatistics(int bust, int waist, int hips) {
        this.bust = bust;
        this.waist = waist;
        this.hips = hips;
    }
    public static void main(String[] args) {
        GirlFriend myGirlFriend = Builder.of(GirlFriend::new)
                .with(GirlFriend::setName, "小美")
                .with(GirlFriend::setAge, 18)
                .with(GirlFriend::setVitalStatistics, 33, 23, 33)
                .with(GirlFriend::setBirthday, "2001-10-26")
                .with(GirlFriend::setAddress, "上海浦东")
                .with(GirlFriend::setMobile, "18688888888")
                .with(GirlFriend::setEmail, "pretty-xiaomei@qq.com")
                .with(GirlFriend::setHairColor, "浅棕色带点微卷")
                .with(GirlFriend::addHobby, "逛街")
                .with(GirlFriend::addHobby, "购物")
                .with(GirlFriend::addHobby, "买东西")
                .with(GirlFriend::addGift, "情人节礼物", "LBR 1912女王时代")
                .with(GirlFriend::addGift, "生日礼物", "迪奥烈焰蓝金")
                .with(GirlFriend::addGift, "纪念日礼物", "阿玛尼红管唇釉")
                // 等等等等 ...
                .build();
    }
}
看到了吗!实例化和属性设置在同一条语句执行,链式操作,一路点点点,清爽!

Talk is cheap, show me the code:

/**

  • 通用的 Builder 模式构建器
    *
  • @author: CipherCui
  • @since 2019/8/29
    */
    public class Builder {
    private final Supplier instantiator;
    private List> modifiers = new ArrayList<>();
    public Builder(Supplier instantiator) {
    this.instantiator = instantiator;
    }
    public static Builder of(Supplier instantiator) {
    return new Builder<>(instantiator);
    }
    public Builder with(Consumer1 consumer, P1 p1) {
    Consumer c = instance -> consumer.accept(instance, p1);
    modifiers.add(c);
    return this;
    }
    public Builder with(Consumer2 consumer, P1 p1, P2 p2) {
    Consumer c = instance -> consumer.accept(instance, p1, p2);
    modifiers.add(c);
    return this;
    }
    public Builder with(Consumer3 consumer, P1 p1, P2 p2, P3 p3) {
    Consumer c = instance -> consumer.accept(instance, p1, p2, p3);
    modifiers.add(c);
    return this;
    }
    public T build() {
    T value = instantiator.get();
    modifiers.forEach(modifier -> modifier.accept(value));
    modifiers.clear();
    return value;
    }
    /**
    • 1 参数 Consumer
      */
      @FunctionalInterface
      public interface Consumer1 {
      void accept(T t, P1 p1);
      }
      /**
      ,>
    • 2 参数 Consumer
      */
      @FunctionalInterface
      public interface Consumer2 {
      void accept(T t, P1 p1, P2 p2);
      }
      /**
      ,>
    • 3 参数 Consumer
      */
      @FunctionalInterface
      public interface Consumer3 {
      void accept(T t, P1 p1, P2 p2, P3 p3);
      }
      }
      这个示例最多支持三个参数的设置属性方法,也完全够用了。如果要扩展也很容易,依葫芦画瓢,添加多个参数的Consumer。
      ,>
    ,>
    ,>
    ,>
    ,>
    ,>

参考:http://www.ciphermagic.cn/java8-builder.html

Builder一个对象Builder一个对象 陈晨辰~ 发布了143 篇原创文章 · 获赞 64 · 访问量 12万+ 私信 关注
上一篇:面向对象 封装


下一篇:“直男”与“暖男”的区别——const