你还在new对象吗?Java8通用Builder了解一下?

2020-11-03 10:53:41 浏览数 (1)

你知道的越多,不知道的就越多,业余的像一棵小草!

你来,我们一起精进!你不来,我和你的竞争对手一起精进!

编辑:业余草

来源:ciphermagic.cn/java8-builder.html

推荐:https://www.xttblog.com/?p=5118

你还在new对象吗?Java8通用Builder了解一下?

程序员经常会遇到灵魂拷问:你有对象吗?

没有,但我可以 new 一个!

代码语言:javascript复制
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);
    }
}

没问题,老铁!但如果对象的属性太多,咋办?

代码语言:javascript复制
public class GirlFriend {
    private String name;
    private int age;
    private int bust;
    private int waist;
    private int hips;
    private List<String> hobby;
    private String birthday;
    private String address;
    private String mobile;
    private String email;
    private String hairColor;
    private Map<String, String> gift;
    // 等等等等 ...
    // 省略 getter & setter ...
    public static void main(String[] args) {
        GirlFriend myGirlFriend = new GirlFriend();
        myGirlFriend.setName("业余草");
        myGirlFriend.setAge(18);
        myGirlFriend.setBust(33);
        myGirlFriend.setWaist(23);
        myGirlFriend.setHips(33);
        myGirlFriend.setBirthday("2020-10-29");
        myGirlFriend.setAddress("业余草科技大厦");
        myGirlFriend.setMobile("18688888888");
        myGirlFriend.setEmail("taoge@xttblog.com");
        myGirlFriend.setHairColor("浅棕色格子衫");
        List<String> hobby = new ArrayList<>();
        hobby.add("写bug");
        hobby.add("改bug");
        hobby.add("背锅");
        myGirlFriend.setHobby(hobby);
        Map<String, String> gift = new HashMap<>();
        gift.put("情人节礼物", "机械键盘");
        gift.put("生日礼物", "格子衫");
        gift.put("纪念日礼物", "防脱发洗发精");
        myGirlFriend.setGift(gift);
        // 等等等等 ...
    }
}

很好办,使用 Lombok 的 @Accessors(chain = true) 就可以简化一下了!

代码语言:javascript复制
@Data
@Accessors(chain = true)
public class GirlFriend {
    private String name;
    private int age;
    private int bust;
    private int waist;
    private int hips;
    private List<String> hobby;
    private String birthday;
    private String address;
    private String mobile;
    private String email;
    private String hairColor;
    private Map<String, String> gift;
    // 等等等等 ...
    // 省略 getter & setter ...
    public static void main(String[] args) {
        GirlFriend myGirlFriend = new GirlFriend();
        myGirlFriend.setName("业余草")
        .setAge(18)
        .setBust(33)
        .setWaist(23)
        .setHips(33)
        .setBirthday("2020-10-29")
        .setAddress("业余草科技大厦")
        .setMobile("18688888888")
        .setEmail("taoge@xttblog.com")
        .setHairColor("浅棕色格子衫");
        List<String> hobby = new ArrayList<>();
        hobby.add("写bug");
        hobby.add("改bug");
        hobby.add("背锅");
        myGirlFriend.setHobby(hobby);
        Map<String, String> gift = new HashMap<>();
        gift.put("情人节礼物", "机械键盘");
        gift.put("生日礼物", "格子衫");
        gift.put("纪念日礼物", "防脱发洗发精");
        myGirlFriend.setGift(gift);
        // 等等等等 ...
    }
}

上面的代码在类上加了 @Accessors(chain = true) 注解后,生成后的 set 方法返回值并不是 void,而是 this。

使用 @Accessors(chain = true) 之后看起来是很美,但写起来也麻烦。而且呢,不少人使用 Lombok 翻过车,详情看这篇文中《记一次使用 Lombok 翻车造成的事故!》。况且,后面还有 jdk11 。。。

但是你也别慌,看法宝~

这里不再介绍其他 Builder 实现方式,直接祭出最实用的通用Builder:

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

先看看使用姿势:

代码语言:javascript复制
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:

代码语言:javascript复制
/**
 * 通用的 Builder 模式构建器
 */
public class Builder<T> {
    private final Supplier<T> instantiator;
    private List<Consumer<T>> modifiers = new ArrayList<>();
    public Builder(Supplier<T> instantiator) {
        this.instantiator = instantiator;
    }
    public static <T> Builder<T> of(Supplier<T> instantiator) {
        return new Builder<>(instantiator);
    }
    public <P1> Builder<T> with(Consumer1<T, P1> consumer, P1 p1) {
        Consumer<T> c = instance -> consumer.accept(instance, p1);
        modifiers.add(c);
        return this;
    }
    public <P1, P2> Builder<T> with(Consumer2<T, P1, P2> consumer, P1 p1, P2 p2) {
        Consumer<T> c = instance -> consumer.accept(instance, p1, p2);
        modifiers.add(c);
        return this;
    }
    public <P1, P2, P3> Builder<T> with(Consumer3<T, P1, P2, P3> consumer, P1 p1, P2 p2, P3 p3) {
        Consumer<T> 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<T, P1> {
        void accept(T t, P1 p1);
    }
    /**
     * 2 参数 Consumer
     */
    @FunctionalInterface
    public interface Consumer2<T, P1, P2> {
        void accept(T t, P1 p1, P2 p2);
    }
    /**
     * 3 参数 Consumer
     */
    @FunctionalInterface
    public interface Consumer3<T, P1, P2, P3> {
        void accept(T t, P1 p1, P2 p2, P3 p3);
    }
}

这个示例最多支持三个参数的设置属性方法,也完全够用了。如果要扩展也很容易,依葫芦画瓢,添加多个参数的 Consumer。

这样,就又可以在同事面前装 X 了。怎么样,学到了吗?快使用本文的 Builder 建个对象吧~

0 人点赞