Merge pull request #281 from ankurkaushal/master
Reformat according to google style guide
This commit is contained in:
@ -3,72 +3,60 @@ package com.iluwatar.stepbuilder;
|
||||
/**
|
||||
* Step Builder Pattern
|
||||
*
|
||||
* <p><b>Intent</b>
|
||||
* <br/>
|
||||
* An extension of the Builder pattern that fully guides the user
|
||||
* through the creation of the object with no chances of confusion.
|
||||
* <br/>
|
||||
* The user experience will be much more improved by the fact that
|
||||
* he will only see the next step methods available, NO build method
|
||||
* until is the right time to build the object.
|
||||
* <p>
|
||||
* <b>Intent</b> <br/>
|
||||
* An extension of the Builder pattern that fully guides the user through the creation of the object
|
||||
* with no chances of confusion. <br/>
|
||||
* The user experience will be much more improved by the fact that he will only see the next step
|
||||
* methods available, NO build method until is the right time to build the object.
|
||||
*
|
||||
* <p><b>Implementation</b>
|
||||
* </br>
|
||||
* <ul>The concept is simple:
|
||||
* <p>
|
||||
* <b>Implementation</b> </br>
|
||||
* <ul>
|
||||
* The concept is simple:
|
||||
*
|
||||
* <li>Write creational steps inner classes or interfaces where each
|
||||
* method knows what can be displayed next.</li>
|
||||
* <li>Write creational steps inner classes or interfaces where each method knows what can be
|
||||
* displayed next.</li>
|
||||
*
|
||||
* <li>Implement all your steps interfaces in an inner static class.</li>
|
||||
* <li>Implement all your steps interfaces in an inner static class.</li>
|
||||
*
|
||||
* <li>Last step is the BuildStep, in charge of creating the object
|
||||
* you need to build.</li>
|
||||
* <li>Last step is the BuildStep, in charge of creating the object you need to build.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p><b>Applicability</b>
|
||||
* <br/>
|
||||
* Use the Step Builder pattern when the algorithm for creating a
|
||||
* complex object should be independent of the parts that make up
|
||||
* the object and how they're assembled the construction process must
|
||||
* allow different representations for the object that's constructed
|
||||
* when in the process of constructing the order is important.
|
||||
* <p>
|
||||
* <b>Applicability</b> <br/>
|
||||
* Use the Step Builder pattern when the algorithm for creating a complex object should be
|
||||
* independent of the parts that make up the object and how they're assembled the construction
|
||||
* process must allow different representations for the object that's constructed when in the
|
||||
* process of constructing the order is important.
|
||||
* <p>
|
||||
* http://rdafbn.blogspot.co.uk/2012/07/step-builder-pattern_28.html
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
Character warrior = CharacterStepBuilder.newBuilder()
|
||||
.name("Amberjill")
|
||||
.fighterClass("Paladin")
|
||||
.withWeapon("Sword")
|
||||
.noAbilities()
|
||||
.build();
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
System.out.println(warrior);
|
||||
Character warrior =
|
||||
CharacterStepBuilder.newBuilder().name("Amberjill").fighterClass("Paladin")
|
||||
.withWeapon("Sword").noAbilities().build();
|
||||
|
||||
Character mage = CharacterStepBuilder.newBuilder()
|
||||
.name("Riobard")
|
||||
.wizardClass("Sorcerer")
|
||||
.withSpell("Fireball")
|
||||
.withAbility("Fire Aura")
|
||||
.withAbility("Teleport")
|
||||
.noMoreAbilities()
|
||||
.build();
|
||||
System.out.println(warrior);
|
||||
|
||||
System.out.println(mage);
|
||||
Character mage =
|
||||
CharacterStepBuilder.newBuilder().name("Riobard").wizardClass("Sorcerer")
|
||||
.withSpell("Fireball").withAbility("Fire Aura").withAbility("Teleport")
|
||||
.noMoreAbilities().build();
|
||||
|
||||
Character thief = CharacterStepBuilder.newBuilder()
|
||||
.name("Desmond")
|
||||
.fighterClass("Rogue")
|
||||
.noWeapon()
|
||||
.build();
|
||||
System.out.println(mage);
|
||||
|
||||
System.out.println(thief);
|
||||
}
|
||||
Character thief =
|
||||
CharacterStepBuilder.newBuilder().name("Desmond").fighterClass("Rogue").noWeapon().build();
|
||||
|
||||
System.out.println(thief);
|
||||
}
|
||||
}
|
||||
|
@ -7,76 +7,76 @@ import java.util.List;
|
||||
*/
|
||||
public class Character {
|
||||
|
||||
private String name;
|
||||
private String fighterClass;
|
||||
private String wizardClass;
|
||||
private String weapon;
|
||||
private String spell;
|
||||
private List<String> abilities;
|
||||
private String name;
|
||||
private String fighterClass;
|
||||
private String wizardClass;
|
||||
private String weapon;
|
||||
private String spell;
|
||||
private List<String> abilities;
|
||||
|
||||
public Character(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public Character(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getFighterClass() {
|
||||
return fighterClass;
|
||||
}
|
||||
public String getFighterClass() {
|
||||
return fighterClass;
|
||||
}
|
||||
|
||||
public void setFighterClass(String fighterClass) {
|
||||
this.fighterClass = fighterClass;
|
||||
}
|
||||
public void setFighterClass(String fighterClass) {
|
||||
this.fighterClass = fighterClass;
|
||||
}
|
||||
|
||||
public String getWizardClass() {
|
||||
return wizardClass;
|
||||
}
|
||||
public String getWizardClass() {
|
||||
return wizardClass;
|
||||
}
|
||||
|
||||
public void setWizardClass(String wizardClass) {
|
||||
this.wizardClass = wizardClass;
|
||||
}
|
||||
public void setWizardClass(String wizardClass) {
|
||||
this.wizardClass = wizardClass;
|
||||
}
|
||||
|
||||
public String getWeapon() {
|
||||
return weapon;
|
||||
}
|
||||
public String getWeapon() {
|
||||
return weapon;
|
||||
}
|
||||
|
||||
public void setWeapon(String weapon) {
|
||||
this.weapon = weapon;
|
||||
}
|
||||
public void setWeapon(String weapon) {
|
||||
this.weapon = weapon;
|
||||
}
|
||||
|
||||
public String getSpell() {
|
||||
return spell;
|
||||
}
|
||||
public String getSpell() {
|
||||
return spell;
|
||||
}
|
||||
|
||||
public void setSpell(String spell) {
|
||||
this.spell = spell;
|
||||
}
|
||||
public void setSpell(String spell) {
|
||||
this.spell = spell;
|
||||
}
|
||||
|
||||
public List<String> getAbilities() {
|
||||
return abilities;
|
||||
}
|
||||
public List<String> getAbilities() {
|
||||
return abilities;
|
||||
}
|
||||
|
||||
public void setAbilities(List<String> abilities) {
|
||||
this.abilities = abilities;
|
||||
}
|
||||
public void setAbilities(List<String> abilities) {
|
||||
this.abilities = abilities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("This is a ");
|
||||
sb.append(fighterClass != null ? fighterClass : wizardClass);
|
||||
sb.append(" named ");
|
||||
sb.append(name);
|
||||
sb.append(" armed with a ");
|
||||
sb.append(weapon != null ? weapon : spell != null ? spell : "with nothing");
|
||||
sb.append(abilities != null ? (" and wielding " + abilities + " abilities") : "");
|
||||
sb.append(".");
|
||||
return sb.toString();
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("This is a ");
|
||||
sb.append(fighterClass != null ? fighterClass : wizardClass);
|
||||
sb.append(" named ");
|
||||
sb.append(name);
|
||||
sb.append(" armed with a ");
|
||||
sb.append(weapon != null ? weapon : spell != null ? spell : "with nothing");
|
||||
sb.append(abilities != null ? (" and wielding " + abilities + " abilities") : "");
|
||||
sb.append(".");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
@ -8,160 +8,158 @@ import java.util.List;
|
||||
*/
|
||||
public class CharacterStepBuilder {
|
||||
|
||||
private CharacterStepBuilder() {
|
||||
}
|
||||
private CharacterStepBuilder() {}
|
||||
|
||||
public static NameStep newBuilder() {
|
||||
return new CharacterSteps();
|
||||
}
|
||||
public static NameStep newBuilder() {
|
||||
return new CharacterSteps();
|
||||
}
|
||||
|
||||
/**
|
||||
* First Builder Step in charge of the Character name.
|
||||
* Next Step available : ClassStep
|
||||
*/
|
||||
public interface NameStep {
|
||||
ClassStep name(String name);
|
||||
}
|
||||
/**
|
||||
* First Builder Step in charge of the Character name. Next Step available : ClassStep
|
||||
*/
|
||||
public interface NameStep {
|
||||
ClassStep name(String name);
|
||||
}
|
||||
|
||||
/**
|
||||
* This step is in charge of setting the Character class (fighter or wizard).
|
||||
* Fighter choice : Next Step available : WeaponStep
|
||||
* Wizard choice : Next Step available : SpellStep
|
||||
*/
|
||||
public interface ClassStep {
|
||||
WeaponStep fighterClass(String fighterClass);
|
||||
SpellStep wizardClass(String wizardClass);
|
||||
}
|
||||
/**
|
||||
* This step is in charge of setting the Character class (fighter or wizard). Fighter choice :
|
||||
* Next Step available : WeaponStep Wizard choice : Next Step available : SpellStep
|
||||
*/
|
||||
public interface ClassStep {
|
||||
WeaponStep fighterClass(String fighterClass);
|
||||
|
||||
/**
|
||||
* This step is in charge of the weapon.
|
||||
* Weapon choice : Next Step available : AbilityStep
|
||||
* No weapon choice : Next Step available : BuildStep
|
||||
*/
|
||||
public interface WeaponStep {
|
||||
AbilityStep withWeapon(String weapon);
|
||||
BuildStep noWeapon();
|
||||
}
|
||||
SpellStep wizardClass(String wizardClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* This step is in charge of the spell.
|
||||
* Spell choice : Next Step available : AbilityStep
|
||||
* No spell choice : Next Step available : BuildStep
|
||||
*/
|
||||
public interface SpellStep {
|
||||
AbilityStep withSpell(String spell);
|
||||
BuildStep noSpell();
|
||||
}
|
||||
/**
|
||||
* This step is in charge of the weapon. Weapon choice : Next Step available : AbilityStep No
|
||||
* weapon choice : Next Step available : BuildStep
|
||||
*/
|
||||
public interface WeaponStep {
|
||||
AbilityStep withWeapon(String weapon);
|
||||
|
||||
/**
|
||||
* This step is in charge of abilities.
|
||||
* Next Step available : BuildStep
|
||||
*/
|
||||
public interface AbilityStep {
|
||||
AbilityStep withAbility(String ability);
|
||||
BuildStep noMoreAbilities();
|
||||
BuildStep noAbilities();
|
||||
}
|
||||
BuildStep noWeapon();
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the final step in charge of building the Character Object.
|
||||
* Validation should be here.
|
||||
*/
|
||||
public interface BuildStep {
|
||||
Character build();
|
||||
}
|
||||
/**
|
||||
* This step is in charge of the spell. Spell choice : Next Step available : AbilityStep No spell
|
||||
* choice : Next Step available : BuildStep
|
||||
*/
|
||||
public interface SpellStep {
|
||||
AbilityStep withSpell(String spell);
|
||||
|
||||
BuildStep noSpell();
|
||||
}
|
||||
|
||||
/**
|
||||
* This step is in charge of abilities. Next Step available : BuildStep
|
||||
*/
|
||||
public interface AbilityStep {
|
||||
AbilityStep withAbility(String ability);
|
||||
|
||||
BuildStep noMoreAbilities();
|
||||
|
||||
BuildStep noAbilities();
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the final step in charge of building the Character Object. Validation should be here.
|
||||
*/
|
||||
public interface BuildStep {
|
||||
Character build();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Step Builder implementation.
|
||||
*/
|
||||
private static class CharacterSteps
|
||||
implements NameStep, ClassStep, WeaponStep, SpellStep, AbilityStep, BuildStep {
|
||||
/**
|
||||
* Step Builder implementation.
|
||||
*/
|
||||
private static class CharacterSteps implements NameStep, ClassStep, WeaponStep, SpellStep,
|
||||
AbilityStep, BuildStep {
|
||||
|
||||
private String name;
|
||||
private String fighterClass;
|
||||
private String wizardClass;
|
||||
private String weapon;
|
||||
private String spell;
|
||||
private List<String> abilities = new ArrayList<>();
|
||||
private String name;
|
||||
private String fighterClass;
|
||||
private String wizardClass;
|
||||
private String weapon;
|
||||
private String spell;
|
||||
private List<String> abilities = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public ClassStep name(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public ClassStep name(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WeaponStep fighterClass(String fighterClass) {
|
||||
this.fighterClass = fighterClass;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public WeaponStep fighterClass(String fighterClass) {
|
||||
this.fighterClass = fighterClass;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SpellStep wizardClass(String wizardClass) {
|
||||
this.wizardClass = wizardClass;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public SpellStep wizardClass(String wizardClass) {
|
||||
this.wizardClass = wizardClass;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbilityStep withWeapon(String weapon) {
|
||||
this.weapon = weapon;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public AbilityStep withWeapon(String weapon) {
|
||||
this.weapon = weapon;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BuildStep noWeapon() {
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public BuildStep noWeapon() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbilityStep withSpell(String spell) {
|
||||
this.spell = spell;
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public AbilityStep withSpell(String spell) {
|
||||
this.spell = spell;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BuildStep noSpell() {
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public BuildStep noSpell() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbilityStep withAbility(String ability) {
|
||||
this.abilities.add(ability);
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public AbilityStep withAbility(String ability) {
|
||||
this.abilities.add(ability);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BuildStep noMoreAbilities() {
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public BuildStep noMoreAbilities() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BuildStep noAbilities() {
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public BuildStep noAbilities() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Character build() {
|
||||
Character character = new Character(name);
|
||||
@Override
|
||||
public Character build() {
|
||||
Character character = new Character(name);
|
||||
|
||||
if (fighterClass != null) {
|
||||
character.setFighterClass(fighterClass);
|
||||
} else {
|
||||
character.setWizardClass(wizardClass);
|
||||
}
|
||||
if (fighterClass != null) {
|
||||
character.setFighterClass(fighterClass);
|
||||
} else {
|
||||
character.setWizardClass(wizardClass);
|
||||
}
|
||||
|
||||
if (weapon != null) {
|
||||
character.setWeapon(weapon);
|
||||
} else {
|
||||
character.setSpell(spell);
|
||||
}
|
||||
if (weapon != null) {
|
||||
character.setWeapon(weapon);
|
||||
} else {
|
||||
character.setSpell(spell);
|
||||
}
|
||||
|
||||
if (!abilities.isEmpty()) {
|
||||
character.setAbilities(abilities);
|
||||
}
|
||||
if (!abilities.isEmpty()) {
|
||||
character.setAbilities(abilities);
|
||||
}
|
||||
|
||||
return character;
|
||||
}
|
||||
}
|
||||
return character;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,9 +9,9 @@ import org.junit.Test;
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user