avatar

目录
Design Patterns : Builder

what is Builder

Builder is a creational design pattern. It helps you construct complex objects step by step and the final step will return the object. The process of constructing an object should be generic so that it can be used to create different representations of the same object.

image-20200319162756161

Example

Suppose we want to make a Lunch Order. It has a bunch of items such as bread, condiments, dressing, and meat, in reality, even more complex objects. if we use tradition way, that would require a bunch of and complex constructors to achieve that. Check out below example to understand the Builder Patter.

java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
public class LunchOrder {
private final String bread;
private final String condiments;
private final String dressing;
private final String meat;

/*
* 1. making constructor as private, ensure LunchOrder can't be instanted outside this class
* 2. constructor only accept Builder, so this is the bridge how we use builder to create LunchOderBean object
* 3. In order to wire the two object up, you observe that redundancy fields between Builder and LunchOrder
* */
private LunchOrder(Builder builder){
this.bread = builder.bread;
this.condiments = builder.condiments;
this.dressing = builder.dressing;
this.meat = builder.meat;
}


/*
* you can only see the getters but no setters, that ensures the object immucontratable
* */
public String getBread() {
return bread;
}
public String getCondiments() {
return condiments;
}
public String getDressing() {
return dressing;
}
public String getMeat() {
return meat;
}


/*
* 1.this inner must be static, otherwise it requires an instance of outer calss(LunchOrder) to access Builder
*
* */
public static class Builder{
private String bread;
private String condiments;
private String dressing;
private String meat;

public Builder(){}

public LunchOrder build(){
return new LunchOrder(this);
}

public Builder buildBread(String bread){
this.bread = bread;
return this;
}
public Builder buildDressing(String dressing){
this.dressing = dressing;
return this;
}
public Builder buildCondiments(String condiments){
this.condiments = condiments;
return this;
}
public Builder buildMeat(String meat){
this.meat = meat;
return this;
}
}

}

Below shows how to use builder to build our object:

java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class TestApp {
public static void main(String[] args) {

LunchOrder.Builder builder = new LunchOrder.Builder();
/*
* it not necessary to invoke all buildXXX() method, but only follow your needs.
*/
LunchOrder lunchOrder = builder.buildBread("bread")
.buildCondiments("condiments")
.buildDressing("dressing")
.buildMeat("meat")
.build();

System.out.println(lunchOrder.getBread());
System.out.println(lunchOrder.getCondiments());
System.out.println(lunchOrder.getDressing());
System.out.println(lunchOrder.getMeat());
}
}

Advantages

  • Use the builder pattern to get rid of a “telescopic constructor”.

    say you have a bunch of parameters, to fulfill various requirements, you have to create different constructors.

    java
    1
    2
    3
    4
    5
    6
    class Bread{
    Bread(){...}
    Bread(String p1,String p2){...}
    Bread(String p1,String p2,String p3){...}
    //...
    }

    The builder pattern lets you build objects step by step, with only those steps/ parameters that you really need.

  • Builder Pattern can help to create different representtations of some product.

    This requires you to build base builder interface defines all possible construction steps. And concrete builders implement these steps to consturst particular representations of the product.

  • Use the Builder to construct Composite trees or other comple objects.

    The Builder pattern lets you construct products step-by-step. You could defer execution of some steps without breaking the final product. You can even call steps recursively, which comes in handy when you need to build an object tree.

    A builder doesn’t expose the unfinished product while running construction steps. This prevents the client code from fetching an incomplete result.

文章作者: Aries Kou
文章链接: http://yoursite.com/posts/Design-Patterns/91188050.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Aries' Blog
打赏
  • 微信
    微信
  • 支付寶
    支付寶

评论