aiyoudiao aiyoudiao
  • JavaScript
  • Vue
  • React
  • 低代码
  • 线性系统
  • 暂未分类
  • LeetCode
  • 算法
  • 数据结构
  • 设计模式
  • Other
  • PMP
  • Office
  • 面试
  • Bash
  • 流年往事
  • 经验片段
  • 读书杂感
  • 归档
  • 分类
  • 标签
  • 简介
  • 收藏
  • 有趣
  • 文档

码二

扫微信二维码,认识一下码二吧😉。
  • JavaScript
  • Vue
  • React
  • 低代码
  • 线性系统
  • 暂未分类
  • LeetCode
  • 算法
  • 数据结构
  • 设计模式
  • Other
  • PMP
  • Office
  • 面试
  • Bash
  • 流年往事
  • 经验片段
  • 读书杂感
  • 归档
  • 分类
  • 标签
  • 简介
  • 收藏
  • 有趣
  • 文档
  • LeetCode

  • 算法

  • 数据结构

  • 设计模式

    • 浅聊设计理念
    • 谈一谈设计原则
    • 工厂方法模式
    • 抽象工厂模式
    • 建造者模式
    • 原型模式
    • 单例模式
    • 桥接模式
    • 代理模式
    • 装饰器模式
    • 适配器模式
    • 享元模式
    • 组合模式
    • 外观模式
    • 观察者模式
    • 模板方法模式
    • 策略模式
    • 职责链模式
    • 状态模式
    • 迭代器模式
    • 访问者模式
    • 备忘录模式
      • 前言
      • 备忘录模式
    • 命令模式
    • 解释器模式
    • 中介者模式
  • Other

  • vue3设计与实现

  • 算法与设计
  • 设计模式
aiyoudiao
2022-02-25

备忘录模式

# 前言

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

# 备忘录模式

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

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

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

}
#设计模式
上次更新时间: 10年18月2023日 01时57分53秒
访问者模式
命令模式

← 访问者模式 命令模式 →

最近更新
01
01.数据结构导论一览.md
10-16
02
30.2023年06月04日.md
06-04
03
08.与测量相关.md
05-06
更多文章>
Theme by Vdoing | Copyright © 2017-2023 aiyoudiao 码二 备案号: 鄂ICP备2022002654号-1