【设计模式】备忘录模式教你怎样优雅地处置惩罚状态快照 ...

打印 上一主题 下一主题

主题 1804|帖子 1804|积分 5414

概述

备忘录模式提供了一种状态规复的实现机制,使得用户可以方便地回到一个特定的历史步调,当新的状态无效大概存在问题时,可以使用暂时存储起来的备忘录将状态复原,很多软件都提供了打消(Undo)操纵,如 Word、记事本、Photoshop、IDEA等软件在编辑时按 Ctrl+Z 组合键时能打消当前操纵,使文档规复到之前的状态;另有在浏览器中的退却键、数据库事务管理中的回滚操纵、玩游戏时的中间结果存档功能、数据库与操纵体系的备份操纵、棋类游戏中的悔棋功能等都属于这类。
定义:
又叫快照模式,在不粉碎封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便以后当需要时能将该对象规复到原先保存的状态。
结构

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

  • 发起人(Originator)角色:记录当前时刻的内部状态信息,提供创建备忘录和规复备忘录数据的功能,实现其他业务功能,它可以访问备忘录里的全部信息。
  • 备忘录(Memento)角色:负责存储发起人的内部状态,在需要的时候提供这些内部状态给发起人。
  • 管理者(Caretaker)角色:对备忘录进行管理,提供保存与获取备忘录的功能,但其不能对备忘录的内容进行访问与修改。
备忘录有两个等效的接口:
窄接口管理者(Caretaker)对象(和其他发起人对象之外的任何对象)看到的是备忘录的窄接口(narror Interface),这个窄接口只答应他把备忘录对象传给其他的对象。但是不答应访问和修改备忘录
宽接口:与管理者看到的窄接口相反,发起人对象可以看到一个宽接口(wide Interface),这个宽接口答应它读取全部的数据,以便根据这些数据规复这个发起人对象的内部状态。
案例实现

【例】游戏挑战BOSS
游戏中的某个场景,一游戏角色有生命力、攻击力、防御力等数据,在打Boss前和后肯定会不一样的,我们答应玩家如果感觉与Boss决斗的结果不理想可以让游戏规复到决斗之前的状态。
要实现上述案例,有两种方式:

  • “白箱”备忘录模式
  • “黑箱”备忘录模式
“白箱”备忘录模式

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

