forked from shabbirdwd53/design_patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBurger.java
More file actions
82 lines (72 loc) · 2.33 KB
/
Copy pathBurger.java
File metadata and controls
82 lines (72 loc) · 2.33 KB
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
/**
* Most Common Way to implement builder pattern is to have Builder class as innner class because : - You might have seen at most of the places. -Joshua Bloch's Builder pattern
* 1. We don't want to expose our constructor which eventually creates confusion at later stage.
* 2. We don't want to have in-consistent object by having setters exposed.
* 3. Now since constructor is private we will need inner class to access that. - Which will have same inputs what we have in class. But without getters.
*/
public class Burger {
private String size;
private boolean egg;
private boolean extraCheese;
private boolean mayonese;
private boolean onion;
private boolean lettuce;
private Burger(BurgerBuilder burgerBuilder) {
// Initialize all fields and you can also add validations.
}
public String getSize() {
return size;
}
public boolean isEgg() {
return egg;
}
public boolean isExtraCheese() {
return extraCheese;
}
public boolean isMayonese() {
return mayonese;
}
public boolean isOnion() {
return onion;
}
public boolean isLettuce() {
return lettuce;
}
//Return same object everytime that is builder instance
//Once build method invoke return actual object.
public static class BurgerBuilder {
private String size;
private boolean egg;
private boolean extraCheese;
private boolean mayonese;
private boolean onion;
private boolean lettuce;
public BurgerBuilder size(String size) {
this.size = size;
return this;
}
public BurgerBuilder egg(boolean egg) {
this.egg = egg;
return this;
}
public BurgerBuilder extraCheese(boolean extraCheese) {
this.extraCheese = extraCheese;
return this;
}
public BurgerBuilder mayonese(boolean mayonese) {
this.mayonese = mayonese;
return this;
}
public BurgerBuilder onion(boolean onion) {
this.onion = onion;
return this;
}
public BurgerBuilder lettuce(boolean lettuce) {
this.lettuce = lettuce;
return this;
}
public Burger build() {
return new Burger(this);
}
}
}