设计模式之备忘录模式

Memento design pattern

备忘录模式的概念、备忘录模式的结构、备忘录模式的优缺点、备忘录模式的使用场景、备忘录模式的实现示例、备忘录模式的源码分析


1、备忘录模式的概念

  备忘录模式,又称快照模式,即在不破坏封装的前提下,获取并保存一个对象某一时刻的内部状态,以便以后恢复它。编辑器中的 Ctrl + Z、后退、数据库事务回滚、游戏存档、后悔药等都是备忘录模式的典型应用。

2、备忘录模式的结构

  • 拥有者/创建者:即备忘录的拥有者或创建者,也可理解为备忘录中记录的状态所对应的对象。并提供保存状态、恢复状态的行为。
  • 备忘录:负责存储创建者的内部状态,并在需要的时候将状态提供给创建者。
  • 管理者:即备忘录的管理者,负责维护和存储备忘录,并提供添加备忘录、获取备忘录的行为。

memento-class

3、备忘录模式的优缺点

  • 优点:
    • 提供了一种可以恢复状态的机制,使得用户在有需要的时候可以将数据恢复到某个历史状态。
    • 实现了内部状态的封装。除了创建它的用户外,其它对象都不能够访问这些状态信息。
    • 简化了创建者类,创建者类不需要管理和维护其内部的各个状态,其所有状态信息都有备忘录的管理负责维护,符合单一职责原则。
  • 缺点:
    • 资源消耗过大。若要保存的内部状态信息过多或过频繁,则会导致占用较大的内存资源。

4、备忘录模式的使用场景。

  • 需要保存与恢复数据的场景,如游戏中的存档功能。
  • 需要提供一个可回滚操作的场景,如 Ctrl + Z、数据库事务回滚等。

5、备忘录模式的实现示例

创建者:

public class Hero {

    private Integer health;

    private Integer attack;

    public Hero(Integer health, Integer attack) {
        this.health = health;
        this.attack = attack;
    }

    /**
     * 保存状态
     * @return
     */
    public HeroMemento saveState() {
        return new HeroMemento(this.health, this.attack);
    }

    /**
     * 恢复状态
     * @param heroMemento
     */
    public void recoverState(HeroMemento heroMemento) {
        this.health = heroMemento.getHealth();
        this.attack = heroMemento.getAttack();
    }

    public Integer getHealth() {
        return health;
    }

    public void setHealth(Integer health) {
        this.health = health;
    }

    public Integer getAttack() {
        return attack;
    }

    public void setAttack(Integer attack) {
        this.attack = attack;
    }

    /**
     * 展示当前状态
     */
    public void display() {
        System.out.println("health = " + this.getHealth() + " attack = " + this.getAttack());
    }
}

备忘录:

public class HeroMemento {

    private Integer health;

    private Integer attack;

    public HeroMemento(Integer health, Integer attack) {
        this.health = health;
        this.attack = attack;
    }

    public Integer getHealth() {
        return health;
    }

    public Integer getAttack() {
        return attack;
    }

    /**
     * 获取状态
     */
    public String display() {
        return "health = " + this.getHealth() + " attack = " + this.getAttack();
    }
}

管理者:

public class CareTaker {

    private List<HeroMemento> mementos;

    public CareTaker() {
        this.mementos = new ArrayList<>();
    }

    /**
     * 添加状态
     * @param heroMemento
     */
    public void addMemento(HeroMemento heroMemento) {
        this.mementos.add(heroMemento);
    }

    /**
     * 获取状态
     * @param index
     * @return
     */
    public HeroMemento getMemento(Integer index) {
        return this.mementos.get(index);
    }

    /**
     * 展示备忘录中所有状态
     */
    public void display() {
        HeroMemento heroMemento;
        for (int i = 0; i < this.mementos.size(); i++) {
            heroMemento = this.mementos.get(i);
            System.out.println("时刻 " + i + " 状态 " + heroMemento.display());
        }
    }
}

测试:

public class MementoTest {

    public static void main(String[] args) {
        CareTaker careTaker = new CareTaker();

        Hero hero = new Hero(100, 100);
        careTaker.addMemento(hero.saveState());

        hero.setHealth(80);
        hero.setAttack(120);
        careTaker.addMemento(hero.saveState());

        hero.setHealth(60);
        hero.setAttack(140);
        careTaker.addMemento(hero.saveState());

        System.out.println("备忘录中所有状态");
        careTaker.display();
        System.out.println();

        System.out.println("创建者当前状态");
        hero.display();
        System.out.println();

        System.out.println("恢复到某一时刻的状态");
        hero.recoverState(careTaker.getMemento(1));
        hero.display();
    }
}

测试结果:

备忘录中所有状态
时刻 0 状态 health = 100 attack = 100
时刻 1 状态 health = 80 attack = 120
时刻 2 状态 health = 60 attack = 140

创建者当前状态
health = 60 attack = 140

恢复到某一时刻的状态
health = 80 attack = 120

6、备忘录模式的源码分析

404
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值