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
+97-1
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,103 @@ Separate the construction of a complex object from its
16
16
representation so that the same construction process can create different
17
17
representations.
18
18
19
-

19
+
## Explanation
20
+
21
+
Real world example
22
+
23
+
> 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.
24
+
25
+
In plain words
26
+
27
+
> 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.
28
+
29
+
Wikipedia says
30
+
31
+
> The builder pattern is an object creation software design pattern with the intentions of finding a solution to the telescoping constructor anti-pattern.
32
+
33
+
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:
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.
41
+
42
+
**Programmatic Example**
43
+
44
+
The sane alternative is to use the Builder pattern. First of all we have our hero that we want to create
45
+
46
+
```
47
+
public final class Hero {
48
+
private final Profession profession;
49
+
private final String name;
50
+
private final HairType hairType;
51
+
private final HairColor hairColor;
52
+
private final Armor armor;
53
+
private final Weapon weapon;
54
+
55
+
private Hero(Builder builder) {
56
+
this.profession = builder.profession;
57
+
this.name = builder.name;
58
+
this.hairColor = builder.hairColor;
59
+
this.hairType = builder.hairType;
60
+
this.weapon = builder.weapon;
61
+
this.armor = builder.armor;
62
+
}
63
+
}
64
+
```
65
+
66
+
And then we have the builder
67
+
68
+
```
69
+
public static class Builder {
70
+
private final Profession profession;
71
+
private final String name;
72
+
private HairType hairType;
73
+
private HairColor hairColor;
74
+
private Armor armor;
75
+
private Weapon weapon;
76
+
77
+
public Builder(Profession profession, String name) {
78
+
if (profession == null || name == null) {
79
+
throw new IllegalArgumentException("profession and name can not be null");
80
+
}
81
+
this.profession = profession;
82
+
this.name = name;
83
+
}
84
+
85
+
public Builder withHairType(HairType hairType) {
86
+
this.hairType = hairType;
87
+
return this;
88
+
}
89
+
90
+
public Builder withHairColor(HairColor hairColor) {
91
+
this.hairColor = hairColor;
92
+
return this;
93
+
}
94
+
95
+
public Builder withArmor(Armor armor) {
96
+
this.armor = armor;
97
+
return this;
98
+
}
99
+
100
+
public Builder withWeapon(Weapon weapon) {
101
+
this.weapon = weapon;
102
+
return this;
103
+
}
104
+
105
+
public Hero build() {
106
+
return new Hero(this);
107
+
}
108
+
}
109
+
```
110
+
111
+
And then it can be used as:
112
+
113
+
```
114
+
Hero mage = new Hero.Builder(Profession.MAGE, "Riobard").withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER).build();
0 commit comments