前言
备忘录模式属于行为型模式,这个类型的设计模式总结出了 类、对象之间的经典交互方式,将类、对象的行为和使用解耦了,花式的去使用对象的行为来完成特定场景下的功能。
备忘录模式
使用场景:当你要对某一个对象的数据进行备份和回滚时,你可以使用备忘录模式。备忘录模式和原型模式有异曲同工之妙,从语义上讲,原型模式是创建型模式用于创建对象,而备忘录模式用于恢复数据某一刻状态、回滚数据某一刻的状态,是一种行为。
理解:这是一种类、对象之间的经典交互方式,将类、对象的行为和使用解耦了。备忘录,顾名思义,备忘录模式用于备份数据在某一个时间内的状态,类似于游戏存档,能存档自然能够读档。
namespace action_mode_08 {
interface IMemento {
store(key: string, game: Game): string
restore(key: string): Game | undefined
}
class GameStorage implements IMemento {
file: Map<string, Game> = new Map()
store(key: string, game: Game): string {
if (this.file.has(key)) {
console.log('已存在该记录,覆盖式存档')
}
const newGame = new Game(game.name)
newGame.hp = game.hp
newGame.mp = game.mp
newGame.level = game.level
newGame.killBoos = game.killBoos
newGame.grade = game.grade
this.file.set(key, newGame)
return key
}
restore(key: string): Game | undefined {
if (!this.file.has(key)) {
console.log('查无此存档')
}
return this.file.get(key)
}
}
class Game{
name: string
hp: number
mp: number
level: number
killBoos: number
grade: number
gameStorage: GameStorage
constructor(name: string) {
this.name = name
this.hp = 100
this.mp = 100
this.level = 1
this.killBoos = 0
this.grade = 1
this.gameStorage = new GameStorage()
}
praintInfo () {
console.log('===========游戏数据 展示开始 ==========')
console.log(`用户名:${this.name}`)
console.log(`所在关卡:${this.grade}`)
console.log(`等级:${this.level}`)
console.log(`血量:${this.hp}`)
console.log(`魔法:${this.mp}`)
console.log(`杀boos数:${this.killBoos}`)
console.log('===========游戏数据 展示结束 ==========')
}
play() {
console.log('-----------------打游戏----------------')
console.log(`用户名:${this.name}`)
console.log(`所在关卡:${this.grade}`)
console.log(`等级:${this.level}`)
console.log(`血量:${this.hp}`)
console.log(`魔法:${this.mp}`)
console.log(`杀boos数:${this.killBoos}`)
this.hp -= ~~(Math.random() * 10)
this.mp -= ~~(Math.random() * 10)
this.level ++
this.killBoos ++
this.grade ++
if (this.hp < 1) {
this.praintInfo()
console.log('血量低于 0,结束游戏。')
return false
}
return true
}
store(): string {
console.log('存档中')
return this.gameStorage.store(Date.now().toString(), this)
}
restore(key: string): Game | undefined {
console.log('存档回滚')
return this.gameStorage.restore(key)
}
}
let game = new Game('江湖百晓生')
game.praintInfo()
const flag = game.store()
game.play()
game.play()
game.play()
game.play()
game.play()
game.play()
game = game.restore(flag) as Game
game.praintInfo()
}