备忘录模式

本文介绍了一种软件设计模式——备忘录模式,该模式允许在不破坏封装的前提下捕获一个对象的内部状态,并在需要时恢复到之前的状态。文章通过游戏存档的例子详细解释了备忘录模式的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一句话定义

在对象外保存对象的状态,且不破坏封闭,方便恢复保存状态。

使用场景

1. 需要保存对象状态,方便恢复
2. 通过中间对象间接的提供内部状态的访问

实现要点

1.  提供中间实体类,只负责储存数据

情景假设

游戏状态包括生命、魔法、关卡三种状态,用户退出进行保存,下次游戏读取存档。

实现步骤

1. 创建备忘录,只保存数据。
/**
 * 游戏备忘录
 * 生命、魔法、关卡 三个属性数据
 *
 * @author fengzhen
 * @version 1.0, 2017/2/10
 */
public class GameMemento {
    // 生命值
    private int healthPoint;
    // 魔法值
    private int magicPoint;
    // 关卡
    private int round;

    public GameMemento(int healthPoint, int magicPoint, int round) {
        this.healthPoint = healthPoint;
        this.magicPoint = magicPoint;
        this.round = round;
    }

    public int getHealthPoint() {
        return healthPoint;
    }

    public void setHealthPoint(int healthPoint) {
        this.healthPoint = healthPoint;
    }

    public int getMagicPoint() {
        return magicPoint;
    }

    public void setMagicPoint(int magicPoint) {
        this.magicPoint = magicPoint;
    }

    public int getRound() {
        return round;
    }

    public void setRound(int round) {
        this.round = round;
    }
}
2. 创建发起者,提供恢复存档和创建存档方法。
/**
 * 游戏类
 * 备忘发起者,包含储存、恢复方法
 *
 * @author fengzhen
 * @version 1.0, 2017/2/10
 */
public class GameOriginator {
    // 生命值
    private int healthPoint = 100;
    // 魔法值
    private int magicPoint = 100;
    // 关卡
    private int round = 1;

    /**
     * 进行游戏状态改变
     */
    public void play(){
        healthPoint = 56;
        magicPoint = 88;
        round = 3;
    }

    /**
     * 创建备忘录,开始存储
     */
    public GameMemento createMememto(){
        return new GameMemento(healthPoint, magicPoint, round);
    }

    /**
     * 恢复存档
     * @param memento 保存的存档
     */
    public void restore(GameMemento memento){
        healthPoint = memento.getHealthPoint();
        magicPoint = memento.getMagicPoint();
        round = memento.getRound();
    }

    @Override
    public String toString() {
        return "GameOriginator{" +
                "healthPoint=" + healthPoint +
                ", magicPoint=" + magicPoint +
                ", round=" + round +
                '}';
    }
}
3. 创建备忘录托管,存取备忘录方法。
/**
 * 备忘录托管者
 * 对备忘录进行存储管理
 *
 * @author fengzhen
 * @version 1.0, 2017/2/10
 */
public class Caretaker {
    private List<GameMemento> mementos = new ArrayList<>();

    /**
     * 加入新的备忘录
     *
     * @param memento 新的备忘录
     */
    public void add(GameMemento memento) {
        mementos.add(memento);
    }

    /**
     * 获取备忘录
     *
     * @param index 索引,倒叙
     * @return 索引对应的备忘录
     */
    public GameMemento getMemento(int index) {
        // 前一个状态
        int i = mementos.size() - index;
        if (i >= 1 && i <= mementos.size()) {
            return mementos.get(i-1);
        }
        return null;
    }
}
4. 具体使用
        //备忘录模式
        GameOriginator game = new GameOriginator();
        Log.i("info", "onCreate: ==++" + game.toString());
        game.play();
        Log.i("info", "onCreate: ==++" + game.toString());
        // 存档
        Caretaker caretaker = new Caretaker();
        caretaker.add(game.createMememto());
        // 退出   下一次进入 读档
        GameOriginator newGame = new GameOriginator();
        newGame.restore(caretaker.getMemento(0));
        Log.i("info", "onCreate: ==++" + newGame.toString());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值