Skip to content

Commit f6c8bfb

Browse files
committed
iluwatar#590 Add explanation for Builder
1 parent cbba487 commit f6c8bfb

File tree

6 files changed

+98
-263
lines changed

6 files changed

+98
-263
lines changed

builder/README.md

+97-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,103 @@ Separate the construction of a complex object from its
1616
representation so that the same construction process can create different
1717
representations.
1818

19-
![alt text](./etc/builder_1.png "Builder")
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:
34+
35+
```
36+
public Hero(Profession profession, String name, HairType hairType, HairColor hairColor, Armor armor, Weapon weapon) {
37+
}
38+
```
39+
40+
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();
115+
```
20116

21117
## Applicability
22118
Use the Builder pattern when

builder/etc/builder.png

-52.5 KB
Binary file not shown.

builder/etc/builder.ucls

-162
This file was deleted.

builder/etc/builder.urm.puml

-100
This file was deleted.

builder/etc/builder_1.png

-104 KB
Binary file not shown.

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@
463463
<param>singleton</param>
464464
<param>factory-method</param>
465465
<param>abstract-factory</param>
466+
<param>builder</param>
466467
</skipForProjects>
467468
</configuration>
468469
</plugin>

0 commit comments

Comments
 (0)