Skip to content

Commit

Permalink
#590 Add explanation for Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
iluwatar committed Aug 13, 2017
1 parent cbba487 commit f6c8bfb
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 263 deletions.
98 changes: 97 additions & 1 deletion builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,103 @@ Separate the construction of a complex object from its
representation so that the same construction process can create different
representations.

![alt text](./etc/builder_1.png "Builder")
## Explanation

Real world example

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

> 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.
Wikipedia says

> The builder pattern is an object creation software design pattern with the intentions of finding a solution to the telescoping constructor anti-pattern.
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:

```
public Hero(Profession profession, String name, HairType hairType, HairColor hairColor, Armor armor, Weapon weapon) {
}
```

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.

**Programmatic Example**

The sane alternative is to use the Builder pattern. First of all we have our hero that we want to create

```
public final class Hero {
private final Profession profession;
private final String name;
private final HairType hairType;
private final HairColor hairColor;
private final Armor armor;
private final Weapon weapon;
private Hero(Builder builder) {
this.profession = builder.profession;
this.name = builder.name;
this.hairColor = builder.hairColor;
this.hairType = builder.hairType;
this.weapon = builder.weapon;
this.armor = builder.armor;
}
}
```

And then we have the builder

```
public static class Builder {
private final Profession profession;
private final String name;
private HairType hairType;
private HairColor hairColor;
private Armor armor;
private Weapon weapon;
public Builder(Profession profession, String name) {
if (profession == null || name == null) {
throw new IllegalArgumentException("profession and name can not be null");
}
this.profession = profession;
this.name = name;
}
public Builder withHairType(HairType hairType) {
this.hairType = hairType;
return this;
}
public Builder withHairColor(HairColor hairColor) {
this.hairColor = hairColor;
return this;
}
public Builder withArmor(Armor armor) {
this.armor = armor;
return this;
}
public Builder withWeapon(Weapon weapon) {
this.weapon = weapon;
return this;
}
public Hero build() {
return new Hero(this);
}
}
```

And then it can be used as:

```
Hero mage = new Hero.Builder(Profession.MAGE, "Riobard").withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER).build();
```

## Applicability
Use the Builder pattern when
Expand Down
Binary file removed builder/etc/builder.png
Binary file not shown.
162 changes: 0 additions & 162 deletions builder/etc/builder.ucls

This file was deleted.

100 changes: 0 additions & 100 deletions builder/etc/builder.urm.puml

This file was deleted.

Binary file removed builder/etc/builder_1.png
Binary file not shown.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@
<param>singleton</param>
<param>factory-method</param>
<param>abstract-factory</param>
<param>builder</param>
</skipForProjects>
</configuration>
</plugin>
Expand Down

0 comments on commit f6c8bfb

Please sign in to comment.