大话设计模式——14备忘录模式

一、概念

把要保存的细节封装在了Memento中,如果要改保存的细节也不用影响客户端。该模式比较适用于功能比较复杂但需要维护或记录属性的类,Originator可以根据保存的Memento还原到前一状态。

比如需要实现命令的撤销功能,就可以使用备忘录模式来存储可撤销操作的状态。

二、示例代码

第一版

public class Memento {
    private String state;
    public Memento(String state) {
        this.state = state;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
}
public class Originator {
    private String state;
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public Memento createMemento() {
        return new Memento(state);
    }
    public void recoverMemento(Memento memento) {
        state = memento.getState();
    }
    public void show() {
        System.out.println("当前状态:" + state);
    }
}
public class Caretaker {
    private Memento memento;
    public Memento getMemento() {
        return memento;
    }
    public void setMemento(Memento memento) {
        this.memento = memento;
    }
}
public class RunMain {
    public static void main(String[] args) {
        Originator originator = new Originator();
        originator.setState("100");
        originator.show();

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

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

        originator.recoverMemento(caretaker.getMemento());
        originator.show();
    }
}

第二版

public class GameRoleBackUp {
    private Integer hp;
    private Integer mp;
    public GameRoleBackUp(Integer hp, Integer mp) {
        this.hp = hp;
        this.mp = mp;
    }
    public Integer getHp() {
        return hp;
    }
    public void setHp(Integer hp) {
        this.hp = hp;
    }
    public Integer getMp() {
        return mp;
    }
    public void setMp(Integer mp) {
        this.mp = mp;
    }
}
public class GameProcessBackUp {
    // todo
}
public class GameRole {
    // Health Point,直译就是健康点数的意思,俗称“红”
    private Integer hp;
    // Magic Point,直译魔法值的意思,俗称“蓝”
    private Integer mp;
    public GameRole(Integer hp, Integer mp) {
        this.hp = hp;
        this.mp = mp;
    }
    public Integer getHp() {
        return hp;
    }
    public void setHp(Integer hp) {
        this.hp = hp;
    }
    public Integer getMp() {
        return mp;
    }
    public void setMp(Integer mp) {
        this.mp = mp;
    }
    public GameRoleBackUp save() {
        return new GameRoleBackUp(hp, mp);
    }
    public void load(GameRoleBackUp gameRoleBackUp) {
        this.hp = gameRoleBackUp.getHp();
        this.mp = gameRoleBackUp.getMp();
    }
    @Override
    public String toString() {
        return "GameRole{" + "hp=" + hp + ", mp=" + mp + '}';
    }
}
public class BackUpManagement {
    private GameRoleBackUp gameRoleBackUp;
    private GameProcessBackUp gameProcessBackUp;
    public GameRoleBackUp getGameRoleBackUp() {
        return gameRoleBackUp;
    }
    public void setGameRoleBackUp(GameRoleBackUp gameRoleBackUp) {
        this.gameRoleBackUp = gameRoleBackUp;
    }
    public GameProcessBackUp getGameProcessBackUp() {
        return gameProcessBackUp;
    }
    public void setGameProcessBackUp(GameProcessBackUp gameProcessBackUp) {
        this.gameProcessBackUp = gameProcessBackUp;
    }
}
public class RunMain {
    public static void main(String[] args) {
        GameRole gameRole = new GameRole(100, 100);
        System.out.println(gameRole);
        // save
        BackUpManagement backUpManagement = new BackUpManagement();
        backUpManagement.setGameRoleBackUp(gameRole.save());
        // fight
        gameRole.setHp(0);
        gameRole.setMp(0);
        System.out.println(gameRole);
        // load
        gameRole.load(backUpManagement.getGameRoleBackUp());
        System.out.println(gameRole);
    }
}

三、总结

备忘录模式就是SL大法,SL真香!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值