You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: builder/README.md
+26-13Lines changed: 26 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -9,36 +9,47 @@ tags:
9
9
---
10
10
11
11
## Intent
12
-
Separate the construction of a complex object from its
13
-
representation so that the same construction process can create different
14
-
representations.
12
+
13
+
Separate the construction of a complex object from its representation so that the same construction
14
+
process can create different representations.
15
15
16
16
## Explanation
17
17
18
18
Real world example
19
19
20
-
> Imagine a character generator for a role playing game. The easiest option is to let computer create the character for you. But if you want to select the character details like profession, gender, hair color etc. the character generation becomes a step-by-step process that completes when all the selections are ready.
20
+
> Imagine a character generator for a role-playing game. The easiest option is to let the computer
21
+
> create the character for you. If you want to manually select the character details like
22
+
> profession, gender, hair color etc. the character generation becomes a step-by-step process that
23
+
> completes when all the selections are ready.
21
24
22
25
In plain words
23
26
24
-
> Allows you to create different flavors of an object while avoiding constructor pollution. Useful when there could be several flavors of an object. Or when there are a lot of steps involved in creation of an object.
27
+
> Allows you to create different flavors of an object while avoiding constructor pollution. Useful
28
+
> when there could be several flavors of an object. Or when there are a lot of steps involved in
29
+
> creation of an object.
25
30
26
31
Wikipedia says
27
32
28
-
> The builder pattern is an object creation software design pattern with the intentions of finding a solution to the telescoping constructor anti-pattern.
33
+
> The builder pattern is an object creation software design pattern with the intentions of finding
34
+
> a solution to the telescoping constructor anti-pattern.
29
35
30
-
Having said that let me add a bit about what telescoping constructor anti-pattern is. At one point or the other we have all seen a constructor like below:
36
+
Having said that let me add a bit about what telescoping constructor anti-pattern is. At one point
37
+
or the other, we have all seen a constructor like below:
As you can see the number of constructor parameters can quickly get out of hand and it might become difficult to understand the arrangement of parameters. Plus this parameter list could keep on growing if you would want to add more options in future. This is called telescoping constructor anti-pattern.
44
+
As you can see the number of constructor parameters can quickly get out of hand, and it may become
45
+
difficult to understand the arrangement of parameters. Plus this parameter list could keep on
46
+
growing if you would want to add more options in the future. This is called telescoping constructor
47
+
anti-pattern.
38
48
39
49
**Programmatic Example**
40
50
41
-
The sane alternative is to use the Builder pattern. First of all we have our hero that we want to create
51
+
The sane alternative is to use the Builder pattern. First of all we have our hero that we want to
52
+
create:
42
53
43
54
```java
44
55
publicfinalclassHero {
@@ -60,7 +71,7 @@ public final class Hero {
60
71
}
61
72
```
62
73
63
-
And then we have the builder
74
+
Then we have the builder:
64
75
65
76
```java
66
77
publicstaticclassBuilder {
@@ -105,20 +116,22 @@ And then we have the builder
105
116
}
106
117
```
107
118
108
-
And then it can be used as:
119
+
Then it can be used as:
109
120
110
121
```java
111
122
var mage =newHero.Builder(Profession.MAGE, "Riobard").withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER).build();
112
123
```
113
124
114
125
## Class diagram
126
+
115
127

116
128
117
129
## Applicability
130
+
118
131
Use the Builder pattern when
119
132
120
-
*the algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled
121
-
*the construction process must allow different representations for the object that's constructed
133
+
*The algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled
134
+
*The construction process must allow different representations for the object that's constructed
0 commit comments