Java中l(wèi)ombok的@Builder注解的解析與簡單使用詳解
1、建造者模式簡介:Builder 使用創(chuàng)建者模式又叫建造者模式。簡單來說,就是一步步創(chuàng)建一個(gè)對象,它對用戶屏蔽了里面構(gòu)建的細(xì)節(jié),但卻可以精細(xì)地控制對象的構(gòu)造過程。
2、注解類Builder.java注釋:
* The builder annotation creates a so-called ’builder’ aspect to the class that is annotated or the class * that contains a member which is annotated with {@code @Builder}. * <p> * If a member is annotated, it must be either a constructor or a method. If a class is annotated, * then a private constructor is generated with all fields as arguments * (as if {@code @AllArgsConstructor(access = AccessLevel.PRIVATE)} is present * on the class), and it is as if this constructor has been annotated with {@code @Builder} instead. * Note that this constructor is only generated if you haven’t written any constructors and also haven’t * added any explicit {@code @XArgsConstructor} annotations. In those cases, lombok will assume an all-args * constructor is present and generate code that uses it; this means you’d get a compiler error if this * constructor is not present.
在企業(yè)開發(fā)中,一般在領(lǐng)域?qū)ο髮?shí)體上標(biāo)注@Builder,其作用就相當(dāng)于@AllArgsConstructor(access = AccessLevel.PRIVATE),@Builder一般與@Getter結(jié)合使用。
3、實(shí)戰(zhàn)① 編寫測試實(shí)體類。
import lombok.Builder;import lombok.Getter;@Builder//@Getterpublic class Person { private String name; private String id; private String phoneNumeber;}
② 編寫測試類。
public class Test { public static void main(String[] args) { Person.PersonBuilder builder = Person.builder(); builder.phoneNumeber('11111').id('1123').name('asdd').build(); System.out.println(builder); }}
③編譯并執(zhí)行的結(jié)果為:Person.PersonBuilder(name=asdd, id=1123, phoneNumeber=11111)
④ 編譯后的字節(jié)碼分析:
//// Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler)//package com.atyunniao;public class Person { private String name; private String id; private String phoneNumeber; Person(String name, String id, String phoneNumeber) { this.name = name; this.id = id; this.phoneNumeber = phoneNumeber; } public static Person.PersonBuilder builder() { return new Person.PersonBuilder(); } public String getName() { return this.name; } public String getId() { return this.id; } public String getPhoneNumeber() { return this.phoneNumeber; } public static class PersonBuilder { private String name; private String id; private String phoneNumeber; PersonBuilder() { } public Person.PersonBuilder name(String name) { this.name = name; return this; } public Person.PersonBuilder id(String id) { this.id = id; return this; } public Person.PersonBuilder phoneNumeber(String phoneNumeber) { this.phoneNumeber = phoneNumeber; return this; } public Person build() { return new Person(this.name, this.id, this.phoneNumeber); } public String toString() { return 'Person.PersonBuilder(name=' + this.name + ', id=' + this.id + ', phoneNumeber=' + this.phoneNumeber + ')'; } }}
@Builder的作用:生成一個(gè)全屬性的構(gòu)造器生成了一個(gè)返回靜態(tài)內(nèi)部類PersonBuilder對象的方法生成了一個(gè)靜態(tài)內(nèi)部類PersonBuilder,這個(gè)靜態(tài)內(nèi)部類包含Person類的三個(gè)屬性,無參構(gòu)造器,三個(gè)方法名為屬性名的方法,返回Person對象的build方法,輸出靜態(tài)內(nèi)部類三個(gè)屬性的toString()方法。
⑤ 建造者使用過程:
Person.PersonBuilder builder = Person.builder(); builder.phoneNumeber('11111').id('1123').name('asdd').build(); System.out.println(builder);
先實(shí)例化內(nèi)部類對象并返回,然后為調(diào)用內(nèi)部類的方法為內(nèi)部類的屬性賦值,build()方法就是將內(nèi)部類PersonBuilder的屬性值傳入Person構(gòu)造器中,實(shí)例化Person對象。
以上即為對于@Builder的簡單使用。
到此這篇關(guān)于Java中l(wèi)ombok的@Builder注解的解析與簡單使用詳解的文章就介紹到這了,更多相關(guān)java lombok的@Builder注解內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. asp讀取xml文件和記數(shù)2. PHP實(shí)現(xiàn)基本留言板功能原理與步驟詳解3. CSS自定義滾動(dòng)條樣式案例詳解4. vue 驗(yàn)證兩次輸入的密碼是否一致的方法示例5. 每日六道java新手入門面試題,通往自由的道路第二天6. 讓你的PHP同時(shí)支持GIF、png、JPEG7. python利用opencv實(shí)現(xiàn)顏色檢測8. 簡體中文轉(zhuǎn)換為繁體中文的PHP函數(shù)9. 多個(gè)SpringBoot項(xiàng)目采用redis實(shí)現(xiàn)Session共享功能10. JavaScript快速實(shí)現(xiàn)一個(gè)顏色選擇器
