我学会了,备忘录模式

简介: 备忘录模式属于行为型模式,这个类型的设计模式总结出了 类、对象之间的经典交互方式,将类、对象的行为和使用解耦了,花式的去使用对象的行为来完成特定场景下的功能。

前言

备忘录模式属于行为型模式,这个类型的设计模式总结出了 类、对象之间的经典交互方式,将类、对象的行为和使用解耦了,花式的去使用对象的行为来完成特定场景下的功能。

备忘录模式

使用场景:当你要对某一个对象的数据进行备份和回滚时,你可以使用备忘录模式。备忘录模式和原型模式有异曲同工之妙,从语义上讲,原型模式是创建型模式用于创建对象,而备忘录模式用于恢复数据某一刻状态、回滚数据某一刻的状态,是一种行为。

理解:这是一种类、对象之间的经典交互方式,将类、对象的行为和使用解耦了。备忘录,顾名思义,备忘录模式用于备份数据在某一个时间内的状态,类似于游戏存档,能存档自然能够读档。

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()

}
目录
相关文章
|
3月前
|
设计模式 存储 Java
聊聊Java设计模式-备忘录模式
备忘录模式(Memento Design Pattern),也叫快照(Snapshot)模式。指在不违背封装原则前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便之后恢复对象为先前的状态。
38 0
聊聊Java设计模式-备忘录模式
|
4月前
|
存储 设计模式 Java
|
4月前
|
设计模式 安全 C++
行为型 备忘录模式
行为型 备忘录模式
23 1
|
4月前
行为型 状态模式
行为型 状态模式
17 0
|
6月前
|
设计模式 存储 数据库
设计模式~备忘录模式(memento)-22
备忘录模式(Memento Pattern)保存一个对象的某个状态,以便在适当的时候恢复对象。备忘录模式属于行为型模式。记录快照(瞬间状态)/存盘。 目录  (1)优点: (2)缺点: (3)使用场景: (4)注意事项: (5)应用实例: 代码
23 1
|
8月前
|
设计模式 存储 Java
设计模式-行为型模式:备忘录模式
设计模式-行为型模式:备忘录模式
|
11月前
|
设计模式 存储 Java
Java设计模式-备忘录模式(Memento)
Java设计模式-备忘录模式(Memento)
|
设计模式 存储 Java
Java设计模式 ->备忘录模式
Java设计模式 ->备忘录模式
84 0
|
存储 设计模式 算法
|
uml
状态模式与备忘录模式(1)
状态模式与备忘录模式(1)
75 0
状态模式与备忘录模式(1)