-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME
152 lines (128 loc) · 5.61 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
FluentBuilder makes implementing the Builder pattern as outlined in Item 2 of
Effective Java 2nd Edition by Josh Bloch easier. The Builder pattern outlined
in the book is useful when classes take a large number of parameters, when
many of the parameters are of the same type and/or when you have many
optional parameters and need to create a large number of constructors and/or
factory methods to handle all the cases.
FluentBuilder makes creating the builder simpler by removing the large amount
of boiler plate code that accompanies creating a builder. Consider the
example from Effective Java. The class with the builder shown in Effective
Java is reproduced below:
public class NutritionFacts
{
private final int servingSize;
private final int servings;
private final int calories;
private final int fat;
private final int sodium;
private final int carbohydrate;
public static class Builder
{
// Required parameters
private final int servingSize;
private final int servings;
// Optional parameters - initialized to default values
private int calories = 0;
private int fat = 0;
private int carbohydrate = 0;
private int sodium = 0;
public Builder(int servingSize, int servings)
{
this.servingSize = servingSize;
this.servings = servings;
}
public Builder calories(int val)
{
calories = val;
return this;
}
public Builder fat(int val)
{
fat = val;
return this;
}
public Builder carbohydrate(int val)
{
carbohydrate = val;
return this;
}
public Builder sodium(int val)
{
sodium = val;
return this;
}
public NutritionFacts build()
{
return new NutritionFacts(this);
}
}
private NutritionFacts(Builder builder)
{
servingSize = builder.servingSize;
servings = builder.servings;
calories = builder.calories;
fat = builder.fat;
sodium = builder.sodium;
carbohydrate = builder.carbohydrate;
}
}
You can see that there is large amount of simple boiler plate code that needs
to be written to implement the builder. Not only is this tedious to write
(and thus is unlikely to be done) but it also obscures the logic of the
class. In the example above most of the code for the class is in the builder
itself. In contrast FluentBuilder eliminates nearly all the boiler plate code
needed to construct a builder. This means almost all the code for the class
is concentrated on core logic for the class. Also the reduction in code means
that the Builder pattern can be more easily employed. Below is the same class
but this time reimplemented using FluentBuilder to construct the builder.
import org.a2sdl.builder.FluentBuilder;
import org.a2sdl.builder.annotation.DefaultValue;
public class NutritionFacts
{
private final int servingSize;
private final int servings;
@DefaultValue(intValue = 0)
private final int calories;
@DefaultValue(intValue = 0)
private final int fat;
@DefaultValue(intValue = 0)
private final int sodium;
@DefaultValue(intValue = 0)
private final int carbohydrate;
public NutritionFacts(int servingSize, int servings, int calories, int fat, int sodium, int carbohydrates)
{
this.servingSize = servingSize;
this.servings = servings;
this.calories = calories;
this.fat = fat;
this.sodium = sodium;
this.carbohydrates = carbohydrates;
}
public interface Builder
{
public Builder servingSize(int s);
public Builder servings(int s);
public Builder calories(int c);
public Builder fat(int f);
public Builder sodium(int s);
public Builder carbohydrates(int c);
public NutritionFacts build();
}
public static Builder builder() { return FluentBuilder.of(NutritionFacts.class); }
}
The version of NutritionFacts written using FluentBuilder eliminates nearly
all the boiler plate code. In place of the original code is an internal
public interface named "Builder" that defines the calls used for the builder,
the placement of the {@code @DefaultValue} annotation to set default values
on some fields and a static factory method that creates the builder. There is
no extra logic and boiler plate code needed to create the builder. The extra
code needed to use the Builder pattern is mostly limited to defining the
* no extra logic and boiler plate code needed to create the builder. The extra
code needed to use the Builder pattern is mostly limited to defining the
Builder interface in a class.
The FluentBuilder has a couple of simple steps that must be followed to use
it:
1. There must be a public constructor with a parameter list for each property in the class. The parameters must be in the same order as the property declarations.
2. The methods in the Builder interface must have the same name as the properties in the class.
3. Only primitive (and their equivalent non-primitive type (eg. int and Integer) and String may have default values assigned to them. This limitation is an annotation limitation (you can't have an annotatin that takes an arbitrary type)
4. The Builder interface must have a build() method to construct the bean.