设计模式-第15章(备忘录模式)

备忘录模式

备忘录(Memento):在不破坏封装性的前提性,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。

在这里插入图片描述
Originator(发起人):创建一个备忘录 Memento,用以记录当前时刻它的内部状态,并可使用备忘录恢复内部状态。Originator 可根据需要决定 Memento存储 Originator 的哪些内部状态。
Memento(备忘录):负责存储 Originator 对象的内部状态,并防止 Originator 以外的其他对象访问备忘录 Memento。
Caretaker(管理者):负责保存好备忘录 Memento。

基本代码

发起人类

class Originator {
    //状态
    private String state;
    public String getState(){
        return this.state;
    }
    public void setState(String value){
        this.state = value;
    }
    //显示数据
    public void show(){
        System.out.println("State:"+this.state);
    }
    //创建备忘录
    public Memento createMemento(){
        return new Memento(this.state);
    }
    //恢复备忘录
    public void recoveryMemento(Memento memento){
        this.setState(memento.getState());
    }
}

备忘录类

class Memento {
    private String state;
    public Memento (String state){
        this.state = state;
    }
    public String getState(){
        return this.state;
    }
    public void setState(String value){
        this.state = value;
    }
}

管理者

class Caretaker{
    private Memento memento;
    public Memento getMemento(){
        return this.memento;
    }
    public void setMemento(Memento value){
        this.memento = value;
    }
}

客户端

public class Test {	
	public static void main(String[] args) {
		System.out.println("**********************************************");		
		System.out.println("《大话设计模式》代码样例");
		System.out.println();		
        // Originator初始状态,状态属性为"On"
        Originator o = new Originator();
        o.setState("On");
        o.show();
        Caretaker c = new Caretaker();
        // 保存状态时,由于有了很好的封装,可以隐藏Originator的实现细节
        c.setMemento(o.createMemento());
        // Originator改变了状态属性为"Off"
        o.setState("Off");
        o.show();
        // 恢复原初始状态
        o.recoveryMemento(c.getMemento());
        o.show();
		System.out.println();
		System.out.println("**********************************************");
	}
}

将要保存的细节给封装到 Memento 中,如果想要修改要保存的细节,只需要更改 Memento 类,不用更改客户端。

Memento 模式比较适用于功能比较复杂的,但需要维护或记录属性历史的类,或者需要保存的属性只是众多属性中的一小部分时,Originator 可以根据保存的Memento信息还原到前一状态。

应用场景
在系统中使用命令模式,需要实现命令的撤销功能,命令模式可以使用备忘录模式来存储可撤销操作的状态。

游戏进度备忘

在这里插入图片描述
GameRole 游戏角色类。
RoleStateMemento 角色备忘录类。
RoleStateCaretaker 角色状态管理者类。

游戏角色类

class GameRole {
    // 生命力
    private int vitality;
    public int getVitality(){
        return this.vitality;
    }
    public void setVitality(int value){
        this.vitality = value;
    }
    // 攻击力
    private int attack;
    public int getAttack(){
        return this.attack;
    }
    public void setAttack(int value){
        this.attack = value;
    }
    // 防御力
    private int defense;
    public int getDefense(){
        return this.defense;
    }
    public void setDefense(int value){
        this.defense = value;
    }
    // 状态显示
    public void displayState(){
        System.out.println("角色当前状态:");
        System.out.println("体力:"+this.vitality);
        System.out.println("攻击力:"+this.attack);
        System.out.println("防御力:"+this.defense);
        System.out.println();
    }
    // 获得初始状态(数据通常来自本机磁盘或远程数据接口)
    public void getInitState(){
        this.vitality = 100;
        this.attack = 100;
        this.defense = 100;
    }
    // 战斗(在与Boss大战后游戏数据损耗为0)
    public void fight(){
        this.vitality = 0;
        this.attack = 0;
        this.defense = 0;
    }
    // 保存角色状态
    public RoleStateMemento saveState(){
    	// 保存状态
        return new RoleStateMemento(this.vitality,this.attack,this.defense);
    }
    // 恢复角色状态
    public void recoveryState(RoleStateMemento memento){
        this.setVitality(memento.getVitality());
        this.setAttack(memento.getAttack());
        this.setDefense(memento.getDefense());
    }
}

角色状态存储箱

class RoleStateMemento {
	// 存储了要保存的状态
    private int vitality;
    private int attack;
    private int defense;
    // 将生命力、攻击力、防御力存入状态存储箱对象中
    public RoleStateMemento (int vitality,int attack,int defense){
        this.vitality = vitality;
        this.attack = attack;
        this.defense = defense;
    }
    // 生命力
    public int getVitality(){
        return this.vitality;
    }
    public void setVitality(int value){
        this.vitality = value;
    }
    // 攻击力
    public int getAttack(){
        return this.attack;
    }
    public void setAttack(int value){
        this.attack = value;
    }
    // 防御力
    public int getDefense(){
        return this.defense;
    }
    public void setDefense(int value){
        this.defense = value;
    }
}

角色状态管理者

class RoleStateCaretaker{
    private RoleStateMemento memento;
    public RoleStateMemento getRoleStateMemento(){
        return this.memento;
    }
    public void setRoleStateMemento(RoleStateMemento value){
        this.memento = value;
    }
}

客户端

public class Test {
	public static void main(String[] args){
		System.out.println("**********************************************");		
		System.out.println("《大话设计模式》代码样例");
		System.out.println();		
        // 大战Boss前
        GameRole role = new GameRole();
        role.getInitState();
        role.displayState();
        // 保存进度
        RoleStateCaretaker stateAdmin = new RoleStateCaretaker();
        stateAdmin.setRoleStateMemento(role.saveState());
        // 大战Boss时,损耗严重
        role.fight();
        // 显示状态
        role.displayState();
        // 游戏进度恢复
        role.recoveryState(stateAdmin.getRoleStateMemento());
        // 显示状态
        role.displayState();
		System.out.println();
		System.out.println("**********************************************");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值