<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          你還在 new 對(duì)象嗎?Java8 通用 Builder 了解一下

          共 12993字,需瀏覽 26分鐘

           ·

          2021-07-14 14:29

          微信搜索逆鋒起筆關(guān)注后回復(fù)編程pdf

          領(lǐng)取編程大佬們所推薦的 23 種編程資料!

          作者:cipher
          www.ciphermagic.cn/java8-builder.html


          程序員經(jīng)常會(huì)遇到靈魂拷問(wèn):你有對(duì)象嗎?

          沒(méi)有,但我可以 new 一個(gè)!


             
          1. public class GirlFriend {

          2.     private String name;

          3.     private int age;

          4.     // 省略 getter & setter ...

          5.     public static void main(String[] args) {

          6.         GirlFriend myGirlFriend = new GirlFriend();

          7.         myGirlFriend.setName("小美");

          8.         myGirlFriend.setAge(18);

          9.     }

          10. }

          沒(méi)問(wèn)題,老鐵!但如果對(duì)象的屬性太多,咋辦?


             
          1. public class GirlFriend {

          2.     private String name;

          3.     private int age;

          4.     private int bust;

          5.     private int waist;

          6.     private int hips;

          7.     private List<String> hobby;

          8.     private String birthday;

          9.     private String address;

          10.     private String mobile;

          11.     private String email;

          12.     private String hairColor;

          13.     private Map<StringString> gift;

          14.     // 等等等等 ...

          15.     // 省略 getter & setter ...

          16.     public static void main(String[] args) {

          17.         GirlFriend myGirlFriend = new GirlFriend();

          18.         myGirlFriend.setName("小美");

          19.         myGirlFriend.setAge(18);

          20.         myGirlFriend.setBust(33);

          21.         myGirlFriend.setWaist(23);

          22.         myGirlFriend.setHips(33);

          23.         myGirlFriend.setBirthday("2001-10-26");

          24.         myGirlFriend.setAddress("上海浦東");

          25.         myGirlFriend.setMobile("18688888888");

          26.         myGirlFriend.setEmail("[email protected]");

          27.         myGirlFriend.setHairColor("淺棕色帶點(diǎn)微卷");

          28.         List<String> hobby = new ArrayList<>();

          29.         hobby.add("逛街");

          30.         hobby.add("購(gòu)物");

          31.         hobby.add("買(mǎi)東西");

          32.         myGirlFriend.setHobby(hobby);

          33.         Map<StringString> gift = new HashMap<>();

          34.         gift.put("情人節(jié)禮物""LBR 1912女王時(shí)代");

          35.         gift.put("生日禮物""迪奧烈焰藍(lán)金");

          36.         gift.put("紀(jì)念日禮物""阿瑪尼紅管唇釉");

          37.         myGirlFriend.setGift(gift);

          38.         // 等等等等 ...

          39.     }

          40. }

          GirlFriend 
             
          1. GirlFriend{name='小美'

          2. , age=18

          3. , bust=33

          4. , waist=23

          5. , hips=33

          6. , hobby=[逛街, 購(gòu)物, 買(mǎi)東西]

          7. , birthday='2001-10-26'

          8. , address='上海浦東'

          9. , mobile='18688888888'

          10. , email='[email protected]'

          11. , hairColor='淺棕色帶點(diǎn)微卷'

          12. , gift={情人節(jié)禮物=LBR 1912女王時(shí)代, 生日禮物=迪奧烈焰藍(lán)金, 紀(jì)念日禮物=阿瑪尼紅管唇釉}

          13. }

          GirlFriend 是很美,但寫(xiě)起來(lái)也太麻煩了吧。
          說(shuō)說(shuō)缺點(diǎn):實(shí)例化和設(shè)置屬性分開(kāi),不好維護(hù);變量名重復(fù)寫(xiě)。

          莫慌,看法寶~

          這里不再介紹其他 Builder 實(shí)現(xiàn)方式,直接祭出最實(shí)用的通用Builder:
          適用于所有類(lèi),不需要改造原來(lái)類(lèi),不需要 lombok 插件支持。
          先看看使用姿勢(shì):
             
          1. public class GirlFriend {

          2.     // 省略屬性 ...

          3.     // 省略 getter & setter ...


          4.     // 為了演示方便,加幾個(gè)聚合方法

          5.     public void addHobby(String hobby) {

          6.         this.hobby = Optional.ofNullable(this.hobby).orElse(new ArrayList<>());

          7.         this.hobby.add(hobby);

          8.     }

          9.     public void addGift(String day, String gift) {

          10.         this.gift = Optional.ofNullable(this.gift).orElse(new HashMap<>());

          11.         this.gift.put(day, gift);

          12.     }

          13.     public void setVitalStatistics(int bust, int waist, int hips) {

          14.         this.bust = bust;

          15.         this.waist = waist;

          16.         this.hips = hips;

          17.     }

          18.     public static void main(String[] args) {

          19.         GirlFriend myGirlFriend = Builder.of(GirlFriend::new)

          20.                 .with(GirlFriend::setName, "小美")

          21.                 .with(GirlFriend::setAge, 18)

          22.                 .with(GirlFriend::setVitalStatistics, 332333)

          23.                 .with(GirlFriend::setBirthday, "2001-10-26")

          24.                 .with(GirlFriend::setAddress, "上海浦東")

          25.                 .with(GirlFriend::setMobile, "18688888888")

          26.                 .with(GirlFriend::setEmail, "[email protected]")

          27.                 .with(GirlFriend::setHairColor, "淺棕色帶點(diǎn)微卷")

          28.                 .with(GirlFriend::addHobby, "逛街")

          29.                 .with(GirlFriend::addHobby, "購(gòu)物")

          30.                 .with(GirlFriend::addHobby, "買(mǎi)東西")

          31.                 .with(GirlFriend::addGift, "情人節(jié)禮物""LBR 1912女王時(shí)代")

          32.                 .with(GirlFriend::addGift, "生日禮物""迪奧烈焰藍(lán)金")

          33.                 .with(GirlFriend::addGift, "紀(jì)念日禮物""阿瑪尼紅管唇釉")

          34.                 // 等等等等 ...

          35.                 .build();

          36.     }

          37. }

          看到了嗎!實(shí)例化和屬性設(shè)置在同一條語(yǔ)句執(zhí)行,鏈?zhǔn)讲僮?,一路點(diǎn)點(diǎn)點(diǎn),清爽!

          Talk is cheap, show me the code:

             
          1. /**

          2.  * 通用的 Builder 模式構(gòu)建器

          3.  *

          4.  * @author: CipherCui

          5.  * @since 2019/8/29

          6.  */

          7. public class Builder<T> {

          8.     private final Supplier<T> instantiator;

          9.     private List<Consumer<T>> modifiers = new ArrayList<>();

          10.     public Builder(Supplier<T> instantiator) {

          11.         this.instantiator = instantiator;

          12.     }

          13.     public static <T> Builder<T> of(Supplier<T> instantiator) {

          14.         return new Builder<>(instantiator);

          15.     }

          16.     public <P1> Builder<T> with(Consumer1<T, P1> consumer, P1 p1) {

          17.         Consumer<T> c = instance -> consumer.accept(instance, p1);

          18.         modifiers.add(c);

          19.         return this;

          20.     }

          21.     public <P1, P2> Builder<T> with(Consumer2<T, P1, P2> consumer, P1 p1, P2 p2) {

          22.         Consumer<T> c = instance -> consumer.accept(instance, p1, p2);

          23.         modifiers.add(c);

          24.         return this;

          25.     }

          26.     public <P1, P2, P3> Builder<T> with(Consumer3<T, P1, P2, P3> consumer, P1 p1, P2 p2, P3 p3) {

          27.         Consumer<T> c = instance -> consumer.accept(instance, p1, p2, p3);

          28.         modifiers.add(c);

          29.         return this;

          30.     }

          31.     public T build() {

          32.         T value = instantiator.get();

          33.         modifiers.forEach(modifier -> modifier.accept(value));

          34.         modifiers.clear();

          35.         return value;

          36.     }

          37.     /**

          38.      * 1 參數(shù) Consumer

          39.      */

          40.     @FunctionalInterface

          41.     public interface Consumer1<T, P1> {

          42.         void accept(T t, P1 p1);

          43.     }

          44.     /**

          45.      * 2 參數(shù) Consumer

          46.      */

          47.     @FunctionalInterface

          48.     public interface Consumer2<T, P1, P2> {

          49.         void accept(T t, P1 p1, P2 p2);

          50.     }

          51.     /**

          52.      * 3 參數(shù) Consumer

          53.      */

          54.     @FunctionalInterface

          55.     public interface Consumer3<T, P1, P2, P3> {

          56.         void accept(T t, P1 p1, P2 p2, P3 p3);

          57.     }

          58. }

          這個(gè)示例最多支持三個(gè)參數(shù)的設(shè)置屬性方法,也完全夠用了。如果要擴(kuò)展也很容易,依葫蘆畫(huà)瓢,添加多個(gè)參數(shù)的Consumer。

          快用你的 Builder 建個(gè)對(duì)象吧~

          逆鋒起筆是一個(gè)專(zhuān)注于程序員圈子的技術(shù)平臺(tái),你可以收獲最新技術(shù)動(dòng)態(tài)、最新內(nèi)測(cè)資格BAT等大廠大佬的經(jīng)驗(yàn)、增長(zhǎng)自身、學(xué)習(xí)資料職業(yè)路線、賺錢(qián)思維,微信搜索逆鋒起筆關(guān)注!

          Java 程序員常犯的 10 個(gè) SQL 錯(cuò)誤!

          Java 全套視頻教程

          Java 初級(jí)和高級(jí)面試知識(shí)點(diǎn)準(zhǔn)備

          進(jìn)階必備!35 個(gè) Java 代碼優(yōu)化細(xì)節(jié)

          Java 9 到 16 新特性總結(jié)大全!


          點(diǎn)贊是最大的支持 

          瀏覽 49
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  www.九九水蜜桃 | 自拍一区不卡 | 男女拍拍拍拍拍拍拍拍 | 欧美后门菊门交3p视频 | 操片豆花视频在线观看 |