我在一個構造方法中寫了30個參數,老板看了想罵人
前言
一般我們寫參數如果寫個一兩個,那就可以了,如果寫七八個,那就有點難受了。如果寫十幾個?尼瑪,難受,我要去緩緩。
于是乎,一種新的方法策略運用而生。那就是builder模式,在構造方法的參數過多時,可以方便的進行創建一個類對象。所以本文的中心主旨一句話總結:當構造方法的參數過多時,推薦使用builder模式
既然推薦使用builder模式,那我們一個一個來,分析一下如果不使用builder模式有什么缺點。
一、傳統方式的缺點
1、可伸縮構造方法
可伸縮構造方法就是我們平時書寫最常見的那種,請看下文代碼;
- public class Student {
- private int id; //必要
- private String name;//必要
- private int age; //可選
- private int sclass; //可選
- private int height;//可選
- private float weight;//可選
- private float score;//可選
- //構造函數1:默認構造方法
- public Student() {};
- //構造方法2:必要字段構造方法
- public Student(int id, String name) {
- this.id = id;
- this.name = name;
- }
- //構造方法3:全部字段構造方法
- public Student(int id, String name, int age, int sclass, int height, float weight, float score) {
- super();
- this.id = id;
- this.name = name;
- this.age = age;
- this.sclass = sclass;
- this.height = height;
- this.weight = weight;
- this.score = score;
- }
- }
下面如果我們要創建一個Student類,一般這樣創建,看下面代碼:
- public class Main {
- public static void main(String[] args) {
- //1、可伸縮構造方法
- Student student1 = new Student();
- Student student2 = new Student(1,"愚公要移山");
- Student student3 = new Student(2,"愚公要移山",18,1,175,120,99);
- }
- }
現在我們列舉了一個具有七個字段的例子,比較容易理解,現在我們來分析一下,他有什么缺點:
缺點1:反轉字段,編譯器不會報錯
比如上面的字段里面有一個weight和一個score,都是float類型,如果再new一個Student類時,不小心寫反了,編譯器不會察覺。
缺點2:難以理解
這里只是七個字段,如果有十幾個,我們就需要不斷地去Student類中去查看,看看第幾個參數應該寫哪些東西,實在是比較麻煩。用戶在看到這個Student(2,"愚公要移山",18,1,175,120,99)無法理解每一個字段屬性代表的是什么含義。
缺點3:不想設置的參數,卻不得不設置值
有時候我們的Student只想著設置ID、name和age字段,其他的無關緊要,但是這種模式必須要設置所有的屬性值。
既然上面有這些缺點,我們可能還想到另外一種方式,那就是javaBean。
2、javaBean模式
先看javaBean模式如何寫的。
- public class Student {
- private int id; //必要
- private String name;//必要
- private int age; //可選
- private int sclass; //可選
- private int height;//可選
- private float weight;//可選
- private float score;//可選
- //構造函數1:默認構造方法
- public Student() {}
- //getter和setter方法
- public int getId() {return id;}
- public void setId(int id) {this.id = id;}
- 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 int getSclass() {return sclass;}
- public void setSclass(int sclass) {this.sclass = sclass;}
- public int getHeight() {return height;}
- public void setHeight(int height) {this.height = height;}
- public float getWeight() {return weight;}
- public void setWeight(float weight) {this.weight = weight;}
- public float getScore() {return score;}
- public void setScore(float score) {this.score = score;};
- }
這種模式,看起來還比較舒服,只是設置了相應的getter和setter方法。再來看看如何使用這種方式去new一個Student類。
- public class Main {
- public static void main(String[] args) {
- //2、javaBean模式
- Student student1 = new Student();
- student1.setId(1);
- student1.setName("愚公要移山");
- student1.setSclass(1);
- student1.setWeight(180);
- student1.setHeight(175);
- student1.setScore(100);
- student1.setAge(20);
- }
- }
這樣看起來還可以,不過這只是我自己一個一個敲出來的。實際在用的時候就知道同樣惡心了,現在來總結一波他的缺點。
缺點1:構造過程中 JavaBean可能處于不一致的狀態
JavaBeans 模式本身有嚴重的缺陷。由于構造方法在多次調用中被分割,所以在構造過程中 JavaBean 可能處于不一致的狀態。該類沒有通過檢查構造參數參數的有效性來執行一致性的選項。在不一致的狀態下嘗試使用對象可能會導致與包含 bug 的代碼大相徑庭的錯誤,因此很難調試。
說一下我對其的理解,在上面的例子中,我們的student1對象被多次調用了set方法,但是可能有時候在用到這個bean時,剩下的setter方法還沒有做完,于是再次調用時發現同一個javaBean呈現出了兩種狀態。于是處于一種不一致的狀態。
缺點2:無法保證javaBean的不可變性
使用第一種模式可伸縮構造方法實例化之后不會更改可變性,所有的數據都是確定好了的。也可以保證線程安全。但是提供了setter方法,就不能保證了。比如:
- public class Main {
- public static void main(String[] args) {
- //2、javaBean模式
- Student student1 = new Student();
- student1.setId(1);
- student1.setName("愚公要移山");
- student1.setSclass(1);
- student1.setWeight(180);
- student1.setHeight(175);
- student1.setScore(100);
- student1.setAge(20);
- System.out.println(student1.getName());
- student1.setName("馮冬冬");
- System.out.println(student1.getName());
- }
- }
- //輸出結果:愚公要移山 馮冬冬
可以看到,我們可以對Student對象設置多次name,前后是不一致的狀態。
既然前面兩種都存在各種各樣的問題。現在我們再來看今天的主題builder模式,
二、builder模式
還是老樣子,我們先看看builder模式長得什么樣子。再來分析一下他的優缺點。
- public class Student {
- private int id; // 必要
- private String name;// 必要
- private int age; // 可選
- private int sclass; // 可選
- private int height;// 可選
- private float weight;// 可選
- private float score;// 可選
- public Student(Builder builder) {
- this.id = builder.id;
- this.name = builder.name;
- this.age = builder.age;
- this.sclass = builder.sclass;
- this.height = builder.height;
- this.weight = builder.weight;
- this.score = builder.score;
- }
- public static class Builder {
- private int id; // 必要
- private String name;// 必要
- private int age; // 可選
- private int sclass; // 可選
- private int height;// 可選
- private float weight;// 可選
- private float score;// 可選
- // 必要參數的構造方法
- public Builder(int id, String name) {
- this.id = id;
- this.name = name;
- }
- public Builder setId(int id) {
- this.id = id;
- return this;
- }
- public Builder setName(String name) {
- this.name = name;
- return this;
- }
- public Builder setAge(int age) {
- this.age = age;
- return this;
- }
- public Builder setSclass(int sclass) {
- this.sclass = sclass;
- return this;
- }
- public Builder setHeight(int height) {
- this.height = height;
- return this;
- }
- public Builder setWeight(float weight) {
- this.weight = weight;
- return this;
- }
- public Builder setScore(float score) {
- this.score = score;
- return this;
- }
- // 對外提供的
- public Student build() {
- return new Student(this);
- }
- }
- }
上面的代碼是在內部構造了一個Builder類,然后我們看看如何去使用。
- public class Main {
- public static void main(String[] args) {
- //3、Builder模式
- Student stu = new Student.Builder(1, "愚公要移山")
- .setAge(20)
- .setHeight(175)
- .setSclass(1)
- .setScore(100)
- .setWeight(100).build();
- }
- }
這本書中對其的缺點也進行了介紹,很直觀可以看到,Student類中的代碼量增加了很多。但是Student類,我們只需要寫一次,這卻為我們創建對象帶來了方便。
優點1:不存在反轉字段的情況
上面可以看出,每次添加新字段值的時候是通過set方式進行的。具有javaBean的優點。
優點2:靈活構造參數
我們把必要的字段一寫,那些非必要的字段我們可以自己選擇是不是要set。
優點3:不存在不一致狀態
使用builder模式,對象的創建必須要等到build完成才可以。
優點4:使用靈活
單個 builder 可以重復使用來構建多個對象。builder 的參數可以在構建方法的調用之間進行調整,以改變創建的對象。builder 可以在創建對象時自動填充一些屬性,例如每次創建對象時增加的序列號。
缺點:
為了創建對象,首先必須創建它的 builder。雖然創建這個 builder 的成本在實踐中不太可能被注意到,但在性能關鍵的情況下可能會出現問題。而且,builder 模式比伸縮構造方法模式更冗長,因此只有在有足夠的參數時才值得使用它,比如四個或更多。
但是,如果從構造方法或靜態工廠開始,并切換到 builder,當類演化到參數數量失控的時候,過時的構造方法或靜態工廠就會面臨尷尬的處境。因此,所以,最好從一開始就創建一個 builder。
總結
如果我們的參數比較多時,builder模式是一個不錯的選擇,如果比較少時,由于Builder本身也是個對象占用一定的資源,所以還是使用可伸縮或者是javaBean的那種模式比較好。
本文轉載自微信公眾號「愚公要移山」,可以通過以下二維碼關注。轉載本文請聯系愚公要移山公眾號。