renamed HeroBuilder to Builder
This commit is contained in:
parent
f6649a4fed
commit
509de9782d
@ -28,7 +28,7 @@
|
|||||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
</display>
|
</display>
|
||||||
</enumeration>
|
</enumeration>
|
||||||
<class id="4" language="java" name="com.iluwatar.builder.Hero.HeroBuilder" project="builder"
|
<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">
|
file="/builder/src/main/java/com/iluwatar/builder/Hero.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
<position height="196" width="234" x="20" y="183"/>
|
<position height="196" width="234" x="20" y="183"/>
|
||||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.iluwatar.builder;
|
package com.iluwatar.builder;
|
||||||
|
|
||||||
import com.iluwatar.builder.Hero.HeroBuilder;
|
import com.iluwatar.builder.Hero.Builder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -41,10 +41,10 @@ import com.iluwatar.builder.Hero.HeroBuilder;
|
|||||||
* Java 2nd Edition.
|
* Java 2nd Edition.
|
||||||
* <p>
|
* <p>
|
||||||
* We want to build {@link Hero} objects, but its construction is complex because of the many
|
* We want to build {@link Hero} objects, but its construction is complex because of the many
|
||||||
* parameters needed. To aid the user we introduce {@link HeroBuilder} class. {@link HeroBuilder}
|
* parameters needed. To aid the user we introduce {@link Builder} class. {@link Hero.Builder}
|
||||||
* takes the minimum parameters to build {@link Hero} object in its constructor. After that
|
* takes the minimum parameters to build {@link Hero} object in its constructor. After that
|
||||||
* additional configuration for the {@link Hero} object can be done using the fluent
|
* additional configuration for the {@link Hero} object can be done using the fluent
|
||||||
* {@link HeroBuilder} interface. When configuration is ready the build method is called to receive
|
* {@link Builder} interface. When configuration is ready the build method is called to receive
|
||||||
* the final {@link Hero} object.
|
* the final {@link Hero} object.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@ -58,18 +58,18 @@ public class App {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
Hero mage =
|
Hero mage =
|
||||||
new HeroBuilder(Profession.MAGE, "Riobard").withHairColor(HairColor.BLACK)
|
new Hero.Builder(Profession.MAGE, "Riobard").withHairColor(HairColor.BLACK)
|
||||||
.withWeapon(Weapon.DAGGER).build();
|
.withWeapon(Weapon.DAGGER).build();
|
||||||
System.out.println(mage);
|
System.out.println(mage);
|
||||||
|
|
||||||
Hero warrior =
|
Hero warrior =
|
||||||
new HeroBuilder(Profession.WARRIOR, "Amberjill").withHairColor(HairColor.BLOND)
|
new Hero.Builder(Profession.WARRIOR, "Amberjill").withHairColor(HairColor.BLOND)
|
||||||
.withHairType(HairType.LONG_CURLY).withArmor(Armor.CHAIN_MAIL).withWeapon(Weapon.SWORD)
|
.withHairType(HairType.LONG_CURLY).withArmor(Armor.CHAIN_MAIL).withWeapon(Weapon.SWORD)
|
||||||
.build();
|
.build();
|
||||||
System.out.println(warrior);
|
System.out.println(warrior);
|
||||||
|
|
||||||
Hero thief =
|
Hero thief =
|
||||||
new HeroBuilder(Profession.THIEF, "Desmond").withHairType(HairType.BALD)
|
new Hero.Builder(Profession.THIEF, "Desmond").withHairType(HairType.BALD)
|
||||||
.withWeapon(Weapon.BOW).build();
|
.withWeapon(Weapon.BOW).build();
|
||||||
System.out.println(thief);
|
System.out.println(thief);
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ public final class Hero {
|
|||||||
private final Armor armor;
|
private final Armor armor;
|
||||||
private final Weapon weapon;
|
private final Weapon weapon;
|
||||||
|
|
||||||
private Hero(HeroBuilder builder) {
|
private Hero(Builder builder) {
|
||||||
this.profession = builder.profession;
|
this.profession = builder.profession;
|
||||||
this.name = builder.name;
|
this.name = builder.name;
|
||||||
this.hairColor = builder.hairColor;
|
this.hairColor = builder.hairColor;
|
||||||
@ -102,7 +102,7 @@ public final class Hero {
|
|||||||
* The builder class.
|
* The builder class.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static class HeroBuilder {
|
public static class Builder {
|
||||||
|
|
||||||
private final Profession profession;
|
private final Profession profession;
|
||||||
private final String name;
|
private final String name;
|
||||||
@ -114,7 +114,7 @@ public final class Hero {
|
|||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*/
|
*/
|
||||||
public HeroBuilder(Profession profession, String name) {
|
public Builder(Profession profession, String name) {
|
||||||
if (profession == null || name == null) {
|
if (profession == null || name == null) {
|
||||||
throw new IllegalArgumentException("profession and name can not be null");
|
throw new IllegalArgumentException("profession and name can not be null");
|
||||||
}
|
}
|
||||||
@ -122,22 +122,22 @@ public final class Hero {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HeroBuilder withHairType(HairType hairType) {
|
public Builder withHairType(HairType hairType) {
|
||||||
this.hairType = hairType;
|
this.hairType = hairType;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HeroBuilder withHairColor(HairColor hairColor) {
|
public Builder withHairColor(HairColor hairColor) {
|
||||||
this.hairColor = hairColor;
|
this.hairColor = hairColor;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HeroBuilder withArmor(Armor armor) {
|
public Builder withArmor(Armor armor) {
|
||||||
this.armor = armor;
|
this.armor = armor;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HeroBuilder withWeapon(Weapon weapon) {
|
public Builder withWeapon(Weapon weapon) {
|
||||||
this.weapon = weapon;
|
this.weapon = weapon;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ public class HeroTest {
|
|||||||
*/
|
*/
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testMissingProfession() throws Exception {
|
public void testMissingProfession() throws Exception {
|
||||||
new Hero.HeroBuilder(null, "Sir without a job");
|
new Hero.Builder(null, "Sir without a job");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -47,7 +47,7 @@ public class HeroTest {
|
|||||||
*/
|
*/
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testMissingName() throws Exception {
|
public void testMissingName() throws Exception {
|
||||||
new Hero.HeroBuilder(Profession.THIEF, null);
|
new Hero.Builder(Profession.THIEF, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -57,7 +57,7 @@ public class HeroTest {
|
|||||||
public void testBuildHero() throws Exception {
|
public void testBuildHero() throws Exception {
|
||||||
final String heroName = "Sir Lancelot";
|
final String heroName = "Sir Lancelot";
|
||||||
|
|
||||||
final Hero hero = new Hero.HeroBuilder(Profession.WARRIOR, heroName)
|
final Hero hero = new Hero.Builder(Profession.WARRIOR, heroName)
|
||||||
.withArmor(Armor.CHAIN_MAIL)
|
.withArmor(Armor.CHAIN_MAIL)
|
||||||
.withWeapon(Weapon.SWORD)
|
.withWeapon(Weapon.SWORD)
|
||||||
.withHairType(HairType.LONG_CURLY)
|
.withHairType(HairType.LONG_CURLY)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user