欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用Builder方式創(chuàng)建對象示例代碼

 更新時間:2018年11月20日 10:46:02   作者:奔跑吧李博  
這篇文章主要給大家介紹了關(guān)于利用Builder方式創(chuàng)建對象的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

在創(chuàng)建對象需要初始化數(shù)據(jù),數(shù)據(jù)參數(shù)不容易區(qū)別,可傳可不傳的時候,可以考慮使用Builder建造方法創(chuàng)建。每每當(dāng)看到別人寫的用Bulder方式來調(diào)用,就覺得so cool,那就自己也來用Builder方式創(chuàng)建對象吧。

下面話不多說了,來一起看看詳細(xì)的介紹吧

現(xiàn)在要錄入一系列人員的基本信息的示例:

創(chuàng)建Person類

public class Person {
private String name;
private int age;
private float height;
private float weight;

public Person(String name, int age, float height, float weight) {
this.name = name;
this.age = age;
this.height = height;
this.weight = weight;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public float getHeight() {
return height;
}

public void setHeight(float height) {
this.height = height;
}

public float getWeight() {
return weight;
}

public void setWeight(float weight) {
this.weight = weight;
}
}

那么創(chuàng)建對象就是這樣,后面參數(shù)不容易看出表示的是什么,可讀性不強(qiáng)

new Person("馮提莫",18,150,43);
new Person("溫婉",17,164,48);

用Builder模式進(jìn)行改造

創(chuàng)建一個Builder類,屬性和Person類一樣,加上SetXxx()方法

static class Builder{
private String name;
private int age;
private float height;
private float weight;

public void setName(String name) {
this.name = name;
}

public void setAge(int age) {
this.age = age;
}

public void setHeight(float height) {
this.height = height;
}

public void setWeight(float weight) {
this.weight = weight;
}
}

將Person的構(gòu)造方法改為傳入Builder對象,將Builder對象的屬性值都賦予Person對象

public Person(Builder builder){
this.name = builder.name;
this.age = builder.age;
this.height = builder.height;
this.weight = builder.weight;
}

改造Builder類,關(guān)鍵步驟是將各個set方法返回Builder類對象,這樣就能繼續(xù)愉快地連續(xù)調(diào)用set方法了,最后調(diào)用build()創(chuàng)建方法返回Person對象。

static class Builder{
private String name;
private int age;
private float height;
private float weight;

public Builder setName(String name) {
this.name = name;
return this;
}

public Builder setAge(int age) {
this.age = age;
return this;
}

public Builder setHeight(float height) {
this.height = height;
return this;
}

public Builder setWeight(float weight) {
this.weight = weight;
return this;
}

public Person build(){
return new Person(this);
}
}

將各個set方法的返回值類型改為Builder,每次都返回builder對象,這樣才能將set方法實現(xiàn)連續(xù)的鏈?zhǔn)秸{(diào)用。

build()方法創(chuàng)建一個person對象,會調(diào)用Person的帶參構(gòu)造方法,將builder對象的屬性依次賦予person對象。person中的屬性值就是鏈?zhǔn)秸{(diào)用set方法的各個值。

使用Builder模式創(chuàng)建Person對象,明顯看出代碼可讀性大大增強(qiáng)。注:Builder創(chuàng)建方式通常是在創(chuàng)建的對象少的情況下使用。

Person person = new Person.Builder()
.setName("莉哥")
.setAge(20)
.setHeight(162)
.setWeight(45)
.build();

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • .NET微信公眾號查看關(guān)注者接口

    .NET微信公眾號查看關(guān)注者接口

    這篇文章主要為大家詳細(xì)介紹了.NET微信公眾號查看關(guān)注者接口的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • .net 讀取項目AssemblyInfo.cs屬性值

    .net 讀取項目AssemblyInfo.cs屬性值

    .net 讀取項目AssemblyInfo.cs屬性值的實現(xiàn)代碼。
    2009-06-06
  • .net core利用orm如何操作mysql數(shù)據(jù)庫詳解

    .net core利用orm如何操作mysql數(shù)據(jù)庫詳解

    這篇文章主要給大家介紹了關(guān)于.net core利用orm如何操作mysql數(shù)據(jù)庫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • .Net Core下HTTP請求IHttpClientFactory示例詳解

    .Net Core下HTTP請求IHttpClientFactory示例詳解

    這篇文章主要給大家介紹了關(guān)于.Net Core下HTTP請求IHttpClientFactory的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用.Net Core具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • asp.net 源碼保存 用程序分頁

    asp.net 源碼保存 用程序分頁

    asp.net 源碼保存 用程序分頁實現(xiàn)代碼。
    2010-06-06
  • 如何使用Swagger上傳文件

    如何使用Swagger上傳文件

    本文將介紹如何使用Swagger來上傳文件。本文分步驟給大家介紹的非常詳細(xì),感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-05-05
  • 淺談ASP.NET Core的幾種托管方式

    淺談ASP.NET Core的幾種托管方式

    這篇文章主要介紹了淺談ASP.NET Core的幾種托管方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • ASP.NET技巧:請求網(wǎng)址并解析返回的html

    ASP.NET技巧:請求網(wǎng)址并解析返回的html

    ASP.NET技巧:請求網(wǎng)址并解析返回的html...
    2006-09-09
  • 淺談ASP.NET Core 2.0 布局頁面(譯)

    淺談ASP.NET Core 2.0 布局頁面(譯)

    本篇文章主要介紹了淺談ASP.NET Core 2.0 布局頁面(譯),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • .NET使用DinkToPdf將HTML轉(zhuǎn)成PDF的示例代碼

    .NET使用DinkToPdf將HTML轉(zhuǎn)成PDF的示例代碼

    這篇文章主要介紹了.NET使用DinkToPdf將HTML轉(zhuǎn)成PDF的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02

最新評論