Reformat builder pattern - issue #224

This commit is contained in:
Ankur Kaushal
2015-11-01 18:03:29 -05:00
parent e7b6542134
commit 3af06a3a3a
8 changed files with 179 additions and 181 deletions

View File

@ -1,55 +1,55 @@
package com.iluwatar.builder;
import com.iluwatar. builder.Hero.HeroBuilder;
import com.iluwatar.builder.Hero.HeroBuilder;
/**
*
* The intention of the Builder pattern is to find a solution to the telescoping
* constructor anti-pattern. The telescoping constructor anti-pattern occurs when the
* increase of object constructor parameter combination leads to an exponential list
* of constructors. Instead of using numerous constructors, the builder pattern uses
* another object, a builder, that receives each initialization parameter step by step
* and then returns the resulting constructed object at once.
* The intention of the Builder pattern is to find a solution to the telescoping constructor
* anti-pattern. The telescoping constructor anti-pattern occurs when the increase of object
* constructor parameter combination leads to an exponential list of constructors. Instead of using
* numerous constructors, the builder pattern uses another object, a builder, that receives each
* initialization parameter step by step and then returns the resulting constructed object at once.
* <p>
* The Builder pattern has another benefit. It can be used for objects that contain
* flat data (html code, SQL query, X.509 certificate...), that is to say, data that
* can't be easily edited. This type of data cannot be edited step by step and must
* be edited at once. The best way to construct such an object is to use a builder
* class.
* The Builder pattern has another benefit. It can be used for objects that contain flat data (html
* code, SQL query, X.509 certificate...), that is to say, data that can't be easily edited. This
* type of data cannot be edited step by step and must be edited at once. The best way to construct
* such an object is to use a builder class.
* <p>
* In this example we have the Builder pattern variation as described by Joshua Bloch in
* Effective Java 2nd Edition.
* In this example we have 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.
* 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) {
/**
* 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 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 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);
Hero thief =
new HeroBuilder(Profession.THIEF, "Desmond").withHairType(HairType.BALD)
.withWeapon(Weapon.BOW).build();
System.out.println(thief);
}
}
}

View File

@ -7,16 +7,16 @@ package com.iluwatar.builder;
*/
public enum Armor {
CLOTHES("clothes"), LEATHER("leather"), CHAIN_MAIL("chain mail"), PLATE_MAIL("plate mail");
CLOTHES("clothes"), LEATHER("leather"), CHAIN_MAIL("chain mail"), PLATE_MAIL("plate mail");
private String title;
private String title;
Armor(String title) {
this.title = title;
}
Armor(String title) {
this.title = title;
}
@Override
public String toString() {
return title;
}
@Override
public String toString() {
return title;
}
}

View File

@ -7,11 +7,11 @@ package com.iluwatar.builder;
*/
public enum HairColor {
WHITE, BLOND, RED, BROWN, BLACK;
WHITE, BLOND, RED, BROWN, BLACK;
@Override
public String toString() {
return name().toLowerCase();
}
@Override
public String toString() {
return name().toLowerCase();
}
}

View File

@ -7,16 +7,17 @@ package com.iluwatar.builder;
*/
public enum HairType {
BALD("bald"), SHORT("short"), CURLY("curly"), LONG_STRAIGHT("long straight"), LONG_CURLY("long curly");
BALD("bald"), SHORT("short"), CURLY("curly"), LONG_STRAIGHT("long straight"), LONG_CURLY(
"long curly");
private String title;
private String title;
HairType(String title) {
this.title = title;
}
HairType(String title) {
this.title = title;
}
@Override
public String toString() {
return title;
}
@Override
public String toString() {
return title;
}
}

View File

