备忘录模式用于保存程序内部状态到外部,又不希望暴露内部状态的情形。
程序内部状态使用窄接口船体给外部进行存储,从而不暴露程序实现细节。
备忘录模式同时可以离线保存内部状态,如保存到数据库,文件等。
memento.go
package mementoimport "fmt"type Memento interface{}type Game struct {hp, mp int
}type gameMemento struct {hp, mp int
}func (g *Game) Play(mpDelta, hpDelta int) {g.mp += mpDeltag.hp += hpDelta
}func (g *Game) Save() Memento {return &gameMemento{hp: g.hp,mp: g.mp,}
}func (g *Game) Load(m Memento) {gm := m.(*gameMemento)g.mp = gm.mpg.hp = gm.hp
}func (g *Game) Status() {fmt.Printf("Current HP:%d, MP:%d\n", g.hp, g.mp)
}
memento_test.go
package mementofunc ExampleGame() {game := &Game{hp: 10,mp: 10,}game.Status()progress := game.Save()game.Play(-2, -3)game.Status()game.Load(progress)game.Status()// Output:// Current HP:10, MP:10// Current HP:7, MP:8// Current HP:10, MP:10
}