你还在new对象吗?Java8通用Builder了解一下?
程序员经常会遇到灵魂拷问:你有对象吗?
没有,但我可以 new 一个!
1public class GirlFriend {
2
3 private String name;
4 private int age;
5
6 // 省略 getter & setter ...
7
8 public static void main(String[] args) {
9 GirlFriend myGirlFriend = new GirlFriend();
10 myGirlFriend.setName("小美");
11 myGirlFriend.setAge(18);
12 }
13}
没问题,老铁!但如果对象的属性太多,咋办?
1public class GirlFriend {
2
3 private String name;
4 private int age;
5 private int bust;
6 private int waist;
7 private int hips;
8 private List<String> hobby;
9 private String birthday;
10 private String address;
11 private String mobile;
12 private String email;
13 private String hairColor;
14 private Map<String, String> gift;
15 // 等等等等 ...
16
17 // 省略 getter & setter ...
18
19 public static void main(String[] args) {
20 GirlFriend myGirlFriend = new GirlFriend();
21 myGirlFriend.setName("小美");
22 myGirlFriend.setAge(18);
23 myGirlFriend.setBust(33);
24 myGirlFriend.setWaist(23);
25 myGirlFriend.setHips(33);
26 myGirlFriend.setBirthday("2001-10-26");
27 myGirlFriend.setAddress("上海浦东");
28 myGirlFriend.setMobile("18688888888");
29 myGirlFriend.setEmail("pretty-xiaomei@qq.com");
30 myGirlFriend.setHairColor("浅棕色带点微卷");
31 List<String> hobby = new ArrayList<>();
32 hobby.add("逛街");
33 hobby.add("购物");
34 hobby.add("买东西");
35 myGirlFriend.setHobby(hobby);
36 Map<String, String> gift = new HashMap<>();
37 gift.put("情人节礼物", "LBR 1912女王时代");
38 gift.put("生日礼物", "迪奥烈焰蓝金");
39 gift.put("纪念日礼物", "阿玛尼红管唇釉");
40 myGirlFriend.setGift(gift);
41 // 等等等等 ...
42 }
43}
1GirlFriend{name='小美'
2, age=18
3, bust=33
4, waist=23
5, hips=33
6, hobby=[逛街, 购物, 买东西]
7, birthday='2001-10-26'
8, address='上海浦东'
9, mobile='18688888888'
10, email='pretty-xiaomei@qq.com'
11, hairColor='浅棕色带点微卷'
12, gift={情人节礼物=LBR 1912女王时代, 生日礼物=迪奥烈焰蓝金, 纪念日礼物=阿玛尼红管唇釉}
13}
GirlFriend 是很美,但写起来也太麻烦了吧。
说说缺点:实例化和设置属性分开,不好维护;变量名重复写。
莫慌,看法宝~
这里不再介绍其他 Builder 实现方式,直接祭出最实用的通用Builder:
适用于所有类,不需要改造原来类,不需要 lombok 插件支持。
先看看使用姿势:
1public class GirlFriend {
2
3 // 省略属性 ...
4
5 // 省略 getter & setter ...
6
7 // 为了演示方便,加几个聚合方法
8 public void addHobby(String hobby) {
9 this.hobby = Optional.ofNullable(this.hobby).orElse(new ArrayList<>());
10 this.hobby.add(hobby);
11 }
12
13 public void addGift(String day, String gift) {
14 this.gift = Optional.ofNullable(this.gift).orElse(new HashMap<>());
15 this.gift.put(day, gift);
16 }
17
18 public void setVitalStatistics(int bust, int waist, int hips) {
19 this.bust = bust;
20 this.waist = waist;
21 this.hips = hips;
22 }
23
24 public static void main(String[] args) {
25 GirlFriend myGirlFriend = Builder.of(GirlFriend::new)
26 .with(GirlFriend::setName, "小美")
27 .with(GirlFriend::setAge, 18)
28 .with(GirlFriend::setVitalStatistics, 33, 23, 33)
29 .with(GirlFriend::setBirthday, "2001-10-26")
30 .with(GirlFriend::setAddress, "上海浦东")
31 .with(GirlFriend::setMobile, "18688888888")
32 .with(GirlFriend::setEmail, "pretty-xiaomei@qq.com")
33 .with(GirlFriend::setHairColor, "浅棕色带点微卷")
34 .with(GirlFriend::addHobby, "逛街")
35 .with(GirlFriend::addHobby, "购物")
36 .with(GirlFriend::addHobby, "买东西")
37 .with(GirlFriend::addGift, "情人节礼物", "LBR 1912女王时代")
38 .with(GirlFriend::addGift, "生日礼物", "迪奥烈焰蓝金")
39 .with(GirlFriend::addGift, "纪念日礼物", "阿玛尼红管唇釉")
40 // 等等等等 ...
41 .build();
42 }
43}
看到了吗!实例化和属性设置在同一条语句执行,链式操作,一路点点点,清爽!
Talk is cheap, show me the code:
1/**
2 * 通用的 Builder 模式构建器
3 *
4 * @author: CipherCui
5 * @since 2019/8/29
6 */
7public class Builder<T> {
8
9 private final Supplier<T> instantiator;
10 private List<Consumer<T>> modifiers = new ArrayList<>();
11
12 public Builder(Supplier<T> instantiator) {
13 this.instantiator = instantiator;
14 }
15
16 public static <T> Builder<T> of(Supplier<T> instantiator) {
17 return new Builder<>(instantiator);
18 }
19
20 public <P1> Builder<T> with(Consumer1<T, P1> consumer, P1 p1) {
21 Consumer<T> c = instance -> consumer.accept(instance, p1);
22 modifiers.add(c);
23 return this;
24 }
25
26 public <P1, P2> Builder<T> with(Consumer2<T, P1, P2> consumer, P1 p1, P2 p2) {
27 Consumer<T> c = instance -> consumer.accept(instance, p1, p2);
28 modifiers.add(c);
29 return this;
30 }
31
32 public <P1, P2, P3> Builder<T> with(Consumer3<T, P1, P2, P3> consumer, P1 p1, P2 p2, P3 p3) {
33 Consumer<T> c = instance -> consumer.accept(instance, p1, p2, p3);
34 modifiers.add(c);
35 return this;
36 }
37
38 public T build() {
39 T value = instantiator.get();
40 modifiers.forEach(modifier -> modifier.accept(value));
41 modifiers.clear();
42 return value;
43 }
44
45 /**
46 * 1 参数 Consumer
47 */
48 @FunctionalInterface
49 public interface Consumer1<T, P1> {
50 void accept(T t, P1 p1);
51 }
52
53 /**
54 * 2 参数 Consumer
55 */
56 @FunctionalInterface
57 public interface Consumer2<T, P1, P2> {
58 void accept(T t, P1 p1, P2 p2);
59 }
60
61 /**
62 * 3 参数 Consumer
63 */
64 @FunctionalInterface
65 public interface Consumer3<T, P1, P2, P3> {
66 void accept(T t, P1 p1, P2 p2, P3 p3);
67 }
68}
这个示例最多支持三个参数的设置属性方法,也完全够用了。如果要扩展也很容易,依葫芦画瓢,添加多个参数的Consumer。
快用你的 Builder 建个对象吧~
