设计模式之Builder模式

  在平时的项目开发中,我们会通过构造方法传参来实例化对象。

  但在需要多个参数时,如果继续使用构造方法实例,编写代码会非常麻烦,而且在其他项目成员传参时特别容易出现传参错误的情况,这时我们不妨来使用Builder模式进行编写。

  在使用Builder模式之前,我们先看下重叠构造器模式和JavaBeans模式:

1、 重叠构造器模式

 1 package com.effectiveJava;
 2 
 3 public class MultiContrucProduct {
 4     private String name;
 5     private float price;
 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 }
View Code

 

实例化对象:

 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模式还有其他的使用方式,本文只是展示了其中的一种使用。

 
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利

 

posted @ 2017-05-21 15:13  I.Striving  阅读(66)  评论(0编辑  收藏  举报