程式設計師經常會遇到靈魂拷問:你有物件嗎?
沒有,但我可以 new 一個!
public classGirlFriend{
private String name;
privateint age;
// 省略 getter & setter ...
publicstaticvoidmain(String[] args){
GirlFriend myGirlFriend = new GirlFriend();
myGirlFriend.setName("小美");
myGirlFriend.setAge(18);
}
}
沒問題,老鐵!但如果物件的內容太多,咋辦?
public classGirlFriend{
private String name;
privateint age;
privateint bust;
privateint waist;
privateint 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 ...
publicstaticvoidmain(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("公眾號:一行Java");
myGirlFriend.setMobile("18688888888");
myGirlFriend.setEmail("[email protected]");
myGirlFriend.setHairColor("淺棕色帶點微卷");
List<String> hobby = new ArrayList<>();
hobby.add("逛街");
hobby.add("購物");
hobby.add("買東西");
myGirlFriend.setHobby(hobby);
Map<String, String> gift = 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='[email protected]'
, hairColor='淺棕色帶點微卷'
, gift={情人節禮物=LBR 1912女王時代, 生日禮物=迪奧烈焰藍金, 紀念日禮物=阿瑪尼紅管唇釉}
}
GirlFriend 是很美,但寫起來也太麻煩了吧。
說說缺點:例項化和設定內容分開,不好維護;變量名重復寫。
莫慌,看法寶~
這裏不再介紹其他 Builder 實作方式,直接祭出最實用的 通用Builder :
適用於所有類,不需要改造原來類,不需要 lombok 外掛程式支持。
先看看使用姿勢:
public classGirlFriend{
// 省略內容 ...
// 省略 getter & setter ...
// 為了演示方便,加幾個聚合方法
publicvoidaddHobby(String hobby){
this.hobby = Optional.ofNullable(this.hobby).orElse(new ArrayList<>());
this.hobby.add(hobby);
}
publicvoidaddGift(String day, String gift){
this.gift = Optional.ofNullable(this.gift).orElse(new HashMap<>());
this.gift.put(day, gift);
}
publicvoidsetVitalStatistics(int bust, int waist, int hips){
this.bust = bust;
this.waist = waist;
this.hips = hips;
}
publicstaticvoidmain(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, "[email protected]")
.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 模式構建器
*
*/
public classBuilder<T> {
privatefinal Supplier<T> instantiator;
private List<Consumer<T>> modifiers = new ArrayList<>();
publicBuilder(Supplier<T> instantiator){
this.instantiator = instantiator;
}
publicstatic <T> Builder<T> of(Supplier<T> instantiator){
returnnew Builder<>(instantiator);
}
public <P1> Builder<T> with(Consumer1<T, P1> consumer, P1 p1){
Consumer<T> c = instance -> consumer.accept(instance, p1);
modifiers.add(c);
returnthis;
}
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);
returnthis;
}
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);
returnthis;
}
public T build(){
T value = instantiator.get();
modifiers.forEach(modifier -> modifier.accept(value));
modifiers.clear();
return value;
}
/**
* 1 參數 Consumer
*/
@FunctionalInterface
publicinterfaceConsumer1<T, P1> {
voidaccept(T t, P1 p1);
}
/**
* 2 參數 Consumer
*/
@FunctionalInterface
publicinterfaceConsumer2<T, P1, P2> {
voidaccept(T t, P1 p1, P2 p2);
}
/**
* 3 參數 Consumer
*/
@FunctionalInterface
publicinterfaceConsumer3<T, P1, P2, P3> {
voidaccept(T t, P1 p1, P2 p2, P3 p3);
}
}
這個範例最多支持三個參數的設定內容方法,也完全夠用了。如果要擴充套件也很容易,依葫蘆畫瓢,添加多個參數的
Consumer
。
快用你的 Builder 建個物件吧~
來源:ciphermagic.cn/java8-builder.html
>>
END
精品資料,超贊福利,免費領
微信掃碼/長按辨識 添加【技術交流群】
群內每天分享精品學習資料
最近開發整理了一個用於速刷面試題的小程式;其中收錄了上千道常見面試題及答案(包含基礎、並行、JVM、MySQL、Redis、Spring、SpringMVC、SpringBoot、SpringCloud、訊息佇列等多個型別),歡迎您的使用。
👇👇
👇點選"閱讀原文",獲取更多資料(持續更新中)