备忘录设计模式(行为型设计模式)

2023-03-27 14:56:36

一、备忘录设计模式jianjie

备忘录设计模式(Memento Pattern)

  • 在不破坏封闭的前提下,捕获⼀个对象的内部状态,保存对象的某个状态,以便在适当的时候恢复对象,⼜叫做快照模式,属于⾏为模式
  • 备忘录模式实现的⽅式需要保证被保存的对象状态不能被对象从外部访问

应用场景

  • 玩游戏的时候肯定有存档功能,下⼀次登录游戏时可以从上次退出的地⽅继续游戏
  • 棋盘类游戏的悔棋、数据库事务回滚
  • 需要记录⼀个对象的内部状态时,为了允许⽤户取消不确定或者错误的操作,能够恢复到原先的状态
  • 提供⼀个可回滚的操作,如ctrl+z、浏览器回退按钮 

角色

  • Originator: 发起者,记录当前的内部状态,并负责创 建和恢复备忘录数据,允许访问返回到先前状态所需的所有数据,可以根据需要决定Memento存储⾃⼰的哪些内部状态
  • Memento: 备忘录,负责存储Originator发起⼈对象的内部状态,在需要的时候提供发起⼈需要的内部状态
  • Caretaker: 管理者,对备忘录进⾏管理、保存和提供备忘录,只能将备忘录传递给其他⻆⾊
  • Originator 和 Memento属性类似 

二、优缺点 

优点:

  • 给⽤户提供了⼀种可以恢复状态的机制实现了信息的封装,使得⽤户不需要关⼼状态的保存细节

缺点:

  • 消耗更多的资源,⽽且每⼀次保存都会消耗⼀定的内存

三、代码实现

/**
 * 发起者
 */
public class RoleOriginator {
    //生命力
    private int live = 100;
    //攻击力
    private int attack = 50;

    public int getAttack() {
        return attack;
    }

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

    public int getLive() {
        return live;
    }

    public void setLive(int live) {
        this.live = live;
    }
    public void display(){
        System.out.println("开始==========");
        System.out.println("生命力:"+live);
        System.out.println("攻击力:"+attack);
        System.out.println("结束==========");
    }
    public void fight(){
        this.attack = attack + 2;
        this.live = live -10;
    }
    /**
     * 保存快照,存储状态
     * @return
     */
    public RoleStateMemento saveState(){
        return new RoleStateMemento(live,attack);
    }
    /**
     * 恢复
     */
    public void recoveryState(RoleStateMemento memento){
        this.attack = memento.getAttack();
        this.live = memento.getLive();
    }
}
/**
 * 备忘录,发起者的属性拷贝一份
 */
public class RoleStateMemento {
    //生命力
    private int live;
    //攻击力
    private int attack;

    public RoleStateMemento(int live, int attack) {
        this.live = live;
        this.attack = attack;
    }

    public int getAttack() {
        return attack;
    }

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

    public int getLive() {
        return live;
    }

    public void setLive(int live) {
        this.live = live;
    }
}
/**
 * 管理者
 */
public class RoleStateCaretaker {
    /**
     * 备忘录对象
     */
    private RoleStateMemento memento;

    public RoleStateMemento getMemento() {
        return memento;
    }

    public void setMemento(RoleStateMemento memento) {
        this.memento = memento;
    }
}
/**
 * 备忘录设计模式
 */
public class Main {
    public static void main(String[] args) {
        RoleOriginator role = new RoleOriginator();
        role.display();
        role.fight();
        role.display();
        System.out.println("保存快照");
        RoleStateCaretaker caretaker = new RoleStateCaretaker();
        caretaker.setMemento(role.saveState());

        role.fight();
        role.fight();
        role.fight();
        role.fight();
        role.display();

        System.out.println("恢复备份");
        role.recoveryState(caretaker.getMemento());
        role.display();
    }
}

 

注释:个人学习观点以作笔记,如有瑕疵望谅解

  • 作者:Stitch12
  • 原文链接:https://blog.csdn.net/Stitch12/article/details/121700972
    更新时间:2023-03-27 14:56:36