Changed package naming across all examples.

This commit is contained in:
Ilkka Seppala
2015-05-31 11:55:18 +03:00
parent 703ebd3e20
commit 8524c75ba6
437 changed files with 1095 additions and 1402 deletions
@@ -0,0 +1,38 @@
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);
}
}
@@ -0,0 +1,17 @@
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;
}
}
@@ -0,0 +1,12 @@
package com.iluwatar.builder;
public enum HairColor {
WHITE, BLOND, RED, BROWN, BLACK;
@Override
public String toString() {
return name().toLowerCase();
}
}
@@ -0,0 +1,17 @@
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;
}
}
@@ -0,0 +1,129 @@
package com.iluwatar.builder;
/**
*
* The class with many parameters.
*
*/
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;
public Profession getProfession() {
return profession;
}
public String getName() {
return name;
}
public HairType getHairType() {
return hairType;
}
public HairColor getHairColor() {
return hairColor;
}
public Armor getArmor() {
return armor;
}
public Weapon getWeapon() {
return weapon;
}
@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();
}
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 {
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 withHairType(HairType hairType) {
this.hairType = hairType;
return this;
}
public HeroBuilder withHairColor(HairColor hairColor) {
this.hairColor = hairColor;
return this;
}
public HeroBuilder withArmor(Armor armor) {
this.armor = armor;
return this;
}
public HeroBuilder withWeapon(Weapon weapon) {
this.weapon = weapon;
return this;
}
public Hero build() {
return new Hero(this);
}
}
}
@@ -0,0 +1,12 @@
package com.iluwatar.builder;
public enum Profession {
WARRIOR, THIEF, MAGE, PRIEST;
@Override
public String toString() {
return name().toLowerCase();
}
}
@@ -0,0 +1,12 @@
package com.iluwatar.builder;
public enum Weapon {
DAGGER, SWORD, AXE, WARHAMMER, BOW;
@Override
public String toString() {
return name().toLowerCase();
}
}