代码如下:
  1. //游戏角色类
  2. public class GameRole {
  3.     private int vit; //生命力
  4.     private int atk; //攻击力
  5.     private int def; //防御力
  6.     //初始化状态
  7.     public void initState() {
  8.         this.vit = 100;
  9.         this.atk = 100;
  10.         this.def = 100;
  11.     }
  12.     //战斗
  13.     public void fight() {
  14.         this.vit = 0;
  15.         this.atk = 0;
  16.         this.def = 0;
  17.     }
  18.     //保存角色状态
  19.     public RoleStateMemento saveState() {
  20.         return new RoleStateMemento(vit, atk, def);
  21.     }
  22.     //回复角色状态
  23.     public void recoverState(RoleStateMemento roleStateMemento) {
  24.         this.vit = roleStateMemento.getVit();
  25.         this.atk = roleStateMemento.getAtk();
  26.         this.def = roleStateMemento.getDef();
  27.     }
  28.     public void stateDisplay() {
  29.         System.out.println("角色生命力:" + vit);
  30.         System.out.println("角色攻击力:" + atk);
  31.         System.out.println("角色防御力:" + def);
  32.     }
  33.     public int getVit() {
  34.         return vit;
  35.     }
  36.     public void setVit(int vit) {
  37.         this.vit = vit;
  38.     }
  39.     public int getAtk() {
  40.         return atk;
  41.     }
  42.     public void setAtk(int atk) {
  43.         this.atk = atk;
  44.     }
  45.     public int getDef() {
  46.         return def;
  47.     }
  48.     public void setDef(int def) {
  49.         this.def = def;
  50.     }
  51. }
  52. //游戏状态存储类(备忘录类)
  53. public class RoleStateMemento {
  54.     private int vit;
  55.     private int atk;
  56.     private int def;
  57.     public RoleStateMemento(int vit, int atk, int def) {
  58.         this.vit = vit;
  59.         this.atk = atk;
  60.         this.def = def;
  61.     }
  62.     public int getVit() {
  63.         return vit;
  64.     }
  65.     public void setVit(int vit) {
  66.         this.vit = vit;
  67.     }
  68.     public int getAtk() {
  69.         return atk;
  70.     }
  71.     public void setAtk(int atk) {
  72.         this.atk = atk;
  73.     }
  74.     public int getDef() {
  75.         return def;
  76.     }
  77.     public void setDef(int def) {
  78.         this.def = def;
  79.     }
  80. }
  81. //角色状态管理者类
  82. public class RoleStateCaretaker {
  83.     private RoleStateMemento roleStateMemento;
  84.     public RoleStateMemento getRoleStateMemento() {
  85.         return roleStateMemento;
  86.     }
  87.     public void setRoleStateMemento(RoleStateMemento roleStateMemento) {
  88.         this.roleStateMemento = roleStateMemento;
  89.     }
  90. }
  91. //测试类
  92. public class Client {
  93.     public static void main(String[] args) {
  94.         System.out.println("------------大战Boss前------------");
  95.         //大战Boss前
  96.         GameRole gameRole = new GameRole();
  97.         gameRole.initState();
  98.         gameRole.stateDisplay();
  99.         //保存进度
  100.         RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker();
  101.         roleStateCaretaker.setRoleStateMemento(gameRole.saveState());
  102.         System.out.println("------------大战Boss后------------");
  103.         //大战Boss时,损耗严重
  104.         gameRole.fight();
  105.         gameRole.stateDisplay();
  106.         System.out.println("------------恢复之前状态------------");
  107.         //恢复之前状态
  108.         gameRole.recoverState(roleStateCaretaker.getRoleStateMemento());
  109.         gameRole.stateDisplay();
  110.     }
  111. }
复制代码
分析:白箱备忘录模式是粉碎封装性的。但是通过程序员自律,同样可以在肯定程度上实现模式的大部分用意。
“黑箱”备忘录模式

备忘录角色对发起人对象提供一个宽接口,而为其他对象提供一个窄接口。在Java语言中,实现双重接口的办法就是将备忘录类设计成发起人类的内部成员类。
将 RoleStateMemento 设为 GameRole 的内部类,从而将 RoleStateMemento 对象封装在 GameRole 里面;在表面提供一个标识接口 Memento 给 RoleStateCaretaker 及其他对象使用。这样 GameRole 类看到的是 RoleStateMemento 全部的接口,而RoleStateCaretaker 及其他对象看到的仅仅是标识接口 Memento 所袒暴露来的接口,从而维护了封装型。类图如下:

代码如下:
窄接口Memento,这是一个标识接口,因此没有定义出任何的方法
  1. public interface Memento {
  2. }
