首页 > 其他分享 >设计模式(二十六)----行为型模式之备忘录模式

设计模式(二十六)----行为型模式之备忘录模式

时间:2023-03-20 21:58:58浏览次数:36  
标签:int void atk 模式 ---- vit 设计模式 public def

1 概述

备忘录模式提供了一种状态恢复的实现机制,使得用户可以方便地回到一个特定的历史步骤,当新的状态无效或者存在问题时,可以使用暂时存储起来的备忘录将状态复原,很多软件都提供了撤销(Undo)操作,如 Word、记事本、Photoshop、IDEA等软件在编辑时按 Ctrl+Z 组合键时能撤销当前操作,使文档恢复到之前的状态;还有在 浏览器 中的后退键、数据库事务管理中的回滚操作、玩游戏时的中间结果存档功能、数据库与操作系统的备份操作、棋类游戏中的悔棋功能等都属于这类。

定义:

又叫快照模式,在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便以后当需要时能将该对象恢复到原先保存的状态。

2 结构

备忘录模式的主要角色如下:

  • 发起人(Originator)角色:记录当前时刻的内部状态信息,提供创建备忘录和恢复备忘录数据的功能,实现其他业务功能,它可以访问备忘录里的所有信息。

  • 备忘录(Memento)角色:负责存储发起人的内部状态,在需要的时候提供这些内部状态给发起人。

  • 管理者(Caretaker)角色:对备忘录进行管理,提供保存与获取备忘录的功能,但其不能对备忘录的内容进行访问与修改。

备忘录有两个等效的接口:

  • 窄接口:管理者(Caretaker)对象(和其他发起人对象之外的任何对象)看到的是备忘录的窄接口(narrow Interface),这个窄接口只允许他把备忘录对象传给其他的对象。

  • 宽接口:与管理者看到的窄接口相反,发起人对象可以看到一个宽接口(wide Interface),这个宽接口允许它读取所有的数据,以便根据这些数据恢复这个发起人对象的内部状态。

3 案例实现

【例】游戏挑战BOSS

游戏中的某个场景,一游戏角色有生命力、攻击力、防御力等数据,在打Boss前和后一定会不一样的,我们允许玩家如果感觉与Boss决斗的效果不理想可以让游戏恢复到决斗之前的状态。

要实现上述案例,有两种方式:

  • “白箱”备忘录模式

  • “黑箱”备忘录模式

3.1 “白箱”备忘录模式

备忘录角色对任何对象都提供一个接口,即宽接口,备忘录角色的内部所存储的状态就对所有对象公开。类图如下:

代码如下:

//游戏角色类(属于发起人角色)
public class GameRole {
    private int vit; //生命力
    private int atk; //攻击力
    private int def; //防御力
​
    //初始化状态
    public void initState() {
        this.vit = 100;
        this.atk = 100;
        this.def = 100;
    }
​
    //战斗
    public void fight() {
        this.vit = 0;
        this.atk = 0;
        this.def = 0;
    }
​
    //保存角色状态
    public RoleStateMemento saveState() {
        return new RoleStateMemento(vit, atk, def);
    }
​
    //回复角色状态
    public void recoverState(RoleStateMemento roleStateMemento) {
        this.vit = roleStateMemento.getVit();
        this.atk = roleStateMemento.getAtk();
        this.def = roleStateMemento.getDef();
    }
​
    public void stateDisplay() {
        System.out.println("角色生命力:" + vit);
        System.out.println("角色攻击力:" + atk);
        System.out.println("角色防御力:" + def);
    }
​
    public int getVit() {
        return vit;
    }
​
    public void setVit(int vit) {
        this.vit = vit;
    }
​
    public int getAtk() {
        return atk;
    }
​
    public void setAtk(int atk) {
        this.atk = atk;
    }
​
    public int getDef() {
        return def;
    }
​
    public void setDef(int def) {
        this.def = def;
    }
}
​
//游戏状态存储类(备忘录类)
public class RoleStateMemento {
    private int vit;
    private int atk;
    private int def;
​
    public RoleStateMemento(int vit, int atk, int def) {
        this.vit = vit;
        this.atk = atk;
        this.def = def;
    }
​
    public int getVit() {
        return vit;
    }
​
    public void setVit(int vit) {
        this.vit = vit;
    }
​
    public int getAtk() {
        return atk;
    }
​
    public void setAtk(int atk) {
        this.atk = atk;
    }
​
    public int getDef() {
        return def;
    }
​
    public void setDef(int def) {
        this.def = def;
    }
}
​
//角色状态管理者类
public class RoleStateCaretaker {
    private RoleStateMemento roleStateMemento;
​
    public RoleStateMemento getRoleStateMemento() {
        return roleStateMemento;
    }
​
    public void setRoleStateMemento(RoleStateMemento roleStateMemento) {
        this.roleStateMemento = roleStateMemento;
    }
}
​
//测试类
public class Client {
    public static void main(String[] args) {
        System.out.println("------------大战Boss前------------");
        //大战Boss前
        GameRole gameRole = new GameRole();
        gameRole.initState();
        gameRole.stateDisplay();
​
        //保存进度
        RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker();
        roleStateCaretaker.setRoleStateMemento(gameRole.saveState());
​
        System.out.println("------------大战Boss后------------");
        //大战Boss时,损耗严重
        gameRole.fight();
        gameRole.stateDisplay();
        System.out.println("------------恢复之前状态------------");
        //恢复之前状态
        gameRole.recoverState(roleStateCaretaker.getRoleStateMemento());
        gameRole.stateDisplay();
​
    }
}

