您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“java中Builder原理的示例分析”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“java中Builder原理的示例分析”這篇文章吧。
首先給一個簡單的Builder設計模式的例子:
主實現類代碼如下:
/** * 實體類 包含一個靜態內部類 Builder */ public class CompanyClient { public String companyName; // 用final修飾的成員變量表示常量,只能被賦值一次,賦值后值無法改變! final修飾的變量有三種:靜態變量、實例變量和局部變量。 public String companyAddress; public double companyRegfunds; public String mPerson; public String mType; //構造方法 public CompanyClient() { this(new Builder()); } //構造方法 public CompanyClient(Builder builder){ this.companyName = builder.companyName; this.companyAddress = builder.companyAddress; this.companyRegfunds = builder.companyRegfunds; this.mPerson = builder.person; this.mType = builder.type; } public String getCompanyName() { return companyName; } public String getCompanyAddress() { return companyAddress; } public double getCompanyRegfunds() { return companyRegfunds; } public String getmPerson() { return mPerson; } public String getmType() { return mType; } public Builder newBuilder() { return new Builder(this); } @Override //重寫toString方法后,當print這個對象的時候,會默認的調用toString()這個方法。 public String toString() { return "CompanyClient{" + "companyName='" + companyName + '\'' + ", companyAddress='" + companyAddress + '\'' + ", companyRegfunds=" + companyRegfunds +"千萬"+ ", mPerson=" + mPerson + ", mType='" + mType + '\'' + '}'; } /** *靜態內部類 Builder */ public static class Builder{ public String companyName; public String companyAddress; public double companyRegfunds; public String person; public String type; //構造方法 public Builder() { companyName = companyName; companyAddress = companyAddress; companyRegfunds = companyRegfunds; person = person; type = type; } //構造方法 Builder(CompanyClient companyClient){ this.companyName = companyClient.companyName; this.companyAddress = companyClient.companyAddress; this.companyRegfunds = companyClient.companyRegfunds; this.person = companyClient.mPerson; this.type = companyClient.mType; } public Builder setCompanyName(String name) { companyName = name; return this; } public Builder setCompanyAddress(String address) { companyAddress = address; return this; } public Builder setCompanyRegfunds(double regfunds) { companyRegfunds = regfunds; return this; } public Builder setmPerson(String per) { person = per; return this; } public Builder setmType(String typeStr) { type = typeStr; return this; } //構建一個實體 public CompanyClient build() { return new CompanyClient(this); } } }
測試類實現代碼如下:
public class TestBuilder { public static void main(String[] args) { CompanyClient client = new CompanyClient.Builder() .setCompanyName("alibaba") .setCompanyAddress("wangjing") .setCompanyRegfunds(5) .setmPerson("10000") .build(); System.out.println(client); System.out.println("---------------------"); CompanyClient.Builder builder = new CompanyClient.Builder(); builder.setCompanyName("huawei"); builder.setCompanyAddress("haidian"); builder.setCompanyRegfunds(20); builder.setmType("communication"); CompanyClient client1 = builder.build(); System.out.println(client1); System.out.println("---------------------"); CompanyClient.Builder build1 = client1.newBuilder(); build1.setCompanyName("baidu"); CompanyClient client2 = build1.build(); System.out.println(client2); } }
輸出結果如下:
CompanyClient{companyName='alibaba', companyAddress='wangjing', companyRegfunds=5.0千萬, mPerson=10000, mType='null'} --------------------- CompanyClient{companyName='huawei', companyAddress='haidian', companyRegfunds=20.0千萬, mPerson=null, mType='communication'} --------------------- CompanyClient{companyName='baidu', companyAddress='haidian', companyRegfunds=20.0千萬, mPerson=null, mType='communication'}
首先看main函數中的第一行代碼:
CompanyClient client = new CompanyClient.Builder() .setCompanyName("alibaba") .setCompanyAddress("wangjing") .setCompanyRegfunds(5) .setmPerson("10000") .build();
之所以可以這么執行,是因為內部靜態類Builder里定義了一份與CompanyClient類一模一樣的變量,通過一系列的成員函數進行設置屬性值,但是返回值都是this,也就是都是Builder對象,最后提供了一個build函數用于創建CompanyClient對象,返回的是CompanyClient對象,對應的構造函數在CompanyClient 類中進行定義,也就是構造函數的入參是Builder對象,然后依次對自己的成員變量進行賦值,對應的值都是Builder對象中的值。此外Builder類中的成員函數返回Builder對象自身的另一個作用就是讓它支持鏈式調用,使代碼可讀性大大增強。
總結下,Java環境下builder設計模式:
定義一個靜態內部類Builder,內部的成員變量和外部類一樣
Builder類通過一系列的方法用于成員變量的賦值,并返回當前對象本身(this)
Builder類提供一個build方法或者create方法用于創建對應的外部類,該方法內部調用了外部類的一個私有構造函數,該構造函數的參數就是內部類Builder
外部類提供一個私有構造函數供內部類調用,在該構造函數中完成成員變量的賦值,取值為Builder對象中對應的值
以上是“java中Builder原理的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。