#590 Add explanation for Builder
This commit is contained in:
parent
cbba487ff8
commit
f6c8bfbc39
@ -16,7 +16,103 @@ Separate the construction of a complex object from its
|
||||
representation so that the same construction process can create different
|
||||
representations.
|
||||
|
||||

|
||||
## 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
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 52 KiB |
@ -1,162 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
||||
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
|
||||
<enumeration id="1" language="java" name="com.iluwatar.builder.HairType" project="builder"
|
||||
file="/builder/src/main/java/com/iluwatar/builder/HairType.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="214" width="189" x="177" y="419"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</enumeration>
|
||||
<enumeration id="2" language="java" name="com.iluwatar.builder.Weapon" project="builder"
|
||||
file="/builder/src/main/java/com/iluwatar/builder/Weapon.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="196" width="171" x="406" y="419"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</enumeration>
|
||||
<enumeration id="3" language="java" name="com.iluwatar.builder.HairColor" project="builder"
|
||||
file="/builder/src/main/java/com/iluwatar/builder/HairColor.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="196" width="144" x="-204" y="419"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</enumeration>
|
||||
<class id="4" language="java" name="com.iluwatar.builder.Hero.Builder" project="builder"
|
||||
file="/builder/src/main/java/com/iluwatar/builder/Hero.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="196" width="234" x="20" y="183"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<enumeration id="5" language="java" name="com.iluwatar.builder.Armor" project="builder"
|
||||
file="/builder/src/main/java/com/iluwatar/builder/Armor.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="196" width="145" x="617" y="419"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</enumeration>
|
||||
<enumeration id="6" language="java" name="com.iluwatar.builder.Profession" project="builder"
|
||||
file="/builder/src/main/java/com/iluwatar/builder/Profession.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="178" width="157" x="-20" y="419"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</enumeration>
|
||||
<class id="7" language="java" name="com.iluwatar.builder.Hero" project="builder"
|
||||
file="/builder/src/main/java/com/iluwatar/builder/Hero.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="232" width="178" x="275" y="-89"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<association id="8">
|
||||
<bendpoint x="284" y="183"/>
|
||||
<end type="SOURCE" refId="7" navigable="false">
|
||||
<attribute id="9" name="hairType"/>
|
||||
<multiplicity id="10" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="1" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="11">
|
||||
<end type="SOURCE" refId="4" navigable="false">
|
||||
<attribute id="12" name="weapon"/>
|
||||
<multiplicity id="13" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="2" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="14">
|
||||
<end type="SOURCE" refId="4" navigable="false">
|
||||
<attribute id="15" name="profession"/>
|
||||
<multiplicity id="16" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="6" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="17">
|
||||
<end type="SOURCE" refId="4" navigable="false">
|
||||
<attribute id="18" name="hairColor"/>
|
||||
<multiplicity id="19" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="3" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="20">
|
||||
<bendpoint x="350" y="183"/>
|
||||
<end type="SOURCE" refId="7" navigable="false">
|
||||
<attribute id="21" name="weapon"/>
|
||||
<multiplicity id="22" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="2" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="23">
|
||||
<end type="SOURCE" refId="4" navigable="false">
|
||||
<attribute id="24" name="hairType"/>
|
||||
<multiplicity id="25" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="1" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<nesting id="26">
|
||||
<end type="SOURCE" refId="7"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</nesting>
|
||||
<association id="27">
|
||||
<end type="SOURCE" refId="4" navigable="false">
|
||||
<attribute id="28" name="armor"/>
|
||||
<multiplicity id="29" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="5" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="30">
|
||||
<bendpoint x="-11" y="183"/>
|
||||
<end type="SOURCE" refId="7" navigable="false">
|
||||
<attribute id="31" name="profession"/>
|
||||
<multiplicity id="32" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="6" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="33">
|
||||
<bendpoint x="-32" y="183"/>
|
||||
<end type="SOURCE" refId="7" navigable="false">
|
||||
<attribute id="34" name="hairColor"/>
|
||||
<multiplicity id="35" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="3" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="36">
|
||||
<bendpoint x="455" y="183"/>
|
||||
<end type="SOURCE" refId="7" navigable="false">
|
||||
<attribute id="37" name="armor"/>
|
||||
<multiplicity id="38" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="5" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</classifier-display>
|
||||
<association-display labels="true" multiplicity="true"/>
|
||||
</class-diagram>
|
@ -1,100 +0,0 @@
|
||||
@startuml
|
||||
package com.iluwatar.builder {
|
||||
class App {
|
||||
- LOGGER : Logger {static}
|
||||
+ App()
|
||||
+ main(args : String[]) {static}
|
||||
}
|
||||
enum Armor {
|
||||
+ CHAIN_MAIL {static}
|
||||
+ CLOTHES {static}
|
||||
+ LEATHER {static}
|
||||
+ PLATE_MAIL {static}
|
||||
- title : String
|
||||
+ toString() : String
|
||||
+ valueOf(name : String) : Armor {static}
|
||||
+ values() : Armor[] {static}
|
||||
}
|
||||
enum HairColor {
|
||||
+ BLACK {static}
|
||||
+ BLOND {static}
|
||||
+ BROWN {static}
|
||||
+ RED {static}
|
||||
+ WHITE {static}
|
||||
+ toString() : String
|
||||
+ valueOf(name : String) : HairColor {static}
|
||||
+ values() : HairColor[] {static}
|
||||
}
|
||||
enum HairType {
|
||||
+ BALD {static}
|
||||
+ CURLY {static}
|
||||
+ LONG_CURLY {static}
|
||||
+ LONG_STRAIGHT {static}
|
||||
+ SHORT {static}
|
||||
- title : String
|
||||
+ toString() : String
|
||||
+ valueOf(name : String) : HairType {static}
|
||||
+ values() : HairType[] {static}
|
||||
}
|
||||
class Hero {
|
||||
- armor : Armor
|
||||
- hairColor : HairColor
|
||||
- hairType : HairType
|
||||
- name : String
|
||||
- profession : Profession
|
||||
- weapon : Weapon
|
||||
- Hero(builder : Builder)
|
||||
+ getArmor() : Armor
|
||||
+ getHairColor() : HairColor
|
||||
+ getHairType() : HairType
|
||||
+ getName() : String
|
||||
+ getProfession() : Profession
|
||||
+ getWeapon() : Weapon
|
||||
+ toString() : String
|
||||
}
|
||||
class Builder {
|
||||
- armor : Armor
|
||||
- hairColor : HairColor
|
||||
- hairType : HairType
|
||||
- name : String
|
||||
- profession : Profession
|
||||
- weapon : Weapon
|
||||
+ Builder(profession : Profession, name : String)
|
||||
+ build() : Hero
|
||||
+ withArmor(armor : Armor) : Builder
|
||||
+ withHairColor(hairColor : HairColor) : Builder
|
||||
+ withHairType(hairType : HairType) : Builder
|
||||
+ withWeapon(weapon : Weapon) : Builder
|
||||
}
|
||||
enum Profession {
|
||||
+ MAGE {static}
|
||||
+ PRIEST {static}
|
||||
+ THIEF {static}
|
||||
+ WARRIOR {static}
|
||||
+ toString() : String
|
||||
+ valueOf(name : String) : Profession {static}
|
||||
+ values() : Profession[] {static}
|
||||
}
|
||||
enum Weapon {
|
||||
+ AXE {static}
|
||||
+ BOW {static}
|
||||
+ DAGGER {static}
|
||||
+ SWORD {static}
|
||||
+ WARHAMMER {static}
|
||||
+ toString() : String
|
||||
+ valueOf(name : String) : Weapon {static}
|
||||
+ values() : Weapon[] {static}
|
||||
}
|
||||
}
|
||||
Builder ..+ Hero
|
||||
Hero --> "-profession" Profession
|
||||
Hero --> "-armor" Armor
|
||||
Builder --> "-hairColor" HairColor
|
||||
Builder --> "-weapon" Weapon
|
||||
Builder --> "-hairType" HairType
|
||||
Hero --> "-hairColor" HairColor
|
||||
Builder --> "-profession" Profession
|
||||
Hero --> "-hairType" HairType
|
||||
Hero --> "-weapon" Weapon
|
||||
Builder --> "-armor" Armor
|
||||
@enduml
|
Binary file not shown.
Before Width: | Height: | Size: 104 KiB |
Loading…
x
Reference in New Issue
Block a user