测试结果

分析:白箱备忘录模式是破坏封装性的。但是通过程序员自律,同样可以在一定程度上实现模式的大部分用意。

3.2 “黑箱”备忘录模式

备忘录角色对发起人对象提供一个宽接口,而为其他对象提供一个窄接口。在Java语言中,实现双重接口的办法就是将备忘录类设计成发起人类的内部成员类。

RoleStateMemento 设为 GameRole 的内部类,从而将 RoleStateMemento 对象封装在 GameRole 里面;在外面提供一个标识接口 MementoRoleStateCaretaker 及其他对象使用。这样 GameRole 类看到的是 RoleStateMemento 所有的接口,而RoleStateCaretaker 及其他对象看到的仅仅是标识接口 Memento 所暴露出来的接口,从而维护了封装型。类图如下:

代码如下:

窄接口Memento,这是一个标识接口,因此没有定义出任何的方法

//备忘录接口,对外提供窄接口
public interface Memento {
}

定义发起人类 GameRole,并在内部定义备忘录内部类 RoleStateMemento(该内部类设置为私有的)

/游戏角色类
public class GameRole {
    private int vit; //生命力
    private int atk; //攻击力
    private int def; //防御力
​
    //初始化状态
    public void initState() {
        this.vit = 100;
        this.atk = 100;
        this.def = 100;
    }
​
    //战斗
    public void fight() {
        this.vit = 0;
        this.atk = 0;
        this.def = 0;
    }
​
    //保存角色状态
    public Memento saveState() {
        return new RoleStateMemento(vit, atk, def);
    }
​
    //回复角色状态
    public void recoverState(Memento memento) {
        RoleStateMemento roleStateMemento = (RoleStateMemento) memento;
        this.vit = roleStateMemento.getVit();
        this.atk = roleStateMemento.getAtk();
        this.def = roleStateMemento.getDef();
    }
​
    public void stateDisplay() {
        System.out.println("角色生命力:" + vit);
        System.out.println("角色攻击力:" + atk);
        System.out.println("角色防御力:" + def);
​
    }
​
    public int getVit() {
        return vit;
    }
​
    public void setVit(int vit) {
        this.vit = vit;
    }
​
    public int getAtk() {
        return atk;
    }
​
    public void setAtk(int atk) {
        this.atk = atk;
    }
​
    public int getDef() {
        return def;
    }
​
    public void setDef(int def) {
        this.def = def;
    }
​
    private class RoleStateMemento implements Memento {
        private int vit;
        private int atk;
        private int def;
​
        public RoleStateMemento(int vit, int atk, int def) {
            this.vit = vit;
            this.atk = atk;
            this.def = def;
        }
​
        public int getVit() {
            return vit;
        }
​
        public void setVit(int vit) {
            this.vit = vit;
        }
​
        public int getAtk() {
            return atk;
        }
​
        public void setAtk(int atk) {
            this.atk = atk;
        }
​
        public int getDef() {
            return def;
        }
​
        public void setDef(int def) {
            this.def = def;
        }
    }
}

负责人角色类 RoleStateCaretaker 能够得到的备忘录对象是以 Memento 为接口的,由于这个接口仅仅是一个标识接口,因此负责人角色不可能改变这个备忘录对象的内容

