|
1 | 1 | package com.iluwatar.builder; |
2 | 2 |
|
3 | | -import com.iluwatar. builder.Hero.HeroBuilder; |
| 3 | +import com.iluwatar.builder.Hero.HeroBuilder; |
4 | 4 |
|
5 | 5 | /** |
6 | 6 | * |
7 | | - * The intention of the Builder pattern is to find a solution to the telescoping |
8 | | - * constructor anti-pattern. The telescoping constructor anti-pattern occurs when the |
9 | | - * increase of object constructor parameter combination leads to an exponential list |
10 | | - * of constructors. Instead of using numerous constructors, the builder pattern uses |
11 | | - * another object, a builder, that receives each initialization parameter step by step |
12 | | - * and then returns the resulting constructed object at once. |
| 7 | + * The intention of the Builder pattern is to find a solution to the telescoping constructor |
| 8 | + * anti-pattern. The telescoping constructor anti-pattern occurs when the increase of object |
| 9 | + * constructor parameter combination leads to an exponential list of constructors. Instead of using |
| 10 | + * numerous constructors, the builder pattern uses another object, a builder, that receives each |
| 11 | + * initialization parameter step by step and then returns the resulting constructed object at once. |
13 | 12 | * <p> |
14 | | - * The Builder pattern has another benefit. It can be used for objects that contain |
15 | | - * flat data (html code, SQL query, X.509 certificate...), that is to say, data that |
16 | | - * can't be easily edited. This type of data cannot be edited step by step and must |
17 | | - * be edited at once. The best way to construct such an object is to use a builder |
18 | | - * class. |
| 13 | + * The Builder pattern has another benefit. It can be used for objects that contain flat data (html |
| 14 | + * code, SQL query, X.509 certificate...), that is to say, data that can't be easily edited. This |
| 15 | + * type of data cannot be edited step by step and must be edited at once. The best way to construct |
| 16 | + * such an object is to use a builder class. |
19 | 17 | * <p> |
20 | | - * In this example we have the Builder pattern variation as described by Joshua Bloch in |
21 | | - * Effective Java 2nd Edition. |
| 18 | + * In this example we have the Builder pattern variation as described by Joshua Bloch in Effective |
| 19 | + * Java 2nd Edition. |
22 | 20 | * <p> |
23 | | - * We want to build {@link Hero} objects, but its construction is complex because of the |
24 | | - * many parameters needed. To aid the user we introduce {@link HeroBuilder} class. |
25 | | - * {@link HeroBuilder} takes the minimum parameters to build {@link Hero} object in its |
26 | | - * constructor. After that additional configuration for the {@link Hero} object can be |
27 | | - * done using the fluent {@link HeroBuilder} interface. When configuration is ready the |
28 | | - * build method is called to receive the final {@link Hero} object. |
| 21 | + * We want to build {@link Hero} objects, but its construction is complex because of the many |
| 22 | + * parameters needed. To aid the user we introduce {@link HeroBuilder} class. {@link HeroBuilder} |
| 23 | + * takes the minimum parameters to build {@link Hero} object in its constructor. After that |
| 24 | + * additional configuration for the {@link Hero} object can be done using the fluent |
| 25 | + * {@link HeroBuilder} interface. When configuration is ready the build method is called to receive |
| 26 | + * the final {@link Hero} object. |
29 | 27 | * |
30 | 28 | */ |
31 | 29 | public class App { |
32 | 30 |
|
33 | | - /** |
34 | | - * Program entry point |
35 | | - * @param args command line args |
36 | | - */ |
37 | | - public static void main(String[] args) { |
| 31 | + /** |
| 32 | + * Program entry point |
| 33 | + * |
| 34 | + * @param args command line args |
| 35 | + */ |
| 36 | + public static void main(String[] args) { |
38 | 37 |
|
39 | | - Hero mage = new HeroBuilder(Profession.MAGE, "Riobard") |
40 | | - .withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER) |
41 | | - .build(); |
42 | | - System.out.println(mage); |
| 38 | + Hero mage = |
| 39 | + new HeroBuilder(Profession.MAGE, "Riobard").withHairColor(HairColor.BLACK) |
| 40 | + .withWeapon(Weapon.DAGGER).build(); |
| 41 | + System.out.println(mage); |
43 | 42 |
|
44 | | - Hero warrior = new HeroBuilder(Profession.WARRIOR, "Amberjill") |
45 | | - .withHairColor(HairColor.BLOND) |
46 | | - .withHairType(HairType.LONG_CURLY).withArmor(Armor.CHAIN_MAIL) |
47 | | - .withWeapon(Weapon.SWORD).build(); |
48 | | - System.out.println(warrior); |
| 43 | + Hero warrior = |
| 44 | + new HeroBuilder(Profession.WARRIOR, "Amberjill").withHairColor(HairColor.BLOND) |
| 45 | + .withHairType(HairType.LONG_CURLY).withArmor(Armor.CHAIN_MAIL).withWeapon(Weapon.SWORD) |
| 46 | + .build(); |
| 47 | + System.out.println(warrior); |
49 | 48 |
|
50 | | - Hero thief = new HeroBuilder(Profession.THIEF, "Desmond") |
51 | | - .withHairType(HairType.BALD).withWeapon(Weapon.BOW).build(); |
52 | | - System.out.println(thief); |
| 49 | + Hero thief = |
| 50 | + new HeroBuilder(Profession.THIEF, "Desmond").withHairType(HairType.BALD) |
| 51 | + .withWeapon(Weapon.BOW).build(); |
| 52 | + System.out.println(thief); |
53 | 53 |
|
54 | | - } |
| 54 | + } |
55 | 55 | } |
0 commit comments