自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

我在一個(gè)構(gòu)造方法中寫了30個(gè)參數(shù),老板看了想罵人

開發(fā) 前端
一般我們寫參數(shù)如果寫個(gè)一兩個(gè),那就可以了,如果寫七八個(gè),那就有點(diǎn)難受了。如果寫十幾個(gè)?難受,我要去緩緩。

[[386824]]

前言

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

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

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

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

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

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

  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 nameint 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.  } 

下面如果我們要?jiǎng)?chuàng)建一個(gè)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)在我們列舉了一個(gè)具有七個(gè)字段的例子,比較容易理解,現(xiàn)在我們來分析一下,他有什么缺點(diǎn):

缺點(diǎn)1:反轉(zhuǎn)字段,編譯器不會(huì)報(bào)錯(cuò)

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

缺點(diǎn)2:難以理解

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

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

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

既然上面有這些缺點(diǎn),我們可能還想到另外一種方式,那就是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一個(gè)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.  } 

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

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

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

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

缺點(diǎn)2:無法保證javaBean的不可變性

使用第一種模式可伸縮構(gòu)造方法實(shí)例化之后不會(huì)更改可變性,所有的數(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)缺點(diǎn)。

  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)造了一個(gè)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.  } 

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

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

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

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

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

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

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

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

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

缺點(diǎn):

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

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

總結(jié)

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

本文轉(zhuǎn)載自微信公眾號「 愚公要移山」,作者馮冬冬 。轉(zhuǎn)載本文請聯(lián)系愚公要移山公眾號。

 

責(zé)任編輯:武曉燕 來源: 愚公要移山
相關(guān)推薦

2020-12-28 05:54:37

構(gòu)造builder模式

2021-08-16 13:51:35

開發(fā)語言腳本

2021-02-20 07:52:35

防猝死插件 IDEA

2021-06-07 10:20:31

2021-02-02 11:59:15

插件開發(fā)工具

2025-04-07 04:00:00

教學(xué)型任務(wù)調(diào)度系統(tǒng)

2020-11-02 08:19:18

RPC框架Java

2020-03-03 07:59:29

設(shè)計(jì)秒殺系統(tǒng)

2012-12-07 10:04:58

管理項(xiàng)目管理日常管理

2020-08-04 16:56:50

Java方法參數(shù)

2013-05-13 10:24:44

谷歌開發(fā)團(tuán)隊(duì)開發(fā)管理

2023-12-28 08:01:59

2019-09-18 09:41:25

億級流量網(wǎng)站

2021-08-12 00:03:37

JSStrview視圖

2020-08-25 20:10:53

GitHub代碼開發(fā)者

2022-03-24 07:57:58

Python水果忍者游戲

2009-09-02 18:36:46

LinuxLinux操作系統(tǒng)Linux開發(fā)

2010-07-07 16:21:40

重用

2022-02-25 08:25:25

程序開發(fā)代碼

2020-05-15 09:30:12

代碼函數(shù)語言
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號