設計模式之Builder模式
阿新 • • 發佈:2017-05-21
builder tin -s close blog ati 可變對象 lap 如果
在平時的項目開發中,我們會通過構造方法傳參來實例化對象。
但在需要多個參數時,如果繼續使用構造方法實例,編寫代碼會非常麻煩,而且在其他項目成員傳參時特別容易出現傳參錯誤的情況,這時我們不妨來使用Builder模式進行編寫。
在使用Builder模式之前,我們先看下重疊構造器模式和JavaBeans模式:
1、 重疊構造器模式
1 package com.effectiveJava; 2 3 public class MultiContrucProduct { 4 private String name; 5 private floatView Codeprice; 6 private float weight; 7 private String introduction; 8 private String category; 9 10 public MultiContrucProduct(String name,float price){ 11 this(name,price,(float)0); 12 } 13 14 public MultiContrucProduct(String name,float price, float weight){15 this(name, price,weight,null); 16 } 17 18 public MultiContrucProduct(String name,float price,float weight,String introduction){ 19 this(name,price,weight,introduction,null); 20 } 21 22 public MultiContrucProduct(String name,float price,float weight,String introduction,String category){23 this.name = name; 24 this.price = price; 25 this.weight = weight; 26 this.introduction = introduction; 27 this.category = category; 28 } 29 30 public String getName() { 31 return name; 32 } 33 34 public void setName(String name) { 35 this.name = name; 36 } 37 38 public float getPrice() { 39 return price; 40 } 41 42 public void setPrice(float price) { 43 this.price = price; 44 } 45 46 public float getWeight() { 47 return weight; 48 } 49 50 public void setWeight(float weight) { 51 this.weight = weight; 52 } 53 54 public String getIntroduction() { 55 return introduction; 56 } 57 58 public void setIntroduction(String introduction) { 59 this.introduction = introduction; 60 } 61 62 public String getCategory() { 63 return category; 64 } 65 66 public void setCategory(String category) { 67 this.category = category; 68 } 69 70 71 }
實例化對象:
1 MultiContrucProduct multiContrucProduct = 2 new MultiContrucProduct("multiProduct", 10, 10, null, "cloth");
使用重疊構造器模式在參數較多時,比較繁雜,冗余代碼多,而且在傳參時容易出現參數類型,參數順序不對,造成錯誤。
2、JavaBeans模式
1 package com.effectiveJava; 2 3 public class JavaBeanProduct { 4 private String name; 5 private float price; 6 private float weight; 7 private String introduction; 8 private String category; 9 10 public JavaBeanProduct(){ 11 12 } 13 14 public String getName() { 15 return name; 16 } 17 18 public void setName(String name) { 19 this.name = name; 20 } 21 22 public float getPrice() { 23 return price; 24 } 25 26 public void setPrice(float price) { 27 this.price = price; 28 } 29 30 public float getWeight() { 31 return weight; 32 } 33 34 public void setWeight(float weight) { 35 this.weight = weight; 36 } 37 38 public String getIntroduction() { 39 return introduction; 40 } 41 42 public void setIntroduction(String introduction) { 43 this.introduction = introduction; 44 } 45 46 public String getCategory() { 47 return category; 48 } 49 50 public void setCategory(String category) { 51 this.category = category; 52 } 53 54 55 }View Code
實例化對象:
1 JavaBeanProduct javaBeanProduct = new JavaBeanProduct(); 2 javaBeanProduct.setName("javaBeanProduct"); 3 javaBeanProduct.setPrice(10); 4 javaBeanProduct.setWeight(10); 5 javaBeanProduct.setIntroduction(null); 6 javaBeanProduct.setCategory("cloth");
使用JavaBeans模式編寫較為簡單,通過set方式也比較容易區分參數。但在構造過程中JavaBean可能處於不一致的狀態,這時使用該實例將會導致失敗。此外JavaBeans模式阻止了把類構造為不可變對象的
可能性,影響多線程中安全性處理。
3、Builder模式
1 package com.effectiveJava; 2 3 public class BuilderProduct { 4 private String name; 5 private float price; 6 private float weight; 7 private String introduction; 8 private String category; 9 10 private BuilderProduct(Builder builder){ 11 this.name = builder.name; 12 this.price = builder.price; 13 this.weight = builder.weight; 14 this.introduction = builder.introduction; 15 this.category = builder.category; 16 } 17 18 public static class Builder{ 19 private final String name; 20 private final float price; 21 private float weight; 22 private String introduction; 23 private String category; 24 25 public float getWeight() { 26 return weight; 27 } 28 29 30 31 public Builder setWeight(float weight) { 32 this.weight = weight; 33 return this; 34 } 35 36 37 38 public String getIntroduction() { 39 return introduction; 40 } 41 42 43 44 public Builder setIntroduction(String introduction) { 45 this.introduction = introduction; 46 return this; 47 } 48 49 50 51 public String getCategory() { 52 return category; 53 } 54 55 56 57 public Builder setCategory(String category) { 58 this.category = category; 59 return this; 60 } 61 62 63 64 public BuilderProduct builder(){ 65 return new BuilderProduct(this); 66 } 67 68 public Builder(String name,float price){ 69 this.name = name; 70 this.price = price; 71 } 72 } 73 }View Code
實例化對象:
1 BuilderProduct builderProduct = new BuilderProduct.Builder("builderProduct",0) 2 .setCategory("cloth") 3 .setIntroduction(null) 4 .setWeight(10) 5 .builder();
Builder模式是綜合了以上兩種模式的優點,即保證了重疊構造器模式的安全性,也能兼顧JavaBeans模式的可讀性。當然Builder模式還有其他的使用方式,本文只是展示了其中的一種使用。
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利
設計模式之Builder模式