-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmomemto.java
102 lines (78 loc) · 2.04 KB
/
momemto.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
interface Gamer {
void attack();
}
class Caretaker {
private Momemto momento;
public Momemto getMomento() {
return momento;
}
public void setMomento(Momemto momento) {
this.momento = momento;
}
}
class Momemto {
private String state;
public Momemto(String state) {
this.state = state;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
abstract class AbstractGamer implements Gamer {
private String state;
public AbstractGamer(String state) {
this.state = state;
}
void setMomemto(Momemto momemto){
this.state = momemto.getState();
}
Momemto createMomemto(){
return new Momemto(this.state);
}
public void showState(){
System.out.printf("State: %s\n", this.state);
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
class SwordMan extends AbstractGamer {
public SwordMan(String state) {
super(state);
}
@Override
public void attack() {
System.out.println("受我一刀吧!");
setState("Game Over");
}
}
class Wizard extends AbstractGamer {
public Wizard(String state) {
super(state);
}
@Override
public void attack() {
System.out.println("受我一咒吧!");
setState("Game Over");
}
}
public class Main {
public static void main(String[] args){
AbstractGamer gamer1 = new SwordMan( "Alive");
Caretaker caretaker = new Caretaker();
caretaker.setMomento(gamer1.createMomemto());
System.out.println("進入大魔王關卡");
gamer1.attack(); /* 攻擊一下就被大魔王打死了... */
gamer1.showState();
System.out.println("重新開始");
gamer1.setMomemto(caretaker.getMomento()); /* 回复到一開始狀態,再打一次 */
gamer1.showState();
}
}