#107 JavaDoc improvements for the Builder example
This commit is contained in:
parent
1dc2d8d0c7
commit
81dda94abd
@ -1,38 +1,42 @@
|
||||
package com.iluwatar.builder;
|
||||
|
||||
import com.iluwatar. builder.Hero.HeroBuilder;
|
||||
|
||||
/**
|
||||
*
|
||||
* This is the Builder pattern variation as described by Joshua Bloch in
|
||||
* Effective Java 2nd Edition.
|
||||
*
|
||||
* We want to build Hero objects, but its construction is complex because of the
|
||||
* many parameters needed. To aid the user we introduce HeroBuilder class.
|
||||
* HeroBuilder takes the minimum parameters to build Hero object in its
|
||||
* constructor. After that additional configuration for the Hero object can be
|
||||
* done using the fluent HeroBuilder interface. When configuration is ready the
|
||||
* build method is called to receive the final Hero object.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Hero mage = new HeroBuilder(Profession.MAGE, "Riobard")
|
||||
.withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER)
|
||||
.build();
|
||||
System.out.println(mage);
|
||||
|
||||
Hero warrior = new HeroBuilder(Profession.WARRIOR, "Amberjill")
|
||||
.withHairColor(HairColor.BLOND)
|
||||
.withHairType(HairType.LONG_CURLY).withArmor(Armor.CHAIN_MAIL)
|
||||
.withWeapon(Weapon.SWORD).build();
|
||||
System.out.println(warrior);
|
||||
|
||||
Hero thief = new HeroBuilder(Profession.THIEF, "Desmond")
|
||||
.withHairType(HairType.BALD).withWeapon(Weapon.BOW).build();
|
||||
System.out.println(thief);
|
||||
|
||||
}
|
||||
}
|
||||
package com.iluwatar.builder;
|
||||
|
||||
import com.iluwatar. builder.Hero.HeroBuilder;
|
||||
|
||||
/**
|
||||
*
|
||||
* This is the Builder pattern variation as described by Joshua Bloch in
|
||||
* Effective Java 2nd Edition.
|
||||
* <p>
|
||||
* 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} 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 {@link HeroBuilder} interface. When configuration is ready the
|
||||
* build method is called to receive the final {@link Hero} object.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
Hero mage = new HeroBuilder(Profession.MAGE, "Riobard")
|
||||
.withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER)
|
||||
.build();
|
||||
System.out.println(mage);
|
||||
|
||||
Hero warrior = new HeroBuilder(Profession.WARRIOR, "Amberjill")
|
||||
.withHairColor(HairColor.BLOND)
|
||||
.withHairType(HairType.LONG_CURLY).withArmor(Armor.CHAIN_MAIL)
|
||||
.withWeapon(Weapon.SWORD).build();
|
||||
System.out.println(warrior);
|
||||
|
||||
Hero thief = new HeroBuilder(Profession.THIEF, "Desmond")
|
||||
.withHairType(HairType.BALD).withWeapon(Weapon.BOW).build();
|
||||
System.out.println(thief);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,22 @@
|
||||
package com.iluwatar.builder;
|
||||
|
||||
public enum Armor {
|
||||
|
||||
CLOTHES("clothes"), LEATHER("leather"), CHAIN_MAIL("chain mail"), PLATE_MAIL("plate mail");
|
||||
|
||||
private String title;
|
||||
|
||||
Armor(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
}
|
||||
package com.iluwatar.builder;
|
||||
|
||||
/**
|
||||
*
|
||||
* Armor enumeration
|
||||
*
|
||||
*/
|
||||
public enum Armor {
|
||||
|
||||
CLOTHES("clothes"), LEATHER("leather"), CHAIN_MAIL("chain mail"), PLATE_MAIL("plate mail");
|
||||
|
||||
private String title;
|
||||
|
||||
Armor(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,17 @@
|
||||
package com.iluwatar.builder;
|
||||
|
||||
public enum HairColor {
|
||||
|
||||
WHITE, BLOND, RED, BROWN, BLACK;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name().toLowerCase();
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.builder;
|
||||
|
||||
/**
|
||||
*
|
||||
* HairColor enumeration
|
||||
*
|
||||
*/
|
||||
public enum HairColor {
|
||||
|
||||
WHITE, BLOND, RED, BROWN, BLACK;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name().toLowerCase();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,17 +1,22 @@
|
||||
package com.iluwatar.builder;
|
||||
|
||||
public enum HairType {
|
||||
|
||||
BALD("bald"), SHORT("short"), CURLY("curly"), LONG_STRAIGHT("long straight"), LONG_CURLY("long curly");
|
||||
|
||||
private String title;
|
||||
|
||||
HairType(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
}
|
||||
package com.iluwatar.builder;
|
||||
|
||||
/**
|
||||
*
|
||||
* HairType enumeration
|
||||
*
|
||||
*/
|
||||
public enum HairType {
|
||||
|
||||
BALD("bald"), SHORT("short"), CURLY("curly"), LONG_STRAIGHT("long straight"), LONG_CURLY("long curly");
|
||||
|
||||
private String title;
|
||||
|
||||
HairType(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package com.iluwatar.builder;
|
||||
|
||||
/**
|
||||
*
|
||||
* The class with many parameters.
|
||||
* Hero, the class with many parameters.
|
||||
*
|
||||
*/
|
||||
public class Hero {
|
||||
|
@ -1,12 +1,17 @@
|
||||
package com.iluwatar.builder;
|
||||
|
||||
public enum Profession {
|
||||
|
||||
WARRIOR, THIEF, MAGE, PRIEST;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name().toLowerCase();
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.builder;
|
||||
|
||||
/**
|
||||
*
|
||||
* Profession enumeration
|
||||
*
|
||||
*/
|
||||
public enum Profession {
|
||||
|
||||
WARRIOR, THIEF, MAGE, PRIEST;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name().toLowerCase();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,12 +1,17 @@
|
||||
package com.iluwatar.builder;
|
||||
|
||||
public enum Weapon {
|
||||
|
||||
DAGGER, SWORD, AXE, WARHAMMER, BOW;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name().toLowerCase();
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.builder;
|
||||
|
||||
/**
|
||||
*
|
||||
* Weapon enumeration
|
||||
*
|
||||
*/
|
||||
public enum Weapon {
|
||||
|
||||
DAGGER, SWORD, AXE, WARHAMMER, BOW;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name().toLowerCase();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,14 +1,19 @@
|
||||
package com.iluwatar.builder;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar. builder.App;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
package com.iluwatar.builder;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar. builder.App;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user