-
Notifications
You must be signed in to change notification settings - Fork 15
/
25Memento.go
89 lines (75 loc) · 1.57 KB
/
25Memento.go
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
/**
@author: Jason Pang
@desc:
@date: 2021/8/15
**/
package design
import (
"container/list"
"fmt"
)
/**
* @Author: Jason Pang
* @Description: 备忘录
*/
type Memento struct {
mario *Mario
}
func (m *Memento) GetMario() *Mario {
return m.mario
}
/**
* @Author: Jason Pang
* @Description: 管理备忘录
*/
type Caretaker struct {
stack *list.List
}
/**
* @Author: Jason Pang
* @Description: 保存备忘录
* @receiver c
* @param m
*/
func (c *Caretaker) Save(m *Memento) {
c.stack.PushBack(m)
}
/**
* @Author: Jason Pang
* @Description: 获取上一个备忘录
* @receiver c
* @return *Memento
*/
func (c *Caretaker) Pop() *Memento {
e := c.stack.Back()
c.stack.Remove(e)
return e.Value.(*Memento)
}
func mementoMain() {
caretaker := &Caretaker{
stack: list.New(),
}
mario := Mario{
status: &SmallMarioStatus{},
score: 0,
}
mario.status.SetMario(&mario)
mario.status.Name()
fmt.Println("-------------------获得蘑菇\n")
mario.status.ObtainMushroom()
mario.status.Name()
fmt.Println("-------------------获得斗篷\n")
mario.status.ObtainCape()
fmt.Println("-------------------备份一下,要打怪了,当前状态为\n")
mario.ShowInfo()
caretaker.Save(mario.CreateMemento())
fmt.Println("-------------------开始打怪\n")
mario.status.Name()
fmt.Println("-------------------遇到怪兽\n")
mario.status.MeetMonster()
fmt.Println("-------------------打怪失败,目前状态为\n")
mario.ShowInfo()
fmt.Println("-------------------恢复状态,重新打怪\n")
mario.SetMemento(caretaker.Pop())
mario.ShowInfo()
}