日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
我在一個構(gòu)造方法中寫了30個參數(shù),老板看了想罵人

前言

成都創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:做網(wǎng)站、網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的天峻網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

一般我們寫參數(shù)如果寫個一兩個,那就可以了,如果寫七八個,那就有點難受了。如果寫十幾個?尼瑪,難受,我要去緩緩。

于是乎,一種新的方法策略運用而生。那就是builder模式,在構(gòu)造方法的參數(shù)過多時,可以方便的進(jìn)行創(chuàng)建一個類對象。所以本文的中心主旨一句話總結(jié):當(dāng)構(gòu)造方法的參數(shù)過多時,推薦使用builder模式

既然推薦使用builder模式,那我們一個一個來,分析一下如果不使用builder模式有什么缺點。

一、傳統(tǒng)方式的缺點

1、可伸縮構(gòu)造方法

可伸縮構(gòu)造方法就是我們平時書寫最常見的那種,請看下文代碼;

 
 
 
 
  1. public class Student { 
  2.  private int id; //必要 
  3.  private String name;//必要 
  4.  private int age; //可選 
  5.  private int sclass; //可選 
  6.  private int height;//可選 
  7.  private float weight;//可選 
  8.  private float score;//可選 
  9.  //構(gòu)造函數(shù)1:默認(rèn)構(gòu)造方法 
  10.  public Student() {}; 
  11.  //構(gòu)造方法2:必要字段構(gòu)造方法 
  12.  public Student(int id, String name) { 
  13.   this.id = id; 
  14.   this.name = name; 
  15.  } 
  16.  //構(gòu)造方法3:全部字段構(gòu)造方法 
  17.  public Student(int id, String name, int age, int sclass, int height, float weight, float score) { 
  18.   super(); 
  19.   this.id = id; 
  20.   this.name = name; 
  21.   this.age = age; 
  22.   this.sclass = sclass; 
  23.   this.height = height; 
  24.   this.weight = weight; 
  25.   this.score = score; 
  26.  } 

下面如果我們要創(chuàng)建一個Student類,一般這樣創(chuàng)建,看下面代碼:

 
 
 
 
  1. public class Main { 
  2.  public static void main(String[] args) { 
  3.   //1、可伸縮構(gòu)造方法 
  4.   Student student1 = new Student(); 
  5.   Student student2 = new Student(1,"愚公要移山"); 
  6.   Student student3 = new Student(2,"愚公要移山",18,1,175,120,99); 
  7.  } 

現(xiàn)在我們列舉了一個具有七個字段的例子,比較容易理解,現(xiàn)在我們來分析一下,他有什么缺點:

缺點1:反轉(zhuǎn)字段,編譯器不會報錯

比如上面的字段里面有一個weight和一個score,都是float類型,如果再new一個Student類時,不小心寫反了,編譯器不會察覺。

缺點2:難以理解

這里只是七個字段,如果有十幾個,我們就需要不斷地去Student類中去查看,看看第幾個參數(shù)應(yīng)該寫哪些東西,實在是比較麻煩。用戶在看到這個Student(2,"愚公要移山",18,1,175,120,99)無法理解每一個字段屬性代表的是什么含義。

缺點3:不想設(shè)置的參數(shù),卻不得不設(shè)置值

有時候我們的Student只想著設(shè)置ID、name和age字段,其他的無關(guān)緊要,但是這種模式必須要設(shè)置所有的屬性值。

既然上面有這些缺點,我們可能還想到另外一種方式,那就是javaBean。

2、javaBean模式

先看javaBean模式如何寫的。

 
 
 
 
  1. public class Student { 
  2.  private int id; //必要 
  3.  private String name;//必要 
  4.  private int age; //可選 
  5.  private int sclass; //可選 
  6.  private int height;//可選 
  7.  private float weight;//可選 
  8.  private float score;//可選 
  9.  //構(gòu)造函數(shù)1:默認(rèn)構(gòu)造方法 
  10.  public Student() {} 
  11.     //getter和setter方法 
  12.  public int getId() {return id;} 
  13.  public void setId(int id) {this.id = id;} 
  14.  public String getName() {return name;} 
  15.  public void setName(String name) {this.name = name;} 
  16.  public int getAge() {return age;} 
  17.  public void setAge(int age) {this.age = age;} 
  18.  public int getSclass() {return sclass;} 
  19.  public void setSclass(int sclass) {this.sclass = sclass;} 
  20.  public int getHeight() {return height;} 
  21.  public void setHeight(int height) {this.height = height;} 
  22.  public float getWeight() {return weight;} 
  23.  public void setWeight(float weight) {this.weight = weight;} 
  24.  public float getScore() {return score;} 
  25.  public void setScore(float score) {this.score = score;}; 

這種模式,看起來還比較舒服,只是設(shè)置了相應(yīng)的getter和setter方法。再來看看如何使用這種方式去new一個Student類。

 
 
 
 
  1. public class Main { 
  2.  public static void main(String[] args) { 
  3.   //2、javaBean模式 
  4.   Student student1 = new Student(); 
  5.   student1.setId(1); 
  6.   student1.setName("愚公要移山"); 
  7.   student1.setSclass(1); 
  8.   student1.setWeight(180); 
  9.   student1.setHeight(175); 
  10.   student1.setScore(100); 
  11.   student1.setAge(20); 
  12.  } 

這樣看起來還可以,不過這只是我自己一個一個敲出來的。實際在用的時候就知道同樣惡心了,現(xiàn)在來總結(jié)一波他的缺點。

缺點1:構(gòu)造過程中 JavaBean可能處于不一致的狀態(tài)

JavaBeans 模式本身有嚴(yán)重的缺陷。由于構(gòu)造方法在多次調(diào)用中被分割,所以在構(gòu)造過程中 JavaBean 可能處于不一致的狀態(tài)。該類沒有通過檢查構(gòu)造參數(shù)參數(shù)的有效性來執(zhí)行一致性的選項。在不一致的狀態(tài)下嘗試使用對象可能會導(dǎo)致與包含 bug 的代碼大相徑庭的錯誤,因此很難調(diào)試。

說一下我對其的理解,在上面的例子中,我們的student1對象被多次調(diào)用了set方法,但是可能有時候在用到這個bean時,剩下的setter方法還沒有做完,于是再次調(diào)用時發(fā)現(xiàn)同一個javaBean呈現(xiàn)出了兩種狀態(tài)。于是處于一種不一致的狀態(tài)。

缺點2:無法保證javaBean的不可變性

使用第一種模式可伸縮構(gòu)造方法實例化之后不會更改可變性,所有的數(shù)據(jù)都是確定好了的。也可以保證線程安全。但是提供了setter方法,就不能保證了。比如:

 
 
 
 
  1. public class Main { 
  2.  public static void main(String[] args) { 
  3.   //2、javaBean模式 
  4.   Student student1 = new Student(); 
  5.   student1.setId(1); 
  6.   student1.setName("愚公要移山"); 
  7.   student1.setSclass(1); 
  8.   student1.setWeight(180); 
  9.   student1.setHeight(175); 
  10.   student1.setScore(100); 
  11.   student1.setAge(20); 
  12.   System.out.println(student1.getName()); 
  13.   student1.setName("馮冬冬"); 
  14.   System.out.println(student1.getName()); 
  15.  } 
  16. //輸出結(jié)果:愚公要移山  馮冬冬 

可以看到,我們可以對Student對象設(shè)置多次name,前后是不一致的狀態(tài)。

既然前面兩種都存在各種各樣的問題?,F(xiàn)在我們再來看今天的主題builder模式,

二、builder模式

還是老樣子,我們先看看builder模式長得什么樣子。再來分析一下他的優(yōu)缺點。

 
 
 
 
  1. public class Student { 
  2.  private int id; // 必要 
  3.  private String name;// 必要 
  4.  private int age; // 可選 
  5.  private int sclass; // 可選 
  6.  private int height;// 可選 
  7.  private float weight;// 可選 
  8.  private float score;// 可選 
  9.  public Student(Builder builder) { 
  10.   this.id = builder.id; 
  11.   this.name = builder.name; 
  12.   this.age = builder.age; 
  13.   this.sclass = builder.sclass; 
  14.   this.height = builder.height; 
  15.   this.weight = builder.weight; 
  16.   this.score = builder.score; 
  17.  } 
  18.  public static class Builder { 
  19.   private int id; // 必要 
  20.   private String name;// 必要 
  21.   private int age; // 可選 
  22.   private int sclass; // 可選 
  23.   private int height;// 可選 
  24.   private float weight;// 可選 
  25.   private float score;// 可選 
  26.   // 必要參數(shù)的構(gòu)造方法 
  27.   public Builder(int id, String name) { 
  28.    this.id = id; 
  29.    this.name = name; 
  30.   } 
  31.   public Builder setId(int id) { 
  32.    this.id = id; 
  33.    return this; 
  34.   } 
  35.   public Builder setName(String name) { 
  36.    this.name = name; 
  37.    return this; 
  38.   } 
  39.   public Builder setAge(int age) { 
  40.    this.age = age; 
  41.    return this; 
  42.   } 
  43.   public Builder setSclass(int sclass) { 
  44.    this.sclass = sclass; 
  45.    return this; 
  46.   } 
  47.   public Builder setHeight(int height) { 
  48.    this.height = height; 
  49.    return this; 
  50.   } 
  51.   public Builder setWeight(float weight) { 
  52.    this.weight = weight; 
  53.    return this; 
  54.   } 
  55.   public Builder setScore(float score) { 
  56.    this.score = score; 
  57.    return this; 
  58.   } 
  59.   // 對外提供的 
  60.   public Student build() { 
  61.    return new Student(this); 
  62.   } 
  63.  } 

上面的代碼是在內(nèi)部構(gòu)造了一個Builder類,然后我們看看如何去使用。

 
 
 
 
  1. public class Main { 
  2.  public static void main(String[] args) { 
  3.   //3、Builder模式 
  4.   Student stu = new Student.Builder(1, "愚公要移山") 
  5.     .setAge(20) 
  6.     .setHeight(175) 
  7.     .setSclass(1) 
  8.     .setScore(100) 
  9.     .setWeight(100).build(); 
  10.  } 

這本書中對其的缺點也進(jìn)行了介紹,很直觀可以看到,Student類中的代碼量增加了很多。但是Student類,我們只需要寫一次,這卻為我們創(chuàng)建對象帶來了方便。

優(yōu)點1:不存在反轉(zhuǎn)字段的情況

上面可以看出,每次添加新字段值的時候是通過set方式進(jìn)行的。具有javaBean的優(yōu)點。

優(yōu)點2:靈活構(gòu)造參數(shù)

我們把必要的字段一寫,那些非必要的字段我們可以自己選擇是不是要set。

優(yōu)點3:不存在不一致狀態(tài)

使用builder模式,對象的創(chuàng)建必須要等到build完成才可以。

優(yōu)點4:使用靈活

單個 builder 可以重復(fù)使用來構(gòu)建多個對象。builder 的參數(shù)可以在構(gòu)建方法的調(diào)用之間進(jìn)行調(diào)整,以改變創(chuàng)建的對象。builder 可以在創(chuàng)建對象時自動填充一些屬性,例如每次創(chuàng)建對象時增加的序列號。

缺點:

為了創(chuàng)建對象,首先必須創(chuàng)建它的 builder。雖然創(chuàng)建這個 builder 的成本在實踐中不太可能被注意到,但在性能關(guān)鍵的情況下可能會出現(xiàn)問題。而且,builder 模式比伸縮構(gòu)造方法模式更冗長,因此只有在有足夠的參數(shù)時才值得使用它,比如四個或更多。

但是,如果從構(gòu)造方法或靜態(tài)工廠開始,并切換到 builder,當(dāng)類演化到參數(shù)數(shù)量失控的時候,過時的構(gòu)造方法或靜態(tài)工廠就會面臨尷尬的處境。因此,所以,最好從一開始就創(chuàng)建一個 builder。

總結(jié)

如果我們的參數(shù)比較多時,builder模式是一個不錯的選擇,如果比較少時,由于Builder本身也是個對象占用一定的資源,所以還是使用可伸縮或者是javaBean的那種模式比較好。

本文轉(zhuǎn)載自微信公眾號「愚公要移山」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系愚公要移山公眾號。


分享名稱:我在一個構(gòu)造方法中寫了30個參數(shù),老板看了想罵人
當(dāng)前鏈接:http://www.dlmjj.cn/article/dpsecio.html