博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式——备忘录模式
阅读量:5041 次
发布时间:2019-06-12

本文共 6278 字,大约阅读时间需要 20 分钟。

备忘录模式又称快照模式,是行为模式之一;

备忘录模式的应用场景是对某些对象做出了改变之后,又需要恢复到改变之前的状态!常常和命令模式结合使用...

备忘录中的三张角色;

1、原始角色,需要具有创建备忘录和根据备忘录恢复状态的方法

/* * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved */package com.pt.memo;/** * @description 游戏角色 * @author panteng * @date 17-2-24. */public class Player {    String name;    //角色名称    Integer HP;     //血量    Weapon weapon;  //武器    public Player(){    }    public Player(String name, Integer HP, Weapon weapon){        this.name = name;        this.HP = HP;        this.weapon = weapon;    }    public Memotor createMemotor(){        //此处也并不是必须定义成final,定义成final这样可能会导致无法回收,但可以更有效的防止备忘录被修改        final Memotor memotor = new Memotor(this.HP, this.weapon);        return memotor;    }    /**     * 深复制与浅复制的使用因场景而定     * @return     * @throws Exception     */    public Memotor createMemotorByDeepCopy() throws Exception{        //采用深复制        final Memotor memotor = new Memotor(new Integer(this.HP), (Weapon) this.weapon.deepClone());        return memotor;    }    public void recover(Memotor memotor){        this.HP = memotor.getHP();        this.weapon = memotor.getWeapon();    }    public String getName(){        return name;    }    public void setName(String name){        this.name = name;    }    public Integer getHP(){        return HP;    }    public void setHP(Integer HP){        this.HP = HP;    }    public Weapon getWeapon(){        return weapon;    }    public void setWeapon(Weapon weapon){        this.weapon = weapon;    }    public void showState(){        System.out.println(this.name + " HP:" + this.HP + " weapon:" + weapon);    }}
Player

武器类

/* * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved */package com.pt.memo;import com.pt.prototype.PrototypeModle;/** * @description 武器类 * @author panteng * @date 17-2-24. */public class Weapon extends PrototypeModle {    String name;            //武器名称    Float attackDistance;   //攻击距离    Float ATK;              //攻击力    public Weapon(){    }    public Weapon(String name, Float attackDistance, Float ATK){        this.name = name;        this.attackDistance = attackDistance;        this.ATK = ATK;    }    public String getName(){        return name;    }    public void setName(String name){        this.name = name;    }    public Float getAttackDistance(){        return attackDistance;    }    public void setAttackDistance(Float attackDistance){        this.attackDistance = attackDistance;    }    public Float getATK(){        return ATK;    }    public void setATK(Float ATK){        this.ATK = ATK;    }    /**     * Returns a string representation of the object. In general, the     * {
@code toString} method returns a string that * "textually represents" this object. The result should * be a concise but informative representation that is easy for a * person to read. * It is recommended that all subclasses override this method. *

* The {

@code toString} method for class {
@code Object} * returns a string consisting of the name of the class of which the * object is an instance, the at-sign character `{
@code @}', and * the unsigned hexadecimal representation of the hash code of the * object. In other words, this method returns a string equal to the * value of: *

*
     * getClass().getName() + '@' + Integer.toHexString(hashCode())     * 
* * @return a string representation of the object. */ @Override public String toString(){ return super.toString() + " name:" + this.name + " ATK:" + this.ATK + " attackDistance:" + attackDistance; }}
Weapon

2、备忘录角色,用于保存原始角色需要记录的状态信息

/* * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved */package com.pt.memo;/** * @description 游戏者备忘录 * @author panteng * @date 17-2-24. */public class Memotor {    public Integer HP;    public Weapon weapon;    public Memotor(){    }    public Memotor(Integer HP, Weapon weapon){        this.HP = HP;        this.weapon = weapon;    }    public Integer getHP(){        return HP;    }    public void setHP(Integer HP){        this.HP = HP;    }    public Weapon getWeapon(){        return weapon;    }    public void setWeapon(Weapon weapon){        this.weapon = weapon;    }}
Memotor

3、备忘录管理者角色,主要做好封装,防止备忘录被修改

/* * Copyright (c) 2017. panteng.Co.Ltd All rights reserved */package com.pt.memo;/** * @description 备忘录管理者,之所以有此角色,是保护备忘录不被改变,备忘录模式使用上,除管理者和发起者外,其他不可以操作备忘录; * @author panteng * @date 17-2-24. */public class MemotorManager {    Memotor memotor;    public MemotorManager(Memotor memotor){        this.memotor = memotor;    }    public Memotor getMemotor(){        return memotor;    }    public void setMemotor(Memotor memotor){        this.memotor = memotor;    }}

4、测试类

/* * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved */package com.pt.memo;import org.junit.Test;/** * @description * @author panteng * @date 17-2-24. */public class MemotorTest {    @Test    public void memotoeTest() throws Exception{        //创建一个两个武器        Weapon weapon1 = new Weapon("手枪", 200.1F, 100.5F);        Weapon weapon2 = new Weapon("狙击枪", 1000.25F, 200.5F);        //创建一个游戏角色        Player player = new Player("魂斗罗", 1000, weapon1);        player.showState();        //战斗前先备份        MemotorManager memotorManager = new MemotorManager(player.createMemotor());        MemotorManager memotorManager1 = new MemotorManager(player.createMemotorByDeepCopy());        System.out.println("战斗... ... 切换武器... ...");        player.setWeapon(weapon2);  //切换武器        player.setHP(0);            //挂了        player.showState();        //恢复        System.out.println("恢复战斗前的状态... ...");        player.recover(memotorManager.getMemotor());        player.showState();        //假如战斗过程中角色发生了这样的变化        System.out.println("==========================================================");        player.setHP(1);        player.getWeapon().setATK(0F);        player.getWeapon().setAttackDistance(0F);        player.showState();        player.recover(memotorManager.getMemotor());        player.showState(); //武器无法恢复,因为备份过程中采用的是浅复制,血量恢复了,说明Integer采用的是深复制        //使用第二种方式恢复        player.recover(memotorManager1.getMemotor());        player.showState();    }}

 

 

====================================================

转载于:https://www.cnblogs.com/tengpan-cn/p/6438382.html

你可能感兴趣的文章
SQLServer 镜像功能完全实现
查看>>
Vue-详解设置路由导航的两种方法
查看>>
一个mysql主从复制的配置案例
查看>>
大数据学习系列(8)-- WordCount+Block+Split+Shuffle+Map+Reduce技术详解
查看>>
dvwa网络渗透测试环境的搭建
查看>>
Win8 安装VS2012 和 Sql Server失败问题
查看>>
过点(2,4)作一直线在第一象限与两轴围成三角形,问三角形面积的最小值?...
查看>>
java aes CBC的填充方式发现
查看>>
使用ionic cordova build android --release --prod命令打包报有如下错误及解决方法
查看>>
BZOJ 2338 HNOI2011 数矩形 计算几何
查看>>
关于页面<!DOCTYPE>声明
查看>>
【AS3代码】播放FLV视频流的三步骤!
查看>>
C++标准库vector使用(更新中...)
查看>>
cocos2d-x 2.2.6 之 .xml文件数据读取
查看>>
枚举的使用
查看>>
BZOJ 1531 二进制优化多重背包
查看>>
BZOJ 2324 (有上下界的)费用流
查看>>
python3基础06(随机数的使用)
查看>>
在ASP.NET中操作EXCEL文件
查看>>
BP神经网络的直观推导与Java实现
查看>>