【设计模式】行为型模式6——备忘录模式

备忘录模式

优点

  1. 提供了一种恢复机制
  2. 实现了信息的封装

缺点

  1. 每次创建备忘录对象都会消耗内存
public class Memento {

    private String stoneProtection;
    private int holeNumber;
    private int holeLevel;

    public Memento(String stoneProtection, int holeNumber, int holeLevel) {
        this.stoneProtection = stoneProtection;
        this.holeNumber = holeNumber;
        this.holeLevel = holeLevel;
    }

    public String getStoneProtection() {
        return stoneProtection;
    }

    public void setStoneProtection(String stoneProtection) {
        this.stoneProtection = stoneProtection;
    }

    public int getHoleNumber() {
        return holeNumber;
    }

    public void setHoleNumber(int holeNumber) {
        this.holeNumber = holeNumber;
    }

    public int getHoleLevel() {
        return holeLevel;
    }

    public void setHoleLevel(int holeLevel) {
        this.holeLevel = holeLevel;
    }

    @Override
    public String toString() {
        return "Memento{" +
                "stoneProtection='" + stoneProtection + '\'' +
                ", holeNumber=" + holeNumber +
                ", holeLevel=" + holeLevel +
                '}';
    }
public class CareTaker {

    private List<Memento> mementoList;

    public CareTaker() {

        this.mementoList = new ArrayList<>();
    }

    public void addMemento(Memento memento){
        mementoList.add(memento);
    }

    public Memento getMemento(int index){
        return mementoList.get(index);
    }
}

/**
 * 角色(Originator)
 */
public class MonsterHunter {

    private String stoneProtection;
    private int holeNumber;
    private int holeLevel;

    public String getStoneProtection() {
        return stoneProtection;
    }

    public void setStoneProtection(String stoneProtection) {
        this.stoneProtection = stoneProtection;
    }

    public int getHoleNumber() {
        return holeNumber;
    }

    public void setHoleNumber(int holeNumber) {
        this.holeNumber = holeNumber;
    }

    public int getHoleLevel() {
        return holeLevel;
    }

    public void setHoleLevel(int holeLevel) {
        this.holeLevel = holeLevel;
    }

    public Memento createMemento(){
        return new Memento(this.stoneProtection, this.holeNumber, this.holeLevel);
    }

    public void restoreMemento(Memento memento){
        this.setStoneProtection(memento.getStoneProtection());
        this.setHoleNumber(memento.getHoleNumber());
        this.setHoleLevel(memento.getHoleLevel());
    }

    @Override
    public String toString() {
        return "MonsterHunter{" +
                "stoneProtection='" + stoneProtection + '\'' +
                ", holeNumber=" + holeNumber +
                ", holeLevel=" + holeLevel +
                '}';
    }
}

测试类

public class Test {

    public static void main(String[] args) {


        MonsterHunter monsterHunter = new MonsterHunter();

        monsterHunter.setStoneProtection("天佑护石");
        monsterHunter.setHoleNumber(3);
        monsterHunter.setHoleLevel(4);
        System.out.println("===============初始状态===============");
        System.out.println(monsterHunter.toString());

        CareTaker careTaker = new CareTaker();

        careTaker.addMemento(monsterHunter.createMemento());

        monsterHunter.setStoneProtection("垃圾");
        monsterHunter.setHoleNumber(1);
        monsterHunter.setHoleLevel(1);
        System.out.println("===============错误状态===============");
        System.out.println(monsterHunter.toString());

        monsterHunter.restoreMemento(careTaker.getMemento(0));
        System.out.println("===============回档状态===============");
        System.out.println(monsterHunter.toString());
    }
}

结果

备忘录模式结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值