设置模式——备忘录模式

备忘录模式

备忘录模式(Memento Design Pattern),也叫快照(Snapshot)模式。指在不违背封装原则前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便之后恢复对象为先前的状态。

备忘录模式主要包含以下几个角色:

Originator(发起人):负责创建一个备忘录Memento,用以记录当前时刻它的内部状态,并可使用备忘录恢复状态。Originator可根据需求决定Memento存储Originator的哪些内部状态。

Memento(备忘录):负责存储Originator对象的内部状态,并可防止Originator以外的其他对象访问备忘录Memento。备忘录有两个接口,Caretaker只能看到备忘录的窄接口,它只能将备忘录传递给其他对象。Originator能够看到一个宽接口,允许它访问返回到先前状态所需的所有数据。

Caretaker(管理者):负责保存好备忘录Memento,不能对备忘录的内容进行操作或检查。

 备忘录模式的实现

在利用备忘录模式时,首先应该设计一个组织者类(Originator),它是一个具体的业务类,存储当前状态。它包含备忘录对象的创建方法createMemeto()和备忘录对象恢复方restoreMemeto()

发起人(Originator)类

public class Originator {

    //需要保存的属性
    private String state;

    //创建备忘录,将当前需要保存的信息导入并实例化出一个Memento对象
    public Memento createMemento() {
        return new Memento(state);
    }

    //恢复备忘录,将Memento导入并将相关数据恢复
    public void setMemento(Memento memento) {
        this.state = memento.getState();
    }

    //显示数据
    public void show() {
        System.out.println("state = " + state);
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
}

管理者(Caretaker)类

public class Caretaker {

    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) {
        Originator originator = new Originator();
        originator.setState("1");
        originator.show();

        Caretaker caretaker = new Caretaker();
        caretaker.setMemento(originator.createMemento());

        originator.setState("0");
        originator.show();

        originator.setMemento(caretaker.getMemento());
        originator.show();

    }

}

 运行结果

state = 1
state = 0
state = 1

模式实现 

在学习的过程发现一个很好的应用实例场景:

文章作者: leisurexi
新博客地址: https://leisurexi.github.io

实例是这样的:

实现场景:

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

代码结构图 :

游戏角色类
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();

    }

}
运行结果 
------------大战Boss前------------
角色生命力:100
角色攻击力:100
角色防御力:100
------------大战Boss后------------
角色生命力:0
角色攻击力:0
角色防御力:0
------------恢复之前状态------------
角色生命力:100
角色攻击力:100
角色防御力:100

 

模式的优缺点

优点

  • 实现了信息的封装,使得用户不需要关心状态的保存细节。
  • 给用户提供了一种可以恢复状态的机制,可以使用户能够比较方便地回到某个历史的状态。

缺点

  • 如果类的成员变量过多,势必会占用比较大的资源,而且每一次保存都会消耗一定的内存

 

 

 

 

相关推荐

  1. 设计模式-备忘录模式

    2024-04-13 02:40:01       55 阅读
  2. 设计模式——备忘录模式

    2024-04-13 02:40:01       55 阅读
  3. 设计模式备忘录模式

    2024-04-13 02:40:01       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-13 02:40:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-13 02:40:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-13 02:40:01       82 阅读
  4. Python语言-面向对象

    2024-04-13 02:40:01       91 阅读

热门阅读

  1. 前端面试题(2)

    2024-04-13 02:40:01       30 阅读
  2. ccf201712-2游戏

    2024-04-13 02:40:01       40 阅读
  3. 替换服务器的SSL证书有什么影响?

    2024-04-13 02:40:01       39 阅读
  4. 数据库迁移平台构思001

    2024-04-13 02:40:01       30 阅读
  5. 自回归模型

    2024-04-13 02:40:01       29 阅读
  6. jQuery笔记 01

    2024-04-13 02:40:01       36 阅读
  7. 循环控制语句的实际应用(3)

    2024-04-13 02:40:01       36 阅读