builder method

在一个对象实例化的过程中,往往需要设置很多的参数
常见的有以下几种方法。

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

public class {
private final int servingSize; //(mL) required
private final int servings; //(per container) required
private final int calories; // optional
private final int fat; // optional
private final int sodium; //(mg) optional
private final int carbohydrate;//(g) optional

public (int servingSize, int servings) {
this(servingSize, servings, 0);
}

public (int servingSize, int servings,
int calories) {
this(servingSize, servings, calories, 0);
}

public (int servingSize, int servings,
int calories, int fat) {
this(servingSize, servings, calories, fat, 0);
}

public (int servingSize, int servings,
int calories, int fat, int sodium) {
this(servingSize, servings, calories, fat, sodium, 0);
}

public NutritionFacts(int servingSize, int servings,
int calories, int fat, int sodium, int carbohydrate) {
this.servingSize = servingSize;
this.servings = servings;
this.calories = calories;
this.fat = fat;
this.sodium = sodium;
this.carbohydrate = carbohydrate;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//JavaBeans pattern
public class NutritionFacts {
// Parameters initialized to default values (if any)
private int servingSize = -1; //(mL) required
private int servings = -1; //(per container) required
private int calories = 0; // optional
private int fat = 0; // optional
private int sodium = 0; //(mg) optional
private int carbohydrate = 0; //(g) optional


//Setters
public NutritionFacts(){ }
public void setServingSize(int val) {this.servingSize = val;}
public void setServings(int val) {this.servings = val;}
public void setCalories(int val) {this.calories = val;}
public void setFat(int val) {this.fat = val;}
public void setSodium(int val) {this.sodium = val;}
public void setCarbohydrate(int val) {this.carbohydrate = val;}

}
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
//Builder pattern
public class NutritionFacts {
private final int servingSize; //(mL) required
private final int servings; //(per container) required
private final int calories; // optional
private final int fat; // optional
private final int sodium; //(mg) optional
private final int carbohydrate;//(g) optional

public static class Builder {
//Required parameters
private final int servingSize;
private final int servings;

//Optional parameters - initialized to default values
private final int calories = 0;
private final int fat = 0;
private final int sodium = 0;
private final int carbohydrate = 0;

public Builder calories(int val)
{
this.calories = val;
return this;
}

public Builder fat(int val)
{
this.fat = val;
return this;
}

public Builder sodium(int val)
{
this.sodium = val;
return this;
}

public Builder carbohydrate(int val)
{
this.carbohydrate = val;
return this;
}

public NutritionFacts build() {
return new NutritionFacts(this);
}
}

public NutritionFacts(Builder builder) {
this.servingSize = builder.servingSize;
this.servings = builder.servings;
this.calories = builder.calories;
this.fat = builder.fat;
this.sodium = builder.sodium;
this.carbohydrate = builder.carbohydrate;
}
}


NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).calories(100).
sodium(35).carbohydrate(27).build();
//initialize in chain