builder pattern


The Builder Pattern has been describe by the Gang of Four:

The builder pattern is a design pattern that allows for the step-by-step creation of complex objects using the correct sequence of actions. The construction is controlled by a director object that only needs to know the type of object it is to create.

It solves the problem when the increase of object constructor parameter combination leads to an exponential list of constructors. Instead of using numerous constructors, the builder pattern uses another object, a builder, that receives each initialization parameter step by step and then returns the resulting constructed object at once.

Intent

  • Separate the construction of a complex object from its representation so that the same construction process can create different representations.
  • Parse a complex representation, create one of several targets.

Implementation

Class Diagram of Builder Pattern
Here is an example from
Wikipedia:

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
72
73
74
75
76
77
78
79
public class {
private final Point origin;
private final Point destination;
private final Color waterColor;
private final Color landColor;
private final Color highTrafficColor;
private final Color mediumTrafficColor;
private final Color lowTrafficColor;
public static class Builder {
private final Point origin;
private final Point destination;
// Optional parameters - initialize with default values
private Color waterColor = Color.BLUE;
private Color landColor = new Color(30, 30, 30);
private Color highTrafficColor = Color.RED;
private Color mediumTrafficColor = Color.YELLOW;
private Color lowTrafficColor = Color.GREEN;
public Builder(Point origin, Point destination) {
this.origin = origin;
this.destination = destination;
}
public Builder waterColor(Color color) {
waterColor = color;
return this;
}
public Builder landColor(Color color) {
landColor = color;
return this;
}
public Builder highTrafficColor(Color color) {
highTrafficColor = color;
return this;
}
public Builder mediumTrafficColor(Color color) {
mediumTrafficColor = color;
return this;
}
public Builder lowTrafficColor(Color color) {
lowTrafficColor = color;
return this;
}
public StreetMap build() {
return new StreetMap(this);
}
}
private (Builder builder) {
origin = builder.origin;
destination = builder.destination;
// Optional parameters
waterColor = builder.waterColor;
landColor = builder.landColor;
highTrafficColor = builder.highTrafficColor;
mediumTrafficColor = builder.mediumTrafficColor;
lowTrafficColor = builder.lowTrafficColor;
}
public static void main(String args[]) {
Point origin = new Point(10, 10);
Point destination = new Point(50, 50);
StreetMap map = new StreetMap.Builder(origin, destination)
.landColor(Color.GRAY)
.waterColor(Color.BLUE.brighter())
.build();
}
}

Reference

Wikipedia