-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstate.java
79 lines (65 loc) · 1.84 KB
/
state.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
interface State {
void handle(Worker worker);
}
class Worker {
private State mood; //現在的工作心情
int time; //現在幾點
public Worker(State mood, int time) {
this.mood = mood;
this.time = time;
}
public State getMood() {
return mood;
}
public void setMood(State mood) {
this.mood = mood;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
void moodHandle(){
mood.handle(this);
}
}
class MorningMood implements State {
@Override
public void handle(Worker worker) {
if(worker.getTime() < 12)
System.out.printf("時間:%d點,早上工作,一條龍\n", worker.getTime());
else{
worker.setMood(new NoonMood());
worker.moodHandle();
}
}
}
class NoonMood implements State {
@Override
public void handle(Worker worker) {
if(worker.getTime() >= 12 && worker.getTime() < 18)
System.out.printf("時間:%d點,下午工作,一條蟲\n", worker.getTime());
else {
worker.setMood(new NightMood());
worker.moodHandle();
}
}
}
class NightMood implements State {
@Override
public void handle(Worker worker) {
if(worker.getTime() >= 18)
System.out.printf("時間:%d點,晚上工作,老子才不幹!\n", worker.getTime());
}
}
public class Main {
public static void main(String[] args) {
int startWorkeringTime = 8; /* 早上8點上班 */
Worker worker = new Worker(new MorningMood(), startWorkeringTime);
for(int i = startWorkeringTime; i < 24; ++i){
worker.moodHandle();
worker.setTime(worker.getTime() + 1);
}
}
}