Skip to content

Commit 3af06a3

Browse files
committed
Reformat builder pattern - issue iluwatar#224
1 parent e7b6542 commit 3af06a3

File tree

8 files changed

+196
-198
lines changed

8 files changed

+196
-198
lines changed
Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
11
package com.iluwatar.builder;
22

3-
import com.iluwatar. builder.Hero.HeroBuilder;
3+
import com.iluwatar.builder.Hero.HeroBuilder;
44

55
/**
66
*
7-
* The intention of the Builder pattern is to find a solution to the telescoping
8-
* constructor anti-pattern. The telescoping constructor anti-pattern occurs when the
9-
* increase of object constructor parameter combination leads to an exponential list
10-
* of constructors. Instead of using numerous constructors, the builder pattern uses
11-
* another object, a builder, that receives each initialization parameter step by step
12-
* and then returns the resulting constructed object at once.
7+
* The intention of the Builder pattern is to find a solution to the telescoping constructor
8+
* anti-pattern. The telescoping constructor anti-pattern occurs when the increase of object
9+
* constructor parameter combination leads to an exponential list of constructors. Instead of using
10+
* numerous constructors, the builder pattern uses another object, a builder, that receives each
11+
* initialization parameter step by step and then returns the resulting constructed object at once.
1312
* <p>
14-
* The Builder pattern has another benefit. It can be used for objects that contain
15-
* flat data (html code, SQL query, X.509 certificate...), that is to say, data that
16-
* can't be easily edited. This type of data cannot be edited step by step and must
17-
* be edited at once. The best way to construct such an object is to use a builder
18-
* class.
13+
* The Builder pattern has another benefit. It can be used for objects that contain flat data (html
14+
* code, SQL query, X.509 certificate...), that is to say, data that can't be easily edited. This
15+
* type of data cannot be edited step by step and must be edited at once. The best way to construct
16+
* such an object is to use a builder class.
1917
* <p>
20-
* In this example we have the Builder pattern variation as described by Joshua Bloch in
21-
* Effective Java 2nd Edition.
18+
* In this example we have the Builder pattern variation as described by Joshua Bloch in Effective
19+
* Java 2nd Edition.
2220
* <p>
23-
* We want to build {@link Hero} objects, but its construction is complex because of the
24-
* many parameters needed. To aid the user we introduce {@link HeroBuilder} class.
25-
* {@link HeroBuilder} takes the minimum parameters to build {@link Hero} object in its
26-
* constructor. After that additional configuration for the {@link Hero} object can be
27-
* done using the fluent {@link HeroBuilder} interface. When configuration is ready the
28-
* build method is called to receive the final {@link Hero} object.
21+
* We want to build {@link Hero} objects, but its construction is complex because of the many
22+
* parameters needed. To aid the user we introduce {@link HeroBuilder} class. {@link HeroBuilder}
23+
* takes the minimum parameters to build {@link Hero} object in its constructor. After that
24+
* additional configuration for the {@link Hero} object can be done using the fluent
25+
* {@link HeroBuilder} interface. When configuration is ready the build method is called to receive
26+
* the final {@link Hero} object.
2927
*
3028
*/
3129
public class App {
3230

33-
/**
34-
* Program entry point
35-
* @param args command line args
36-
*/
37-
public static void main(String[] args) {
31+
/**
32+
* Program entry point
33+
*
34+
* @param args command line args
35+
*/
36+
public static void main(String[] args) {
3837

39-
Hero mage = new HeroBuilder(Profession.MAGE, "Riobard")
40-
.withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER)
41-
.build();
42-
System.out.println(mage);
38+
Hero mage =
39+
new HeroBuilder(Profession.MAGE, "Riobard").withHairColor(HairColor.BLACK)
40+
.withWeapon(Weapon.DAGGER).build();
41+
System.out.println(mage);
4342

44-
Hero warrior = new HeroBuilder(Profession.WARRIOR, "Amberjill")
45-
.withHairColor(HairColor.BLOND)
46-
.withHairType(HairType.LONG_CURLY).withArmor(Armor.CHAIN_MAIL)
47-
.withWeapon(Weapon.SWORD).build();
48-
System.out.println(warrior);
43+
Hero warrior =
44+
new HeroBuilder(Profession.WARRIOR, "Amberjill").withHairColor(HairColor.BLOND)
45+
.withHairType(HairType.LONG_CURLY).withArmor(Armor.CHAIN_MAIL).withWeapon(Weapon.SWORD)
46+
.build();
47+
System.out.println(warrior);
4948

50-
Hero thief = new HeroBuilder(Profession.THIEF, "Desmond")
51-
.withHairType(HairType.BALD).withWeapon(Weapon.BOW).build();
52-
System.out.println(thief);
49+
Hero thief =
50+
new HeroBuilder(Profession.THIEF, "Desmond").withHairType(HairType.BALD)
51+
.withWeapon(Weapon.BOW).build();
52+
System.out.println(thief);
5353

54-
}
54+
}
5555
}

builder/src/main/java/com/iluwatar/builder/Armor.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
*/
88
public enum Armor {
99

10-
CLOTHES("clothes"), LEATHER("leather"), CHAIN_MAIL("chain mail"), PLATE_MAIL("plate mail");
10+
CLOTHES("clothes"), LEATHER("leather"), CHAIN_MAIL("chain mail"), PLATE_MAIL("plate mail");
1111

12-
private String title;
12+
private String title;
1313

14-
Armor(String title) {
15-
this.title = title;
16-
}
14+
Armor(String title) {
15+
this.title = title;
16+
}
1717

18-
@Override
19-
public String toString() {
20-
return title;
21-
}
18+
@Override
19+
public String toString() {
20+
return title;
21+
}
2222
}

builder/src/main/java/com/iluwatar/builder/HairColor.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
*/
88
public enum HairColor {
99

10-
WHITE, BLOND, RED, BROWN, BLACK;
10+
WHITE, BLOND, RED, BROWN, BLACK;
1111

12-
@Override
13-
public String toString() {
14-
return name().toLowerCase();
15-
}
12+
@Override
13+
public String toString() {
14+
return name().toLowerCase();
15+
}
1616

1717
}

builder/src/main/java/com/iluwatar/builder/HairType.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@
77
*/
88
public enum HairType {
99

10-
BALD("bald"), SHORT("short"), CURLY("curly"), LONG_STRAIGHT("long straight"), LONG_CURLY("long curly");
10+
BALD("bald"), SHORT("short"), CURLY("curly"), LONG_STRAIGHT("long straight"), LONG_CURLY(
11+
"long curly");
1112

12-
private String title;
13+
private String title;
1314

14-
HairType(String title) {
15-
this.title = title;
16-
}
15+
HairType(String title) {
16+
this.title = title;
17+
}
1718

18-
@Override
19-
public String toString() {
20-
return title;
21-
}
19+
@Override
20+
public String toString() {
21+
return title;
22+
}
2223
}

0 commit comments

Comments
 (0)