forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
537 B
43 lines
537 B
3 years ago
|
|
||
|
```go
|
||
|
package memento
|
||
|
|
||
|
type Memento struct {
|
||
|
state int
|
||
|
}
|
||
|
|
||
|
func NewMemento(value int) *Memento {
|
||
|
return &Memento{state:value}
|
||
|
}
|
||
|
|
||
|
type Number struct {
|
||
|
value int
|
||
|
}
|
||
|
|
||
|
func NewNumber(value int) *Number {
|
||
|
return &Number{value: value}
|
||
|
}
|
||
|
|
||
|
func (n *Number)Double() {
|
||
|
n.value*=2
|
||
|
}
|
||
|
|
||
|
func (n *Number)Hide() {
|
||
|
n.value/=2
|
||
|
}
|
||
|
|
||
|
func (n *Number)Value() int {
|
||
|
return n.value
|
||
|
}
|
||
|
|
||
|
func (n *Number)CreateMemento() *Memento {
|
||
|
return NewMemento(n.value)
|
||
|
}
|
||
|
|
||
|
func (n *Number)ReginstateMemento(m *Memento) {
|
||
|
n.value = m.state
|
||
|
}
|
||
|
```
|
||
|
* 备忘录模式
|
||
|
|
||
|
*
|