备忘录模式-Memento Pattern

模式概述

Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原来保存的状态.

UML类图

这里写图片描述

题目

使用备忘录模式实现游戏中存档的功能。存档的属性包括血量、攻击力、防御力三种。

GitHub源码

https://github.com/YEN-GitHub/PatternDemo/tree/master/src/BehavioralPattern2/MementoPattern

源码

package BehavioralPattern2.MementoPattern.demo1;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by yangenneng on 2017-06-21 10:46
 * Abstract:游戏角色
 */
public class GameRole {

    private int bloodVolume;//血量
    private int attack;//攻击力
    private int defense;//防御力

    /*构造函数*/
    public GameRole(){
        this.bloodVolume=100;
        this.attack=100;
        this.defense=100;
    }

    public GameRole(int bloodVolume,int attack,int defense){
        this.bloodVolume=bloodVolume;
        this.attack=attack;
        this.defense=defense;
    }

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

    /*保存备份游戏角色信息*/
    public RoleStateMemento saveGameRole(){
        System.out.println(df.format(new Date())+"游戏角色备份");
        return new RoleStateMemento(this.bloodVolume,this.attack,this.defense);
    }

    /*恢复备份游戏角色信息*/
    public void resumeGameRole(RoleStateMemento roleStateMemento){
        System.out.println(df.format(new Date())+"游戏角色恢复");

        this.bloodVolume=roleStateMemento.getBloodVolume();
        this.attack=roleStateMemento.getAttack();
        this.defense=roleStateMemento.getDefense();
    }

    /*输出当前角色信息*/
    public void showGameRole(){
        System.out.println("------角色基本信息-------");
        System.out.println("血量:"+this.getBloodVolume());
        System.out.println("攻击力:"+this.getAttack());
        System.out.println("防御力:"+this.getDefense());
    }

    /*get/set构造器*/
    public int getBloodVolume() {
        return bloodVolume;
    }

    public void setBloodVolume(int bloodVolume) {
        this.bloodVolume = bloodVolume;
    }

    public int getAttack() {
        return attack;
    }

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

    public int getDefense() {
        return defense;
    }

    public void setDefense(int defense) {
        this.defense = defense;
    }
}
package BehavioralPattern2.MementoPattern.demo1;

/**
 * Created by yangenneng on 2017-06-21 11:02
 * Abstract:角色备忘录
 */
public class RoleStateMemento {

    private int bloodVolume;//血量
    private int attack;//攻击力
    private int defense;//防御力

    /*构造函数*/
    public RoleStateMemento(int bloodVolume,int attack,int defense){
        this.bloodVolume=bloodVolume;
        this.attack=attack;
        this.defense=defense;
    }

    /*get/set方法*/
    public int getBloodVolume() {
        return bloodVolume;
    }

    public void setBloodVolume(int bloodVolume) {
        this.bloodVolume = bloodVolume;
    }

    public int getAttack() {
        return attack;
    }

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

    public int getDefense() {
        return defense;
    }

    public void setDefense(int defense) {
        this.defense = defense;
    }

}
package BehavioralPattern2.MementoPattern.demo1;

/**
 * Created by yangenneng on 2017-06-21 11:05
 * Abstract:备份管理者
 */
public class RoleStateCarataker {

    private RoleStateMemento roleStateMemento;

    /*get/set方法*/
    public RoleStateMemento getRoleStateMemento() {
        return roleStateMemento;
    }

    public void setRoleStateMemento(RoleStateMemento roleStateMemento) {
        this.roleStateMemento = roleStateMemento;
    }

}
package BehavioralPattern2.MementoPattern.demo1;

/**
 * Created by yangenneng on 2017-06-21 11:06
 * Abstract:
 */
public class Client {
    public static void main(String[] args) {
        GameRole gameRole=new GameRole();
        System.out.println("*********************打Boss前备份************************");
        gameRole.showGameRole();

        RoleStateCarataker roleStateCarataker=new RoleStateCarataker();
        roleStateCarataker.setRoleStateMemento(gameRole.saveGameRole());

        gameRole.setBloodVolume(20);
        gameRole.setAttack(58);
        gameRole.setDefense(84);
        System.out.println("\n*********************打Boss后存档************************");
        gameRole.showGameRole();

        System.out.println("\n*********************读取档案重新开始************************");
        gameRole.resumeGameRole(roleStateCarataker.getRoleStateMemento());
        gameRole.showGameRole();

    }
}

运行结果

这里写图片描述

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/YEN_CSDN/article/details/73530113
所属专栏: 编程体系相关基础
上一篇观察者模式-Observer Pattern
下一篇机器学习-决策树(decision tree)算法
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