diff --git a/builder/src/main/java/com/iluwatar/builder/App.java b/builder/src/main/java/com/iluwatar/builder/App.java index b6131c3ab..b5c8fd7a9 100644 --- a/builder/src/main/java/com/iluwatar/builder/App.java +++ b/builder/src/main/java/com/iluwatar/builder/App.java @@ -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. *
- * 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. *
- * 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. *
- * 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); - } + } } diff --git a/builder/src/main/java/com/iluwatar/builder/Armor.java b/builder/src/main/java/com/iluwatar/builder/Armor.java index 95fcba43b..6b67b8f94 100644 --- a/builder/src/main/java/com/iluwatar/builder/Armor.java +++ b/builder/src/main/java/com/iluwatar/builder/Armor.java @@ -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; + } } diff --git a/builder/src/main/java/com/iluwatar/builder/HairColor.java b/builder/src/main/java/com/iluwatar/builder/HairColor.java index dd8ec2ecc..b99b3db63 100644 --- a/builder/src/main/java/com/iluwatar/builder/HairColor.java +++ b/builder/src/main/java/com/iluwatar/builder/HairColor.java @@ -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(); + } } diff --git a/builder/src/main/java/com/iluwatar/builder/HairType.java b/builder/src/main/java/com/iluwatar/builder/HairType.java index ea49c0470..48eeac950 100644 --- a/builder/src/main/java/com/iluwatar/builder/HairType.java +++ b/builder/src/main/java/com/iluwatar/builder/HairType.java @@ -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; + } } diff --git a/builder/src/main/java/com/iluwatar/builder/Hero.java b/builder/src/main/java/com/iluwatar/builder/Hero.java index 05304bf09..54be11ea3 100644 --- a/builder/src/main/java/com/iluwatar/builder/Hero.java +++ b/builder/src/main/java/com/iluwatar/builder/Hero.java @@ -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); + } + } } diff --git a/builder/src/main/java/com/iluwatar/builder/Profession.java b/builder/src/main/java/com/iluwatar/builder/Profession.java index c9a7cc4e9..a157366b4 100644 --- a/builder/src/main/java/com/iluwatar/builder/Profession.java +++ b/builder/src/main/java/com/iluwatar/builder/Profession.java @@ -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(); + } } diff --git a/builder/src/main/java/com/iluwatar/builder/Weapon.java b/builder/src/main/java/com/iluwatar/builder/Weapon.java index af71c596d..08f55c65c 100644 --- a/builder/src/main/java/com/iluwatar/builder/Weapon.java +++ b/builder/src/main/java/com/iluwatar/builder/Weapon.java @@ -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(); + } } diff --git a/builder/src/test/java/com/iluwatar/builder/AppTest.java b/builder/src/test/java/com/iluwatar/builder/AppTest.java index acae1ca70..857f49dc9 100644 --- a/builder/src/test/java/com/iluwatar/builder/AppTest.java +++ b/builder/src/test/java/com/iluwatar/builder/AppTest.java @@ -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); + } }