我学会了,备忘录模式

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

前言

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

备忘录模式

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

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

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

}
目录
相关文章
|
弹性计算
2023年阿里云服务器多少钱1年?免费试用版不要钱,收费版108元1年起
2023年购买阿里云服务器多少钱1年?如果是申请试用版云服务器就不需要钱,现在轻量应用服务器最低108元1年,通用型u1云服务器最低1532.04元1年,计算型c7云服务器最低2129.41元1年,通用型g7云服务器最低2608.62元1年,内存型r7云服务器最低3325.97元1年,不过阿里云增加了不少免费试用版云服务器,现在最高可以申请到4核16G配置的云服务器免费试用1个月,最长可以申请2核4G配置的云服务器免费试用3个月。
2023年阿里云服务器多少钱1年?免费试用版不要钱,收费版108元1年起
|
JavaScript 前端开发 Windows
使用webdriver实现基于GUI的测试
本文介绍的webdriver是playwright的插件
183 1
|
安全 Java 网络安全
如何在Java中处理SSLHandshakeException异常?
如何在Java中处理SSLHandshakeException异常?
2687 1
【高阶数据结构】图 -- 详解(上)
【高阶数据结构】图 -- 详解(上)
|
缓存 前端开发 JavaScript
前端铜九铁十面试必备八股文——性能优化
前端铜九铁十面试必备八股文——性能优化
417 0
|
人工智能
微软变“渣男”!据悉已投资法国开源 AI 独角兽Mistral,OpenAI成“备胎”了?
【2月更文挑战第8天】微软变“渣男”!据悉已投资法国开源 AI 独角兽Mistral,OpenAI成“备胎”了?
354 3
微软变“渣男”!据悉已投资法国开源 AI 独角兽Mistral,OpenAI成“备胎”了?
|
存储 数据采集 物联网
《物联网技术》课程笔记——第三章 物联网感知技术之标识技术
《物联网技术》课程笔记——第三章 物联网感知技术之标识技术
|
存储 编解码 Windows
ABB PP886 紧凑型产品套件硬件选择器
ABB PP886 紧凑型产品套件硬件选择器
550 3
|
算法 数据可视化
【视觉高级篇】25 # 如何用法线贴图模拟真实物体表面
【视觉高级篇】25 # 如何用法线贴图模拟真实物体表面
577 0
【视觉高级篇】25 # 如何用法线贴图模拟真实物体表面
晶振的作用,高速晶振优缺点
晶振的作用,高速晶振优缺点
495 0