//角色状态管理者类
public class RoleStateCaretaker {
    private Memento memento;
​
    public Memento getMemento() {
        return memento;
    }
​
    public void setMemento(Memento memento) {
        this.memento = memento;
    }
}

客户端测试类

public class Client {
    public static void main(String[] args) {
        System.out.println("------------大战Boss前------------");
        //大战Boss前
        GameRole gameRole = new GameRole();
        gameRole.initState();
        gameRole.stateDisplay();
​
        //保存进度
        RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker();
        roleStateCaretaker.setMemento(gameRole.saveState());
        
        System.out.println("------------大战Boss后------------");
        //大战Boss时,损耗严重
        gameRole.fight();
        gameRole.stateDisplay();
        System.out.println("------------恢复之前状态------------");
        //恢复之前状态
        gameRole.recoverState(roleStateCaretaker.getMemento());
        gameRole.stateDisplay();
    }
}
​

测试结果

4 优缺点

1,优点:

  • 提供了一种可以恢复状态的机制。当用户需要时能够比较方便地将数据恢复到某个历史的状态。

  • 实现了内部状态的封装。除了创建它的发起人之外,其他对象都不能够访问这些状态信息。

  • 简化了发起人类。发起人不需要管理和保存其内部状态的各个备份,所有状态信息都保存在备忘录中,并由管理者进行管理,这符合单一职责原则。

2,缺点:

  • 资源消耗大。如果要保存的内部状态信息过多或者特别频繁,将会占用比较大的内存资源。

5 使用场景

  • 需要保存与恢复数据的场景,如玩游戏时的中间结果的存档功能。

  • 需要提供一个可回滚操作的场景,如 Word、记事本、Photoshop,idea等软件在编辑时按 Ctrl+Z 组合键,还有数据库中事务操作。

 

 

标签:int,void,atk,模式,----,vit,设计模式,public,def
From: https://www.cnblogs.com/xiaoyh/p/16563337.html

相关文章

  • 一天一个小技巧系列
    每天上一当,当当不一样大家好,今天说一下可选链1.obj?.prop.--------如果obj存在则返回obj.prop,否则返回undefined。2.obj?.[prop]-------如果存在则返回obj[pro......
  • 言语
          ......
  • s2oj1651 【2022.10.15】灯 (light)
    s2oj1651【2022.10.15】灯(light)link。首先通过经典的点减边容斥将极长连续段转化为求点亮的灯的个数减去相邻两个灯均点亮的对数。点亮的灯的个数是简单的。接下来......
  • P3747 [六省联考 2017] 相逢是问候
    [六省联考2017]相逢是问候P3747[六省联考2017]相逢是问候题目描述Informatikverbindetdichundmich.信息将你我连结。B君希望以维护一个长度为\(n\)的数......
  • Orleans简单使用
    .NET7更新后Orleans也随着更新了一个大版本,但是感觉微软官方的文档并不是很好,这里写个小demo来演示简单的集群管理(本次使用redis,官方文档甚至都没有提过redis),可以参考Orl......
  • 三月二十日结对
    合作对象:李佳岳     三月十九日和三月二十日,基本完成了北京市的地铁查询功能,可以查看路线,输入起始点和终点可以计算出最优路径,表结构未两个,一个用于存放站点......
  • 如何使用ChatGPT赚钱之四
    目前网络上最热门的话题之一是如何使用ChatGPT赚钱。我们的文章讨论了使用AI工具赚钱的13种简单方法。我们将提供一些实际示例,说明如何从AI获得您想要的东西。由Elo......
  • 9条消除if...else的锦囊妙计,助你写出更优雅的代码
    前言最近在做代码重构,发现了很多代码的烂味道。其他的不多说,今天主要说说那些又臭又长的if...else要如何重构。在介绍更更优雅的编程之前,让我们一起回顾一下,不好的if...e......
  • PTA 红豆生南国
    题目:有诗云:相思(王维唐)红豆生南国,春来发几枝。愿君多采撷,此物最相思。 那么,我们来采红豆吧!假设红豆树是这个样子的:这种红豆树的特点是:每个结点都有一......
  • DFS
    题目:1.BouncyBall解法就是模拟。。。另外就是注意怎么停止dfs!详见代码。//>>>Qiansui#include<map>#include<set>#include<cmath>#include<queue>#include<deque......