-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBuilder.java
60 lines (50 loc) · 1.45 KB
/
Builder.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
public class Builder {
public static void main(String[] args){
Batman.AlfredBuilder alfredBuilder = new Batman.AlfredBuilder();
Batman batman = alfredBuilder.setGun("M22").setWeapon("Sword").setVehicle("Batmobile").build();
System.out.println(batman.getGun());
System.out.println(batman.getVehicle());
System.out.println(batman.getWeapon());
}
}
class Batman{
private final String vehicle;
private final String gun;
private final String weapon;
public Batman(AlfredBuilder alfredBuilder) {
this.vehicle = alfredBuilder.vehicle;
this.gun = alfredBuilder.gun;
this.weapon = alfredBuilder.weapon;
}
static class AlfredBuilder{
private String vehicle;
private String gun;
private String weapon;
AlfredBuilder(){
}
public AlfredBuilder setVehicle(String vehicle) {
this.vehicle = vehicle;
return this;
}
public AlfredBuilder setGun(String gun) {
this.gun = gun;
return this;
}
public AlfredBuilder setWeapon(String weapon) {
this.weapon = weapon;
return this;
}
public Batman build(){
return new Batman(this);
}
}
public String getVehicle() {
return vehicle;
}
public String getGun() {
return gun;
}
public String getWeapon() {
return weapon;
}
}