Skip to content

Commit a4b58fe

Browse files
committed
added builder sample
1 parent 959a1f6 commit a4b58fe

File tree

9 files changed

+272
-0
lines changed

9 files changed

+272
-0
lines changed

builder/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.iluwatar</groupId>
7+
<artifactId>java-design-patterns</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<groupId>com.iluwatar</groupId>
11+
<artifactId>builder</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
<name>builder</name>
14+
<url>http://maven.apache.org</url>
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>3.8.1</version>
20+
<scope>test</scope>
21+
</dependency>
22+
</dependencies>
23+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.iluwatar;
2+
3+
import com.iluwatar.Hero.HeroBuilder;
4+
5+
public class App
6+
{
7+
public static void main( String[] args )
8+
{
9+
10+
Hero mage = new HeroBuilder(Profession.MAGE, "Riobard")
11+
.withHairColor(HairColor.BLACK)
12+
.withWeapon(Weapon.DAGGER)
13+
.build();
14+
System.out.println(mage);
15+
16+
Hero warrior = new HeroBuilder(Profession.WARRIOR, "Amberjill")
17+
.withHairColor(HairColor.BLOND)
18+
.withHairType(HairType.LONG_CURLY)
19+
.withArmor(Armor.CHAIN_MAIL)
20+
.withWeapon(Weapon.SWORD)
21+
.build();
22+
System.out.println(warrior);
23+
24+
Hero thief = new HeroBuilder(Profession.THIEF, "Desmond")
25+
.withHairType(HairType.BOLD)
26+
.withWeapon(Weapon.BOW)
27+
.build();
28+
System.out.println(thief);
29+
30+
}
31+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.iluwatar;
2+
3+
public enum Armor {
4+
5+
CLOTHES, LEATHER, CHAIN_MAIL, PLATE_MAIL;
6+
7+
@Override
8+
public String toString() {
9+
String s = "";
10+
switch(this) {
11+
case CLOTHES: s = "clothes"; break;
12+
case LEATHER: s = "leather armor"; break;
13+
case CHAIN_MAIL: s = "chain mail"; break;
14+
case PLATE_MAIL: s = "plate mail"; break;
15+
}
16+
return s;
17+
}
18+
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.iluwatar;
2+
3+
public enum HairColor {
4+
5+
WHITE, BLOND, RED, BROWN, BLACK;
6+
7+
@Override
8+
public String toString() {
9+
String s = "";
10+
switch(this) {
11+
case WHITE: s = "white"; break;
12+
case BLOND: s = "blond"; break;
13+
case RED: s = "red"; break;
14+
case BROWN: s = "brown"; break;
15+
case BLACK: s = "black"; break;
16+
}
17+
return s;
18+
}
19+
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.iluwatar;
2+
3+
public enum HairType {
4+
5+
BOLD, SHORT, CURLY, LONG_STRAIGHT, LONG_CURLY;
6+
7+
@Override
8+
public String toString() {
9+
String s = "";
10+
switch(this) {
11+
case BOLD: s = "bold"; break;
12+
case SHORT: s = "short"; break;
13+
case CURLY: s = "curly"; break;
14+
case LONG_STRAIGHT: s = "long straight"; break;
15+
case LONG_CURLY: s = "long curly"; break;
16+
}
17+
return s;
18+
}
19+
20+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.iluwatar;
2+
3+
public class Hero {
4+
5+
private final Profession profession;
6+
private final String name;
7+
private final HairType hairType;
8+
private final HairColor hairColor;
9+
private final Armor armor;
10+
private final Weapon weapon;
11+
12+
public Profession getProfession() {
13+
return profession;
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public HairType getHairType() {
21+
return hairType;
22+
}
23+
24+
public HairColor getHairColor() {
25+
return hairColor;
26+
}
27+
28+
public Armor getArmor() {
29+
return armor;
30+
}
31+
32+
public Weapon getWeapon() {
33+
return weapon;
34+
}
35+
36+
@Override
37+
public String toString() {
38+
39+
StringBuilder sb = new StringBuilder();
40+
sb.append(profession);
41+
sb.append(" named ");
42+
sb.append(name);
43+
if (hairColor != null || hairType != null) {
44+
sb.append(" with ");
45+
if (hairColor != null) {
46+
sb.append(hairColor);
47+
sb.append(" ");
48+
}
49+
if (hairType != null) {
50+
sb.append(hairType);
51+
sb.append(" ");
52+
}
53+
sb.append(hairType != HairType.BOLD ? "hair" : "head");
54+
}
55+
if (armor != null) {
56+
sb.append(" wearing ");
57+
sb.append(armor);
58+
}
59+
if (weapon != null) {
60+
sb.append(" and wielding ");
61+
sb.append(weapon);
62+
}
63+
sb.append(".");
64+
return sb.toString();
65+
}
66+
67+
private Hero(HeroBuilder builder) {
68+
this.profession = builder.profession;
69+
this.name = builder.name;
70+
this.hairColor = builder.hairColor;
71+
this.hairType = builder.hairType;
72+
this.weapon = builder.weapon;
73+
this.armor = builder.armor;
74+
}
75+
76+
public static class HeroBuilder {
77+
78+
private final Profession profession;
79+
private final String name;
80+
private HairType hairType;
81+
private HairColor hairColor;
82+
private Armor armor;
83+
private Weapon weapon;
84+
85+
public HeroBuilder(Profession profession, String name) {
86+
if (profession == null || name == null) {
87+
throw new NullPointerException("profession and name can not be null");
88+
}
89+
this.profession = profession;
90+
this.name = name;
91+
}
92+
93+
public HeroBuilder withHairType(HairType hairType) {
94+
this.hairType = hairType;
95+
return this;
96+
}
97+
98+
public HeroBuilder withHairColor(HairColor hairColor) {
99+
this.hairColor = hairColor;
100+
return this;
101+
}
102+
103+
public HeroBuilder withArmor(Armor armor) {
104+
this.armor = armor;
105+
return this;
106+
}
107+
108+
public HeroBuilder withWeapon(Weapon weapon) {
109+
this.weapon = weapon;
110+
return this;
111+
}
112+
113+
public Hero build() {
114+
return new Hero(this);
115+
}
116+
117+
}
118+
119+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.iluwatar;
2+
3+
public enum Profession {
4+
5+
WARRIOR, THIEF, MAGE, PRIEST;
6+
7+
@Override
8+
public String toString() {
9+
String s = "";
10+
switch(this) {
11+
case WARRIOR: s = "Warrior"; break;
12+
case THIEF: s = "Thief"; break;
13+
case MAGE: s = "Mage"; break;
14+
case PRIEST: s = "Priest"; break;
15+
}
16+
return s;
17+
}
18+
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.iluwatar;
2+
3+
public enum Weapon {
4+
5+
DAGGER, SWORD, AXE, WARHAMMER, BOW;
6+
7+
@Override
8+
public String toString() {
9+
String s = "";
10+
switch(this) {
11+
case DAGGER: s = "dagger"; break;
12+
case SWORD: s = "sword"; break;
13+
case AXE: s = "axe"; break;
14+
case WARHAMMER: s = "warhammer"; break;
15+
case BOW: s = "bow"; break;
16+
}
17+
return s;
18+
}
19+
20+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
</properties>
2020
<modules>
2121
<module>abstract-factory</module>
22+
<module>builder</module>
2223
</modules>
2324
</project>

0 commit comments

Comments
 (0)