• Java设计模式之备忘录模式


    定义

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

    结构

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

    • 发起人角色:记录当前时刻的内部状态信息,提供创建备忘录和恢复备忘录数据的功能,实现其他业务功能,它可以访问备忘录里的所有信息。
    • 备忘录角色:负责存储发起人的内部状态,在需要的时候提供这些内部状态给发起人。
    • 管理者角色:对备忘录进行管理,提供保存与获取备忘录的功能,但其不能对备忘录的内容进行访问与修改。

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

    • 窄接口:管理者对象(和其他发起人对象之外的任何对象)看到的是备忘录的窄接口,这个窄接口只允许他把备忘录对象传给其他的对象。
    • 宽接口:与管理者看到的窄接口相反,发起人对象可以看到一个宽接口,这个宽接口允许它读取所有的数据,以便根据这些数据恢复这个发起人对象的内部状态。

    “白箱”备忘录模式

    备忘录角色对任何对象都提供一个接口,即宽接口,备忘录角色的内部所存储的状态就对所有对象公开。与上述窄接口冲突

    发起人 

    1. //游戏角色
    2. public class GameRole {
    3. private int vit;//生命值
    4. private int atk;//攻击力
    5. private int def;//防御力
    6. public GameRole() {
    7. this.vit = 100;
    8. this.atk = 100;
    9. this.def = 100;
    10. }
    11. public void To0(){
    12. this.vit = 0;
    13. this.atk = 0;
    14. this.def = 0;
    15. }
    16. public int getVit() {
    17. return vit;
    18. }
    19. public void setVit(int vit) {
    20. this.vit = vit;
    21. }
    22. public int getAtk() {
    23. return atk;
    24. }
    25. public void setAtk(int atk) {
    26. this.atk = atk;
    27. }
    28. public int getDef() {
    29. return def;
    30. }
    31. public void setDef(int def) {
    32. this.def = def;
    33. }
    34. public RoleStateMemento saveRoleStateMemento(){
    35. return new RoleStateMemento(vit,atk,def);
    36. }
    37. public void recoverState(RoleStateMemento roleStateMemento){
    38. this.vit= roleStateMemento.getVit();
    39. this.atk= roleStateMemento.getAtk();
    40. this.def= roleStateMemento.getDef();
    41. }
    42. }

     备忘录管理类(提供保存与获取备忘录的功能)

    1. public class RoleStateCaretaker {
    2. private RoleStateMemento roleStateMemento;
    3. public RoleStateMemento getRoleStateMemento(){
    4. return roleStateMemento;
    5. }
    6. public void setRoleStateMemento(RoleStateMemento roleStateMemento){
    7. this.roleStateMemento = roleStateMemento;
    8. }
    9. }

     备忘录类(只做存储状态)

    1. public class RoleStateMemento {
    2. private int vit;//生命值
    3. private int atk;//攻击力
    4. private int def;//防御力
    5. public RoleStateMemento(int vit, int atk, int def) {
    6. this.vit = vit;
    7. this.atk = atk;
    8. this.def = def;
    9. }
    10. public int getVit() {
    11. return vit;
    12. }
    13. public void setVit(int vit) {
    14. this.vit = vit;
    15. }
    16. public int getAtk() {
    17. return atk;
    18. }
    19. public void setAtk(int atk) {
    20. this.atk = atk;
    21. }
    22. public int getDef() {
    23. return def;
    24. }
    25. public void setDef(int def) {
    26. this.def = def;
    27. }
    28. }

     测试

    1. public class Client {
    2. public static void main(String[] args) {
    3. GameRole gameRole = new GameRole();
    4. //存档
    5. RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker();
    6. RoleStateMemento roleStateMemento = gameRole.saveRoleStateMemento();
    7. roleStateCaretaker.setRoleStateMemento(roleStateMemento);
    8. System.out.println("初始状态:");
    9. System.out.println(gameRole.getVit());
    10. System.out.println(gameRole.getAtk());
    11. System.out.println(gameRole.getDef());
    12. System.out.println("置零");
    13. gameRole.To0();
    14. System.out.println(gameRole.getVit());
    15. System.out.println(gameRole.getAtk());
    16. System.out.println(gameRole.getDef());
    17. //读档
    18. gameRole.recoverState(roleStateCaretaker.getRoleStateMemento());
    19. System.out.println("回档后状态");
    20. System.out.println(gameRole.getVit());
    21. System.out.println(gameRole.getAtk());
    22. System.out.println(gameRole.getDef());
    23. }
    24. }

    初始状态:

    100

    100

    100

    置零

    0

    0

    0

    回档后状态

    100

    100

    100

    白箱备忘录模式破坏了封装性,在客户端部分可以通过备忘录类对象对发起人的属性进行更改。

    “黑箱”备忘录模式

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

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

    备忘录窄接口 

    1. 给除了发起人的所有角色所示,只能拿到对应的对象,无法执行任何方法
    2. public interface Memento {
    3. }

     备忘录管理类

    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. //游戏角色
    2. public class GameRole {
    3. private int vit;//生命值
    4. private int atk;//攻击力
    5. private int def;//防御力
    6. public GameRole() {
    7. this.vit = 100;
    8. this.atk = 100;
    9. this.def = 100;
    10. }
    11. public void To0() {
    12. this.vit = 0;
    13. this.atk = 0;
    14. this.def = 0;
    15. }
    16. public int getVit() {
    17. return vit;
    18. }
    19. public void setVit(int vit) {
    20. this.vit = vit;
    21. }
    22. public int getAtk() {
    23. return atk;
    24. }
    25. public void setAtk(int atk) {
    26. this.atk = atk;
    27. }
    28. public int getDef() {
    29. return def;
    30. }
    31. public void setDef(int def) {
    32. this.def = def;
    33. }
    34. //私有内部类,只供本类使用
    35. private class RoleStateMemento implements Memento {
    36. private int vit;//生命值
    37. private int atk;//攻击力
    38. private int def;//防御力
    39. public RoleStateMemento(int vit, int atk, int def) {
    40. this.vit = vit;
    41. this.atk = atk;
    42. this.def = def;
    43. }
    44. public int getVit() {
    45. return vit;
    46. }
    47. public void setVit(int vit) {
    48. this.vit = vit;
    49. }
    50. public int getAtk() {
    51. return atk;
    52. }
    53. public void setAtk(int atk) {
    54. this.atk = atk;
    55. }
    56. public int getDef() {
    57. return def;
    58. }
    59. public void setDef(int def) {
    60. this.def = def;
    61. }
    62. }
    63. public Memento saveMemento() {
    64. return new RoleStateMemento(vit, atk, def);
    65. }
    66. public void recoverState(Memento memento) {
    67. //由于Memento类中什么都不存在,即无法设置发起人状态,因此需要进行一个强转对象
    68. RoleStateMemento roleStateMemento = (RoleStateMemento) memento;
    69. this.vit = roleStateMemento.getVit();
    70. this.atk = roleStateMemento.getAtk();
    71. this.def = roleStateMemento.getDef();
    72. }
    73. }

    测试 

    1. public class Client {
    2. public static void main(String[] args) {
    3. GameRole gameRole = new GameRole();
    4. System.out.println("初始状态:");
    5. System.out.println(gameRole.getVit());
    6. System.out.println(gameRole.getAtk());
    7. System.out.println(gameRole.getDef());
    8. //保存状态
    9. RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker();
    10. Memento memento = gameRole.saveMemento();
    11. roleStateCaretaker.setMemento(memento);
    12. System.out.println("置零");
    13. gameRole.To0();
    14. System.out.println(gameRole.getVit());
    15. System.out.println(gameRole.getAtk());
    16. System.out.println(gameRole.getDef());
    17. //回档
    18. gameRole.recoverState(memento);
    19. System.out.println("回档后状态");
    20. System.out.println(gameRole.getVit());
    21. System.out.println(gameRole.getAtk());
    22. System.out.println(gameRole.getDef());
    23. }
    24. }

    初始状态:

    100

    100

    100

    置零

    0

    0

    0

    回档后状态

    100

    100

    100

    优点

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

    缺点

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

    使用场景

    • 需要保存与恢复数据的场景,如玩游戏时的中间结果的存档功能。
    • 需要提供一个可回滚操作的场景,如 Word、记事本、Photoshop,idea等软件在编辑时按 Ctrl+Z 组合键,还有数据库中事务操作。
  • 相关阅读:
    (高阶) Redis 7 第17讲 分布式锁 实战篇
    电汇ABC(2023-10)
    玉米社:竞价推广账户展现低+点击率低+跳出率高+询盘少怎么办?优化思路
    第二期 微信云开发之位置信息获取(wx.getLocation)
    Docker部署 Nacos
    操作系统备考学习 day2 (1.3.2 - 1.6)
    HTML相关
    美国网站服务器SSL证书介绍
    数据分析报告怎么写
    Java21的新特性
  • 原文地址:https://blog.csdn.net/zmbwcx/article/details/134430223