Java使用builder構(gòu)建對(duì)象

為什么使用Builder模式

1、傳統(tǒng)的創(chuàng)建對(duì)象可以使用構(gòu)造器創(chuàng)建,但是當(dāng)我們調(diào)用構(gòu)造器時(shí)通常需要許多你不想要設(shè)置的參數(shù),但是又不得不為它們傳值。這個(gè)時(shí)候如果我們使用重疊構(gòu)造器可行,但是當(dāng)有許多參數(shù)的時(shí)候,客戶端的代碼就會(huì)非常難寫,并且很難閱讀。

重疊構(gòu)造器代碼如下

public class BuilderPerson {
    //必選參數(shù)
    private final String name;
    private final int age;
    private final int height;
    private final int weight;
    //可選參數(shù)
    private final boolean isMarraying;
    private final boolean isHasChildren;

    
    public BuilderPerson(String name, int age) {
        this(name,age,0);
    }

    public BuilderPerson(String name, int age, int height) {
        this(name,age,height,0);
    }

    public BuilderPerson(String name, int age, int height, int weight) {
        this(name,age,height,weight,false);
    }

    public BuilderPerson(String name, int age, int height, int weight, boolean isMarraying) {
        this(name, age,height,weight,isMarraying,false);
    }

    public BuilderPerson(String name, int age, int height, int weight, boolean isMarraying, boolean isHasChildren) {
        this.name = name;
        this.age = age;
        this.height = height;
        this.weight = weight;
        this.isMarraying = isMarraying;
        this.isHasChildren = isHasChildren;
    }
}

2、當(dāng)遇到許多構(gòu)造器參數(shù)的時(shí)候,還有第二中代替的辦法,即使用JavaBean模式,在這種模式下,調(diào)用一個(gè)無參的構(gòu)造器來創(chuàng)建對(duì)象,然后調(diào)用setter方法來設(shè)置每個(gè)必要的參數(shù),以及每個(gè)相關(guān)的可選參數(shù)。

JavaBean代碼如下

public class BuilderPerson {
    //必選參數(shù)
    private  String name;
    private  int age;
    private  int height;
    private  int weight;
    //可選參數(shù)
    private  boolean isMarraying;
    private  boolean isHasChildren;

    public BuilderPerson() {
    }

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

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

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

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

    public void setMarraying(boolean marraying) {
        isMarraying = marraying;
    }

    public void setHasChildren(boolean hasChildren) {
        isHasChildren = hasChildren;
    }
}

但是JavaBean模式自身有著嚴(yán)重的缺點(diǎn),會(huì)導(dǎo)致類處于不一致(你new了Test類的兩個(gè)實(shí)例,一個(gè)只set了A屬性,一個(gè)只設(shè)置了B屬性,這兩個(gè)實(shí)例不一致,你不能保證通過該類的同一個(gè)構(gòu)造器保證構(gòu)造出來的對(duì)象是屬性相同的。)的狀態(tài)。

這時(shí)就出現(xiàn)了第三種替代的方法既能像重疊構(gòu)造器那樣的安全性,也能保證代碼的可讀性即Builder模式。

代碼如下

public static class Builder{
        //必選參數(shù)
        private  String name;
        private  int age;
        private  int height;
        private  int weight;
        //可選參數(shù)
        private final boolean isMarraying;
        private final boolean isHasChildren;

        public Builder(boolean isMarraying, boolean isHasChildren) {
            this.isMarraying = isMarraying;
            this.isHasChildren = isHasChildren;
        }
        public Builder name(String name) {
            name = name;
            return this;
        }

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

        public Builder height(int height) {
            height = height;
            return this;
        }

        public Builder weight(int weight) {
            weight = weight;
            return this;
        }
        public BuilderPerson builderPerson() {
            return new BuilderPerson(this);
        }
    }
}
調(diào)用如下
BuilderPerson person = new BuilderPerson.Builder(false,false).
name("張三").age(20).height(180).weight(120).buildPerson();

總結(jié)

簡(jiǎn)而言之,如果類的構(gòu)造器或者靜態(tài)工廠中具有多個(gè)參數(shù),設(shè)計(jì)這種類時(shí),Builder模式就是種不錯(cuò)的選擇,特別是當(dāng)大多數(shù)參數(shù)都是可選的時(shí)候,與使用傳統(tǒng)的重疊構(gòu)造器模式相比,使用Builder模式的客戶端代碼將更易于閱讀和編寫,構(gòu)建器也比JavaBeans更加安全。

歡迎加入學(xué)習(xí)交流群569772982,大家一起學(xué)習(xí)交流。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、文章前言 我們?cè)趈ava中穿梭時(shí)都會(huì)不停的構(gòu)建對(duì)象,new對(duì)象,但是為什么我卻找不到“對(duì)象”就像一個(gè)“野指針”...
    exphuhong閱讀 6,510評(píng)論 0 1
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,578評(píng)論 19 139
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,734評(píng)論 18 399
  • ——“極簡(jiǎn)主義生活是近年來的流行新趨勢(shì),是對(duì)自由的重新定義。它強(qiáng)調(diào)相對(duì)于“物”而言,生活方式應(yīng)當(dāng)由“人”所主導(dǎo)?!?..
    暮依閱讀 1,418評(píng)論 0 0
  • 第一、2017年的夢(mèng)想清單: 1、看綜藝追電視劇。放太多時(shí)間在玩電腦上。 2、買小家電和小東西。亂花錢令自己生活窘...
    星徴羽閱讀 235評(píng)論 0 0

友情鏈接更多精彩內(nèi)容