复制代码
定义发起人类 GameRole,并在内部定义备忘录内部类 RoleStateMemento(该内部类设置为私有的)
  1. //游戏角色类
  2. public class GameRole {
  3.     private int vit; //生命力
  4.     private int atk; //攻击力
  5.     private int def; //防御力
  6.     //初始化状态
  7.     public void initState() {
  8.         this.vit = 100;
  9.         this.atk = 100;
  10.         this.def = 100;
  11.     }
  12.     //战斗
  13.     public void fight() {
  14.         this.vit = 0;
  15.         this.atk = 0;
  16.         this.def = 0;
  17.     }
  18.     //保存角色状态
  19.     public Memento saveState() {
  20.         return new RoleStateMemento(vit, atk, def);
  21.     }
  22.     //回复角色状态
  23.     public void recoverState(Memento memento) {
  24.         RoleStateMemento roleStateMemento = (RoleStateMemento) memento;
  25.         this.vit = roleStateMemento.getVit();
  26.         this.atk = roleStateMemento.getAtk();
  27.         this.def = roleStateMemento.getDef();
  28.     }
  29.     public void stateDisplay() {
  30.         System.out.println("角色生命力:" + vit);
  31.         System.out.println("角色攻击力:" + atk);
  32.         System.out.println("角色防御力:" + def);
  33.     }
  34.     public int getVit() {
  35.         return vit;
  36.     }
  37.     public void setVit(int vit) {
  38.         this.vit = vit;
  39.     }
  40.     public int getAtk() {
  41.         return atk;
  42.     }
  43.     public void setAtk(int atk) {
  44.         this.atk = atk;
  45.     }
  46.     public int getDef() {
  47.         return def;
  48.     }
  49.     public void setDef(int def) {
  50.         this.def = def;
  51.     }
  52.     private class RoleStateMemento implements Memento {
  53.         private int vit;
  54.         private int atk;
  55.         private int def;
  56.         public RoleStateMemento(int vit, int atk, int def) {
  57.             this.vit = vit;
  58.             this.atk = atk;
  59.             this.def = def;
  60.         }
  61.         public int getVit() {
  62.             return vit;
  63.         }
  64.         public void setVit(int vit) {
  65.             this.vit = vit;
  66.         }
  67.         public int getAtk() {
  68.             return atk;
  69.         }
  70.         public void setAtk(int atk) {
  71.             this.atk = atk;
  72.         }
  73.         public int getDef() {
  74.             return def;
  75.         }
  76.         public void setDef(int def) {
  77.             this.def = def;
  78.         }
  79.     }
  80. }
复制代码
负责人角色类 RoleStateCaretaker 能够得到的备忘录对象是以 Memento 为接口的,由于这个接口仅仅是一个标识接口,因此负责人角色不可能改变这个备忘录对象的内容
  1. //角色状态管理者类
  2. public class RoleStateCaretaker {
  3.     private Memento memento;
  4.     public Memento getMemento() {
  5.         return memento;
  6.     }
  7.     public void setMemento(Memento memento) {
  8.         this.memento = memento;
  9.     }
  10. }
复制代码
客户端测试类
  1. public class Client {
  2.     public static void main(String[] args) {
  3.         System.out.println("------------大战Boss前------------");
  4.         //大战Boss前
  5.         GameRole gameRole = new GameRole();
  6.         gameRole.initState();
  7.         gameRole.stateDisplay();
  8.         //保存进度
  9.         RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker();
  10.         roleStateCaretaker.setMemento(gameRole.saveState());
  11.         
  12.         System.out.println("------------大战Boss后------------");
  13.         //大战Boss时,损耗严重
  14.         gameRole.fight();
  15.         gameRole.stateDisplay();
  16.         System.out.println("------------恢复之前状态------------");
  17.         //恢复之前状态
  18.         gameRole.recoverState(roleStateCaretaker.getMemento());
  19.         gameRole.stateDisplay();
  20.     }
  21. }
复制代码
优缺点

长处:

  • 提供了一种可以规复状态的机制。当用户需要时能够比力方便地将数据规复到某个历史的状态。
  • 实现了内部状态的封装。除了创建它的发起人之外,其他对象都不能够访问这些状态信息。
  • 简化了发起人类。发起人不需要管理和保存其内部状态的各个备份,全部状态信息都保存在备忘录中,并由管理者进行管理,这符合单一职责原则。
缺点:

  • 资源消耗大。如果要保存的内部状态信息过多大概特殊频仍,将会占用比力大的内存资源。
使用场景


  • 需要保存与规复数据的场景,如玩游戏时的中间结果的存档功能。
  • 需要提供一个可回滚操纵的场景,如 Word、记事本、Photoshop,idea等软件在编辑时按 Ctrl+Z 组合键,另有数据库中事务操纵。
往期推荐


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

拉不拉稀肚拉稀

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表