Java学习-设计模式-备忘录模式

61 篇文章 0 订阅
48 篇文章 1 订阅

Java学习-设计模式-备忘录模式

概述:

备忘录模式保存一个对象的某个状态,以便在适当的时候恢复对象。备忘录模式属于行为型模式。

优点: 1、给用户提供了一种可以恢复状态的机制,可以使用户能够比较方便地回到某个历史的状态。 2、实现了信息的封装,使得用户不需要关心状态的保存细节。

**缺点:**消耗资源。如果类的成员变量过多,势必会占用比较大的资源,而且每一次保存都会消耗一定的内存。

使用场景: 1、需要保存/恢复数据的相关状态场景。 2、提供一个可回滚的操作。

理解:

类似游戏中的回档,将可能回退的属性封装进一个类中,备忘录模式是备份指定类的某一时刻状态,需要回退时,将当前对象还原成备份时的状态。

1. 定义一个备份类实现cloneable接口(在备份时直接克隆对象)
	包含一个类型为本类的属性,一个代表需要备份的属性(只写一个是为了测试方便),
	提供get和set方法,
	重写clone方法实现对象的克隆
2. 创建一个备忘录模式类,包含一个备份类的属性,提供改写后的get和set方法(set需要对对象进行克隆。避免直接引用对象)
3. 创建一个游戏类,完成对备份类的属性的修改,并实现备份和还原功能
2. 测试,先创建一个类对象,设置好要备份的值,备份好当前对象,修改属性,接着再还原回去。

示例:

package Practices.Mode.Memento_Pattern;
// 1. 新建一个属性类,这里假设备份游戏中的金钱
public class Status implements Cloneable {
    private int money;

    public Status(int money) {
        this.money = money;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    @Override
    protected Status clone(){
       Status st = null;
       try {
           st = (Status)super.clone();
       }catch (CloneNotSupportedException e){
            e.printStackTrace();
       }
       st.money = this.money;
       return st;
    }
}
package Practices.Mode.Memento_Pattern;
// 2. 创建一个备忘录类来记录备份类的状态
public class Memento {
    private Status status;

    public Memento(Status status) {
        this.status = status;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        Status clone = status.clone();
        this.status = clone;
    }
}
package Practices.Mode.Memento_Pattern;
// 3. 创建一个游戏来实现属性的变化
public class Game {
    private Status status;
    private Memento memento;

    public Game(Status status) {
        this.status = status;
        this.memento = new Memento(status);
    }

    public void getMoney(int mm){
        status.setMoney(status.getMoney()+mm);
        System.out.println("获取金钱:"+mm+",当前金钱:"+status.getMoney());
    }

    public void loseMoney(int lm){
        status.setMoney(status.getMoney()-lm);
        System.out.println("失去金钱:"+lm+",当前金钱:"+status.getMoney());
    }

    public void createMemento(){
        memento.setStatus(this.status);
        System.out.println("创建备份,当前金钱:"+status.getMoney());
    }

    public void loadMemento(){
        Status status = memento.getStatus().clone();
        this.status = status;
        System.out.println("还原备份,当前金钱为:"+status.getMoney());
    }
}
package Practices.Mode.Memento_Pattern;
// 4. 测试
public class Test_main {
    public static void main(String[] args) {
        Game game = new Game(new Status(100));
        game.getMoney(50);
        game.createMemento();
        game.loseMoney(100);
        game.loadMemento();
    }
}
这个模式理解之后隔了好久才写,导致理解可能出现偏差,有可能代码错误,但是理念应该差不多。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值