forked from rick2785/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHoagie.java
More file actions
96 lines (55 loc) · 1.8 KB
/
Hoagie.java
File metadata and controls
96 lines (55 loc) · 1.8 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// A Template Method Pattern contains a method that provides
// the steps of the algorithm. It allows subclasses to override
// some of the methods
public abstract class Hoagie {
boolean afterFirstCondiment = false;
// This is the Template Method
// Declare this method final to keep subclasses from
// changing the algorithm
final void makeSandwich(){
cutBun();
if(customerWantsMeat()){
addMeat();
// Here to handle new lines for spacing
afterFirstCondiment = true;
}
if(customerWantsCheese()){
if(afterFirstCondiment) { System.out.print("\n"); }
addCheese();
afterFirstCondiment = true;
}
if(customerWantsVegetables()){
if(afterFirstCondiment) { System.out.print("\n"); }
addVegetables();
afterFirstCondiment = true;
}
if(customerWantsCondiments()){
if(afterFirstCondiment) { System.out.print("\n"); }
addCondiments();
afterFirstCondiment = true;
}
wrapTheHoagie();
}
// These methods must be overridden by the extending subclasses
abstract void addMeat();
abstract void addCheese();
abstract void addVegetables();
abstract void addCondiments();
public void cutBun(){
System.out.println("The Hoagie is Cut");
}
// These are called hooks
// If the user wants to override these they can
// Use abstract methods when you want to force the user
// to override and use a hook when you want it to be optional
boolean customerWantsMeat() { return true; }
boolean customerWantsCheese() { return true; }
boolean customerWantsVegetables() { return true; }
boolean customerWantsCondiments() { return true; }
public void wrapTheHoagie(){
System.out.println("\nWrap the Hoagie");
}
public void afterFirstCondiment(){
System.out.println("\n");
}
}