@ -7,123 +7,122 @@ package com.iluwatar.builder;
*/
public 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 final Profession profession;
private final String name;
private final HairType hairType;
private final HairColor hairColor;
private final Armor armor;
private final Weapon weapon;
public Profession getProfession() {
return profession;
}
public Profession getProfession() {
return profession;
}
public String getName() {
return name;
}
public String getName() {
return name;
}
public HairType getHairType() {
return hairType;
}
public HairType getHairType() {
return hairType;
}
public HairColor getHairColor() {
return hairColor;
}
public HairColor getHairColor() {
return hairColor;
}
public Armor getArmor() {
return armor;
}
public Armor getArmor() {
return armor;
}
public Weapon getWeapon() {
return weapon;
}
public Weapon getWeapon() {
return weapon;
}
@Override
public String toString() {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("This is a ");
sb.append(profession);
sb.append(" named ");
sb.append(name);
if (hairColor != null || hairType != null) {
sb.append(" with ");
if (hairColor != null) {
sb.append(hairColor);
sb.append(" ");
}
if (hairType != null) {
sb.append(hairType);
sb.append(" ");
}
sb.append(hairType != HairType.BALD ? "hair" : "head");
}
if (armor != null) {
sb.append(" wearing ");
sb.append(armor);
}
if (weapon != null) {
sb.append(" and wielding a ");
sb.append(weapon);
}
sb.append(".");
return sb.toString();
}
StringBuilder sb = new StringBuilder();
sb.append("This is a ");
sb.append(profession);
sb.append(" named ");
sb.append(name);
if (hairColor != null || hairType != null) {
sb.append(" with ");
if (hairColor != null) {
sb.append(hairColor);
sb.append(" ");
}
if (hairType != null) {
sb.append(hairType);
sb.append(" ");
}
sb.append(hairType != HairType.BALD ? "hair" : "head");
}
if (armor != null) {
sb.append(" wearing ");
sb.append(armor);
}
if (weapon != null) {
sb.append(" and wielding a ");
sb.append(weapon);
}
sb.append(".");
return sb.toString();
}
private Hero(HeroBuilder 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;
}
private Hero(HeroBuilder 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;
}
/**
*
* The builder class.
*
*/
public static class HeroBuilder {
/**
*
* The builder class.
*
*/
public static class HeroBuilder {
private final Profession profession;
private final String name;
private HairType hairType;
private HairColor hairColor;
private Armor armor;
private Weapon weapon;
private final Profession profession;
private final String name;
private HairType hairType;
private HairColor hairColor;
private Armor armor;
private Weapon weapon;
public HeroBuilder(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 HeroBuilder(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 HeroBuilder withHairType(HairType hairType) {
this.hairType = hairType;
return this;
}
public HeroBuilder withHairType(HairType hairType) {
this.hairType = hairType;
return this;
}
public HeroBuilder withHairColor(HairColor hairColor) {
this.hairColor = hairColor;
return this;
}
public HeroBuilder withHairColor(HairColor hairColor) {
this.hairColor = hairColor;
return this;
}
public HeroBuilder withArmor(Armor armor) {
this.armor = armor;
return this;
}
public HeroBuilder withArmor(Armor armor) {
this.armor = armor;
return this;
}
public HeroBuilder withWeapon(Weapon weapon) {
this.weapon = weapon;
return this;
}
public HeroBuilder withWeapon(Weapon weapon) {
this.weapon = weapon;
return this;
}
public Hero build() {
return new Hero(this);
}
}
public Hero build() {
return new Hero(this);
}
}
}

View File

@ -7,11 +7,10 @@ package com.iluwatar.builder;
*/
public enum Profession {
WARRIOR, THIEF, MAGE, PRIEST;
@Override
public String toString() {
return name().toLowerCase();
}
WARRIOR, THIEF, MAGE, PRIEST;
@Override
public String toString() {
return name().toLowerCase();
}
}

View File

@ -7,11 +7,10 @@ package com.iluwatar.builder;
*/
public enum Weapon {
DAGGER, SWORD, AXE, WARHAMMER, BOW;
@Override
public String toString() {
return name().toLowerCase();
}
DAGGER, SWORD, AXE, WARHAMMER, BOW;
@Override
public String toString() {
return name().toLowerCase();
}
}

View File

@ -2,7 +2,7 @@ package com.iluwatar.builder;
import org.junit.Test;
import com.iluwatar. builder.App;
import com.iluwatar.builder.App;
/**
*
@ -11,9 +11,9 @@ import com.iluwatar. builder.App;
*/
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
@Test
public void test() {
String[] args = {};
App.main(args);
}
}