随笔 - 1162  文章 - 0  评论 - 16  阅读 - 59万 

一、需求说明

  1、应用实例要求

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

 

二、思路分析和图解

  

 

三、代码实现

   备忘录对象:

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

 

  游戏角色:

复制代码
 1 public class GameRole {
 2 
 3     private int vit;
 4     private int def;
 5 
 6     public int getVit() {
 7         return vit;
 8     }
 9 
10     public void setVit(int vit) {
11         this.vit = vit;
12     }
13 
14     public int getDef() {
15         return def;
16     }
17 
18     public void setDef(int def) {
19         this.def = def;
20     }
21 
22     /**
23      * 创建Memento,即根据当前的状态得到 Memento
24      * @return
25      */
26     public Memento createMemento() {
27         return new Memento(vit, def);
28     }
29 
30     /**
31      * 从备忘录对象,恢复 gameRole 的状态
32      * @param memento
33      */
34     public void recoverGameRoleFromMemento(Memento memento) {
35         this.vit = memento.getVit();
36         this.def = memento.getDef();
37     }
38 
39 
40     /**
41      *  显示当前游戏角色的状态
42      */
43     public void display() {
44         System.out.println("游戏角色的攻击力:" + this.vit + "防御力:" + this.def);
45     }
46 }
复制代码

 

  Caretaker 守护对象:

复制代码
 1 /**
 2  * 守护对象,用于保存游戏角色状态
 3  */
 4 public class Caretaker {
 5 
 6     //如果只保存一次状态
 7     private Memento memento;
 8     //对 GameRole 保存多次状态
 9     //private ArrayList<Memento> mementos;
10     //对多个游戏角色保存多个状态
11     //private HashMap<String, List<Memento>> rolesMementos;
12 
13 
14     public Memento getMemento() {
15         return memento;
16     }
17 
18     public void setMemento(Memento memento) {
19         this.memento = memento;
20     }
21 }
复制代码

 

  客户端:

复制代码
 1 public class Client {
 2     public static void main(String[] args) {
 3 
 4         GameRole gameRole = new GameRole();
 5         gameRole.setVit(100);
 6         gameRole.setDef(100);
 7 
 8         System.out.println("和Boss大战前的状态");
 9         gameRole.display();
10 
11         //把当前状态保存到 Caretaker
12         Caretaker caretaker = new Caretaker();
13         caretaker.setMemento(gameRole.createMemento());
14 
15         System.out.println("和Boss大战....");
16 
17         gameRole.setDef(50);
18         gameRole.setVit(60);
19 
20         System.out.println("和Boss大战后的状态");
21         gameRole.display();
22 
23         System.out.println("大战后,使用备忘录对象恢复到对战前");
24         gameRole.recoverGameRoleFromMemento(caretaker.getMemento());
25 
26         System.out.println("恢复后的状态");
27         gameRole.display();
28 
29     }
30 }
复制代码

 

 

 

posted on   格物致知_Tony  阅读(112)  评论(0编辑  收藏  举报
编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· 面试官:你是如何进行SQL调优的?
点击右上角即可分享
微信分享提示

目录导航