-
Notifications
You must be signed in to change notification settings - Fork 311
/
BuilderPattern.java
102 lines (102 loc) · 1.63 KB
/
BuilderPattern.java
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
public class BuilderPattern {
2
static class Coffee {
3
private Coffee(Builder builder) {
4
this.type = builder.type;
5
this.sugar = builder.sugar;
6
this.milk = builder.milk;
7
this.size = builder.size;
8
}
9
10
private String type;
11
private boolean sugar;
12
private boolean milk;
13
private String size;
14
15
public static class Builder {
16
private String type;
17
private boolean sugar;
18
private boolean milk;
19
private String size;
20
21
public Builder(String type) {
22
this.type = type;
23
}
24
25
public Builder sugar(boolean value) {
26
this.sugar = value;
27
return this;
28
}
29
30
public Builder milk(boolean value) {
31
this.milk = value;
32
return this;
33
}
34
35
public Builder size(String value) {
36
this.size = value;
37
return this;
38
}
39
40
public Coffee build() {
41
return new Coffee(this);
42
}
43
}
44
45
@Override
46
public String toString() {
47
return String.format("Coffee [type=%s, sugar=%s, milk=%s, size=%s]", this.type, sugar, milk, size);
48
}
49
50
}
51
52
public static void main(String[] args) {
53
Coffee coffee = new BuilderPattern.Coffee.Builder("Mocha").milk(true).sugar(false).size("Large").build();
54
System.out.println(coffee);
55
56
}
57
}