-
Notifications
You must be signed in to change notification settings - Fork 0
/
memento.go
51 lines (39 loc) · 886 Bytes
/
memento.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
// Package x06memento 备忘录
package x06memento
type Memento struct {
state string
}
func (m *Memento) State() string {
return m.state
}
type Originator struct {
state string
}
func (o *Originator) Update(state string) {
o.state = state
}
func (o *Originator) Compare(state string) bool {
return o.state == state
}
func (o *Originator) CreateMemento() *Memento {
return &Memento{o.state}
}
func (o *Originator) SetMemento(memento *Memento) {
o.state = memento.state
}
func NewOriginator(state string) *Originator {
return &Originator{state}
}
type Caretaker struct {
memento map[string]*Memento
}
func (c *Caretaker) Get(name string) (memento *Memento) {
memento, _ = c.memento[name]
return
}
func (c *Caretaker) Add(name string, memento Memento) {
c.memento[name] = &memento
}
func NewCaretaker() *Caretaker {
return &Caretaker{make(map[string]*Memento)}
}