• JAVA设计模式-备忘录模式


    1、例子

    游戏角色状态恢复问题
    游戏角色有攻击力和防御力,在大战 Boss 前保存自身的状态 ( 攻击力和防御力 ) ,当大
    Boss 后攻击力和防御力下降,从备忘录对象恢复到大战前的状态
    传统分析:

    1) 一个对象,就对应一个保存对象状态的对象, 这样当我们游戏的对象很多时,不
    利于管理,开销也很大 .
    2) 传统的方式是简单地做备份, new 出另外一个对象出来,再把需要备份的数据放到
    这个新对象,但这就暴露了对象内部的细节
    3) 解决方案: => 备忘录模式
    2、基本定义
     
    1) 备忘录模式( Memento Pattern )在不破坏封装性的前提下,捕获一个对象的内
    部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保
    存的状态
    2) 可以这里理解备忘录模式:现实生活中的备忘录是用来记录某些要去做的事情,
    或者是记录已经达成的共同意见的事情,以防忘记了。而在软件层面,备忘录
    模式有着相同的含义,备忘录对象主要用来记录一个对象的某种状态,或者某
    些数据,当要做回退时,可以从备忘录对象里获取原来的数据进行恢复操作
    3) 备忘录模式属于行为型模式

     

    3、代码
    1. public class Caretaker {
    2. private Memento memento;
    3. private ArrayList<Memento> mementoArrayList;
    4. private HashMap<String,ArrayList<Memento>> map;
    5. public Memento getMemento() {
    6. return memento;
    7. }
    8. public void setMemento(Memento memento) {
    9. this.memento = memento;
    10. }
    11. }

     

    1. public class GameRole {
    2. private int vit;
    3. private int def;
    4. public Memento createMemento(){
    5. return new Memento(vit,def);
    6. }
    7. public void recoverGameRoleFromMemento(Memento memento){
    8. this.vit=memento.getVit();
    9. this.def=memento.getDef();
    10. }
    11. public void display(){
    12. System.out.println("攻击力"+this.def+"防御力"+this.vit);
    13. }
    14. public int getVit() {
    15. return vit;
    16. }
    17. public void setVit(int vit) {
    18. this.vit = vit;
    19. }
    20. public int getDef() {
    21. return def;
    22. }
    23. public void setDef(int def) {
    24. this.def = def;
    25. }
    26. }
    1. public class Memento {
    2. private int vit;
    3. private int def;
    4. public Memento(int vit, int def) {
    5. this.vit = vit;
    6. this.def = def;
    7. }
    8. public int getVit() {
    9. return vit;
    10. }
    11. public void setVit(int vit) {
    12. this.vit = vit;
    13. }
    14. public int getDef() {
    15. return def;
    16. }
    17. public void setDef(int def) {
    18. this.def = def;
    19. }
    20. }
    1. public class Client {
    2. public static void main(String[] args) {
    3. GameRole gameRole = new GameRole();
    4. gameRole.setDef(100);
    5. gameRole.setVit(100);
    6. gameRole.display();
    7. Caretaker caretaker=new Caretaker();
    8. caretaker.setMemento(gameRole.createMemento());
    9. gameRole.setVit(30);
    10. gameRole.setDef(30);
    11. gameRole.display();
    12. gameRole.recoverGameRoleFromMemento(caretaker.getMemento());
    13. gameRole.display();
    14. }
    15. }

    总结:
    使用几个类做中间存储数据和获取状态

     

  • 相关阅读:
    通过 Traefik Hub 暴露家里的网络服务
    测试用例设计方法——等价类划分法
    Git简单使用介绍
    01创建型设计模式——单例模式
    npm run 串行执行时,如何给某个命令动态传参数
    weblogic/CVE-2018-2894文件上传漏洞复现
    文生图的最新进展:从一致性模型CMs、LCM、SDXL到Stable Diffusion3、SDXL-Lightning
    Django的模板系统(二)
    面试官:你认为一个优秀的测试工程师需要具备哪些知识和经验?
    uni-app 超详细教程(三)(从菜鸟到大佬)
  • 原文地址:https://blog.csdn.net/CB_Beginner/article/details/127697414