mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-15 08:17:06 +00:00
Reformat builder pattern - issue #224
This commit is contained in:
1 parent
e7b6542134
commit
3af06a3a3a
5 files changed
+31
-33
No files matched your search
@@ -4,51 +4,51 @@ import com.iluwatar. builder.Hero.HeroBuilder;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* The intention of the Builder pattern is to find a solution to the telescoping
|
* The intention of the Builder pattern is to find a solution to the telescoping constructor
|
||||||
* constructor anti-pattern. The telescoping constructor anti-pattern occurs when the
|
* anti-pattern. The telescoping constructor anti-pattern occurs when the increase of object
|
||||||
* increase of object constructor parameter combination leads to an exponential list
|
* constructor parameter combination leads to an exponential list of constructors. Instead of using
|
||||||
* of constructors. Instead of using numerous constructors, the builder pattern uses
|
* numerous constructors, the builder pattern uses another object, a builder, that receives each
|
||||||
* another object, a builder, that receives each initialization parameter step by step
|
* initialization parameter step by step and then returns the resulting constructed object at once.
|
||||||
* and then returns the resulting constructed object at once.
|
|
||||||
* <p>
|
* <p>
|
||||||
* The Builder pattern has another benefit. It can be used for objects that contain
|
* The Builder pattern has another benefit. It can be used for objects that contain flat data (html
|
||||||
* flat data (html code, SQL query, X.509 certificate...), that is to say, data that
|
* code, SQL query, X.509 certificate...), that is to say, data that can't be easily edited. This
|
||||||
* can't be easily edited. This type of data cannot be edited step by step and must
|
* type of data cannot be edited step by step and must be edited at once. The best way to construct
|
||||||
* be edited at once. The best way to construct such an object is to use a builder
|
* such an object is to use a builder class.
|
||||||
* class.
|
|
||||||
* <p>
|
* <p>
|
||||||
* In this example we have the Builder pattern variation as described by Joshua Bloch in
|
* In this example we have the Builder pattern variation as described by Joshua Bloch in Effective
|
||||||
* Effective Java 2nd Edition.
|
* Java 2nd Edition.
|
||||||
* <p>
|
* <p>
|
||||||
* We want to build {@link Hero} objects, but its construction is complex because of the
|
* We want to build {@link Hero} objects, but its construction is complex because of the many
|
||||||
* many parameters needed. To aid the user we introduce {@link HeroBuilder} class.
|
* parameters needed. To aid the user we introduce {@link HeroBuilder} class. {@link HeroBuilder}
|
||||||
* {@link HeroBuilder} takes the minimum parameters to build {@link Hero} object in its
|
* takes the minimum parameters to build {@link Hero} object in its constructor. After that
|
||||||
* constructor. After that additional configuration for the {@link Hero} object can be
|
* additional configuration for the {@link Hero} object can be done using the fluent
|
||||||
* done using the fluent {@link HeroBuilder} interface. When configuration is ready the
|
* {@link HeroBuilder} interface. When configuration is ready the build method is called to receive
|
||||||
* build method is called to receive the final {@link Hero} object.
|
* the final {@link Hero} object.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point
|
* Program entry point
|
||||||
|
*
|
||||||
* @param args command line args
|
* @param args command line args
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
Hero mage = new HeroBuilder(Profession.MAGE, "Riobard")
|
Hero mage =
|
||||||
.withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER)
|
new HeroBuilder(Profession.MAGE, "Riobard").withHairColor(HairColor.BLACK)
|
||||||
.build();
|
.withWeapon(Weapon.DAGGER).build();
|
||||||
System.out.println(mage);
|
System.out.println(mage);
|
||||||
|
|
||||||
Hero warrior = new HeroBuilder(Profession.WARRIOR, "Amberjill")
|
Hero warrior =
|
||||||
.withHairColor(HairColor.BLOND)
|
new HeroBuilder(Profession.WARRIOR, "Amberjill").withHairColor(HairColor.BLOND)
|
||||||
.withHairType(HairType.LONG_CURLY).withArmor(Armor.CHAIN_MAIL)
|
.withHairType(HairType.LONG_CURLY).withArmor(Armor.CHAIN_MAIL).withWeapon(Weapon.SWORD)
|
||||||
.withWeapon(Weapon.SWORD).build();
|
.build();
|
||||||
System.out.println(warrior);
|
System.out.println(warrior);
|
||||||
|
|
||||||
Hero thief = new HeroBuilder(Profession.THIEF, "Desmond")
|
Hero thief =
|
||||||
.withHairType(HairType.BALD).withWeapon(Weapon.BOW).build();
|
new HeroBuilder(Profession.THIEF, "Desmond").withHairType(HairType.BALD)
|
||||||
|
.withWeapon(Weapon.BOW).build();
|
||||||
System.out.println(thief);
|
System.out.println(thief);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ package com.iluwatar.builder;
|
|||||||
*/
|
*/
|
||||||
public enum HairType {
|
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;
|
||||||
|
|
||||||
|
|||||||
@@ -95,8 +95,7 @@ public class Hero {
|
|||||||
|
|
||||||
public HeroBuilder(Profession profession, String name) {
|
public HeroBuilder(Profession profession, String name) {
|
||||||
if (profession == null || name == null) {
|
if (profession == null || name == null) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException("profession and name can not be null");
|
||||||
"profession and name can not be null");
|
|
||||||
}
|
}
|
||||||
this.profession = profession;
|
this.profession = profession;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
|||||||
@@ -13,5 +13,4 @@ public enum Profession {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return name().toLowerCase();
|
return name().toLowerCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -13,5 +13,4 @@ public enum Weapon {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return name().toLowerCase();
|
return name().toLowerCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user