你还在 new 对象吗?Java8 通用 Builder 了解一下?
阅读本文大概需要 3.5 分钟。
来自:rrd.me/gtQTp
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 Listhobby;
private String birthday;
private String address;
private String mobile;
private String email;
private String hairColor;
private Mapgift;
// 等等等等 ...
// 省略 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("2001-10-26");
myGirlFriend.setAddress("上海浦东");
myGirlFriend.setMobile("18688888888");
myGirlFriend.setEmail("pretty-xiaomei@qq.com");
myGirlFriend.setHairColor("浅棕色带点微卷");
Listhobby = new ArrayList<>();
hobby.add("逛街");
hobby.add("购物");
hobby.add("买东西");
myGirlFriend.setHobby(hobby);
Mapgift = new HashMap<>();
gift.put("情人节礼物", "LBR 1912女王时代");
gift.put("生日礼物", "迪奥烈焰蓝金");
gift.put("纪念日礼物", "阿玛尼红管唇釉");
myGirlFriend.setGift(gift);
// 等等等等 ...
}
}
GirlFriend{name='小美'
, age=18
, bust=33
, waist=23
, hips=33
, hobby=[逛街, 购物, 买东西]
, birthday='2001-10-26'
, address='上海浦东'
, mobile='18688888888'
, email='pretty-xiaomei@qq.com'
, hairColor='浅棕色带点微卷'
, gift={情人节礼物=LBR 1912女王时代, 生日礼物=迪奥烈焰蓝金, 纪念日礼物=阿玛尼红管唇釉}
}
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();
}
}
/**
* 通用的 Builder 模式构建器
*
* @author: CipherCui
* @since 2019/8/29
*/
public class Builder{
private final Supplierinstantiator;
private Listmodifiers = new ArrayList<>();
public Builder(Supplierinstantiator){
this.instantiator = instantiator;
}
public staticBuilderof(Supplierinstantiator){
return new Builder<>(instantiator);
}
publicBuilderwith(Consumer1consumer, P1 p1){
Consumerc = instance -> consumer.accept(instance, p1);
modifiers.add(c);
return this;
}
publicBuilderwith(Consumer2consumer, P1 p1, P2 p2){
Consumerc = instance -> consumer.accept(instance, p1, p2);
modifiers.add(c);
return this;
}
publicBuilderwith(Consumer3consumer, P1 p1, P2 p2, P3 p3){
Consumerc = 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);
}
}
推荐阅读:
为什么牛逼的程序员都不用 “ ! = null " 做判空?
Java 项目权威排名:Nacos 未上版,Gradle 排名第二,Maven 排名 28
微信扫描二维码,关注我的公众号
朕已阅
评论