The builder pattern is defined in design mode as a pattern for building complex objects that often require multiple-step initialization or assignment to complete. So, in the actual development process, where do we fit the builder model? It is a good practice to use builder mode instead of multi-parameter constructors.
We often face the writing of such an implementation class (assuming that the class is called dodocontact), which has multiple constructors,
Dodocontact (String name);
Dodocontact (String name, int age);
Dodocontact (string name, int age, string address);
Dodocontact (string name, int age, string address, int cardID);
The main purpose of such a series of constructors is to provide more customer invocation options to handle different construction requests. This approach is common and effective, but it has many drawbacks. The author of the class has to write a constructor for various combinations of parameters, and it also needs to set the default parameter values, which is an attentive and tedious task. Second, the flexibility of such a constructor is not high, and you have to provide some meaningless parameter values at the time of invocation, for example, Dodocontact ("Ace",-1, "SH"), obviously the age of negative is meaningless, but you do not do so, to conform to Java specifications. If such code is released, the maintainer behind it will have a headache because he doesn't know what the 1 means. For such cases, it is very well suited to use the builder mode. The key point of the builder mode is to complete the object construction process through an agent. This is the agent's responsibility to complete the steps of the build, and it is also easy to expand. Here's a piece of code that's rewritten from effective Java:
123456789101112131415161718192021222324252627 |
public
class
DoDoContact {
private
final
int age;
private
final
int
safeID;
private
final
String name;
private
final
String address;
public
int
getAge() {
return
age;
}
public
int
getSafeID() {
return
safeID;
}
public
String getName() {
return
name;
}
public String getAddress() {
return
address;
}
public
static
class
Builder {
private
int age =
0
;
private
int
safeID =
0
;
private
String name =
null
;
private
String address =
null
;
|
123456789101112131415161718192021222324252627282930313233 |
// 构建的步骤
public
Builder(String name) {
this
.name = name;
}
public
Builder age(
int
val) {
age = val;
return
this
;
}
public
Builder safeID(
int
val) {
safeID = val;
return
this
;
}
public
Builder address(String val) {
address = val;
return
this
;
}
public
DoDoContact build() {
// 构建,返回一个新对象
return
new
DoDoContact(
this
);
}
}
private
DoDoContact(Builder b) {
age = b.age;
safeID = b.safeID;
name = b.name;
address = b.address;
}
}
|
Finally, the client program can be very flexible to build this object.
1234 |
DoDoContact ddc =
new
DoDoContact.Builder(
"Ace"
).age(
10
)
.address(
"beijing"
).build();
System.out.println(
"name=" + ddc.getName() +
"age ="
+ ddc.getAge()
+
"address"
+ ddc.getAddress());
|
The application of builder mode in Java