备忘录模式(java实现)
来源:互联网 发布:php整站防注入程序 编辑:程序博客网 时间:2024/05/19 02:23
备忘录模式
public class GameRole {
//生命力
private int vitality;
//攻击力
private int attack;
//防御力
private int defense;
public int getVitality() {
return vitality;
}
public void setVitality(int vitality) {
this.vitality =vitality;
}
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;
}
//状态显示
public void stateShow() {
System.out.println("当前角色状态为:");
System.out.println("体力:"+this.vitality);
System.out.println("攻击力:"+this.attack);
System.out.println("防御力:"+this.defense);
}
//获得初始状态
public void getInitState() {
this.vitality=100;
this.attack=100;
this.defense=100;
}
//战斗
public void fight() {
this.vitality=0;
this.attack=0;
this.defense=0;
}
public static void main(String[]args) {
GameRole role=new GameRole();
//大战boss前,获得角色初始状态
role.getInitState();
role.stateShow();
//通过构造一个新的对象来保存游戏进度
GameRole save=new GameRole();
save.vitality=role.vitality;
save.attack=role.attack;
save.defense=role.defense;
//大战boss,数据损耗为0
role.fight();
role.stateShow();
//恢复之前的状态
role.vitality=save.vitality;
role.attack=save.attack;
role.defense=save.defense;
role.stateShow();
}
}
这样写,客户端的职责太大,需要知道游戏的生命力,攻击力和防御力这些细节,还要对它进行备份和恢复,我们希望把这些游戏的存取状态细节封装起来,而且最好是封装在外部的类中,以体现职责分离.
备忘录模式:
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后可以将该对象恢复到原先保存的状态.
public class GameRole {
//生命力
private int vitality;
//攻击力
private int attack;
//防御力
private int defense;
public int getVitality() {
return vitality;
}
public void setVitality(int vitality) {
this.vitality =vitality;
}
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;
}
//状态显示
public void stateShow() {
System.out.println("当前角色状态为:");
System.out.println("体力:"+this.vitality);
System.out.println("攻击力:"+this.attack);
System.out.println("防御力:"+this.defense);
}
//获得初始状态
public void getInitState() {
this.vitality=100;
this.attack=100;
this.defense=100;
}
//战斗
public void fight() {
this.vitality=0;
this.attack=0;
this.defense=0;
}
public RoleStateMemento saveState() {
return new RoleStateMemento(vitality,attack,defense);
}
public void recoverState(RoleStateMemento memento) {
this.vitality=memento.getVitality();
this.attack=memento.getAttack();
this.defense=memento.getDefense();
}
}
//角色状态存储箱
public 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 vitality;
}
public void setVitality(int vitality) {
this.vitality =vitality;
}
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;
}
}
public class Demo {
public static void main(String[]args) {
//大战boss前
GameRole role=new GameRole();
role.getInitState();
role.stateShow();
//保存进度,由于封装在RoleStateMemento类中
//因此我们也不知道保存了哪些具体的角色数据
RoleStateCaretaker stateAdmin=new RoleStateCaretaker();
stateAdmin.memento=role.saveState();
//大战boss
role.fight();
role.stateShow();
//恢复之前的状态
role.recoverState(stateAdmin.memento);
role.stateShow();
}
}
问题描述:备忘录模式也是有缺点的,角色状态需要完整存储到角色对象中,如果状态数据很大很多,那么在资源消耗上,备忘录对象会非常耗内存.
- 备忘录模式(java实现)
- Java 实现备忘录(Memento)模式
- php,java实现备忘录模式
- 《大话设计模式》java实现之备忘录模式
- C++实现备忘录模式
- 备忘录模式lua实现
- 备忘录模式-----C++实现
- 备忘录模式 C++实现
- 备忘录模式-c++实现
- Java备忘录模式
- 备忘录模式(Java)
- java备忘录模式
- java Memento(备忘录模式)
- java 备忘录模式
- java 备忘录模式
- java 备忘录模式
- Java备忘录模式(Memento)
- java备忘录模式
- Struts2实现通过浏览器返回一个helloworld页面给用户
- python argparse用法总结
- IntelliJ IDEA中运行Tomcat报内存溢出(java.lang.OutOfMemoryError: PermGen space)
- iOS_数学函数数学函数
- Android 沉浸式状态栏及悬浮效果
- 备忘录模式(java实现)
- 阻止事件冒泡
- linux三种方式使用库:静态链接库,动态链接库,动态加载库
- RTMP与Flowplayer实现web页面视频流播放功能
- 从0开始学习 GITHUB 系列之「GIT 速成」
- BimServerClient使用
- Autofac官方文档(六)【注册组件之程序集扫描】
- Linux的.a、.so和.o文件
- 组合模式(java实现)