Skip to content

Commit 9dd46d7

Browse files
committed
Update README.md
1 parent 723afb8 commit 9dd46d7

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

builder/README.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,47 @@ tags:
99
---
1010

1111
## 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.
1515

1616
## Explanation
1717

1818
Real world example
1919

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.
2124
2225
In plain words
2326

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.
2530
2631
Wikipedia says
2732

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.
2935
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:
3138

3239
```java
3340
public Hero(Profession profession, String name, HairType hairType, HairColor hairColor, Armor armor, Weapon weapon) {
3441
}
3542
```
3643

37-
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.
3848

3949
**Programmatic Example**
4050

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:
4253

4354
```java
4455
public final class Hero {
@@ -60,7 +71,7 @@ public final class Hero {
6071
}
6172
```
6273

63-
And then we have the builder
74+
Then we have the builder:
6475

6576
```java
6677
public static class Builder {
@@ -105,20 +116,22 @@ And then we have the builder
105116
}
106117
```
107118

108-
And then it can be used as:
119+
Then it can be used as:
109120

110121
```java
111122
var mage = new Hero.Builder(Profession.MAGE, "Riobard").withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER).build();
112123
```
113124

114125
## Class diagram
126+
115127
![alt text](./etc/builder.urm.png "Builder class diagram")
116128

117129
## Applicability
130+
118131
Use the Builder pattern when
119132

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
122135

123136
## Real world examples
124137

0 commit comments

Comments
 (0)