备忘录模式及其实现
一、游戏存进度
package operation;
class GameRole{
private int vit;
public int Vitality;
public int Getvit() {
return vit;
}
public void Setvit(int vit) {
this.vit=vit;
}
private int atk;
public int GetAttack(){
return atk;
}
public void SetAttack(int atk) {
this.atk=atk;
}
private int def;
public int GetDefense() {
return def;
}
public void SetDefense(int def) {
this.def=def;
}
public void StateDisplay() {
System.out.println("角色当前的状态:");
System.out.println("体力:"+Getvit());
System.out.println("攻击力:"+GetAttack());
System.out.println("防御力:"+GetDefense());
System.out.println("");
}
public void GetInitState()
{
this.vit=100;
this.atk=100;
this.def=100;
}
public void Fight() {
this.vit=0;
this.atk=0;
this.def=0;
}
}
public class Main{
public static void main(String[] args){
GameRole lixiaoyao=new GameRole();
lixiaoyao.GetInitState();
lixiaoyao.StateDisplay();
GameRole Backup=new GameRole();
Backup.SetAttack(lixiaoyao.GetAttack());
Backup.Setvit(lixiaoyao.Getvit());
Backup.SetDefense(lixiaoyao.GetDefense());
lixiaoyao.Fight();
lixiaoyao.StateDisplay();
lixiaoyao.Setvit(Backup.Getvit());
lixiaoyao.SetAttack(Backup.GetAttack());
lixiaoyao.SetDefense(Backup.GetDefense());
lixiaoyao.StateDisplay();
}}
以下
GameRole Backup=new GameRole();
Backup.SetAttack(lixiaoyao.GetAttack());
Backup.Setvit(lixiaoyao.Getvit());
Backup.SetDefense(lixiaoyao.GetDefense());
lixiaoyao.Fight();
lixiaoyao.StateDisplay();
lixiaoyao.Setvit(Backup.Getvit());
lixiaoyao.SetAttack(Backup.GetAttack());
lixiaoyao.SetDefense(Backup.GetDefense());
lixiaoyao.StateDisplay();
暴露了细节,不可取。
二、备忘录模式
Originator(发起人):负责创建一个备忘录Memento,用以记录当前时刻它的内部状态,并可使用备忘录恢复内部状态。Originator可根据需要决定Memento存储Originator的哪些内部状态。
Memento(备忘录):负责存储Originator对象的内部状态,并可防止Originator以外的其他对象访问备忘录Memento。备忘录有两个接口,Caretaker只能看到备忘录的窄接口,它只能将备忘录传递给他对象。Originator能够看到一个宽接口,允许它访问回到先前状态所需的所有数据。
Caretaker(管理者):负责保存好备忘录Memento,不能对备忘录的内容进行操作或检查。
三、备忘录模式基本代码
备忘录:在不破坏封装性的前提下,铺获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可以将该对象恢复到原先保存的状态。
package operation;
class Originator{
private String state;
public String GetState() {
return state;
}
public void SetState(String state) {
this.state = state;
}
public Memento CreateMemento() {
return(new Memento(state));
}
public void SetMemento(Memento memento) {
state=memento.GetState();
}
public void Show() {
System.out.println("State="+state);
}
}
class Memento{
private String state;
public Memento(String state) {
super();
this.state = state;
}
public String GetState() {
return state;
}
public void SetState(String state) {
this.state = state;
}
}
class Caretaker{
private Memento memento;
public Memento GetMemento() {
return memento;
}
public void SetMemento(Memento memento) {
this.memento = memento;
}
}
public class Main{
public static void main(String[] args){
Originator o=new Originator();
o.SetState("On");
o.Show();
Caretaker c=new Caretaker();
c.SetMemento(o.CreateMemento());
o.SetState("Off");
o.Show();
o.SetMemento(c.GetMemento());
o.Show();
}
}
就是把要保存的细节封装在了Memento中了,哪一天要更改保存的细节也不用影响客户端了。
四、游戏进度备忘
package operation;
class GameRole{
private int vit;
public int Vitality;
public int Getvit() {
return vit;
}
public void Setvit(int vit) {
this.vit=vit;
}
private int atk;
public int GetAttack(){
return atk;
}
public void SetAttack(int atk) {
this.atk=atk;
}
private int def;
public int GetDefense() {
return def;
}
public void SetDefense(int def) {
this.def=def;
}
public void StateDisplay() {
System.out.println("角色当前的状态:");
System.out.println("体力:"+Getvit());
System.out.println("攻击力:"+GetAttack());
System.out.println("防御力:"+GetDefense());
System.out.println("");
}
public void GetInitState()
{
this.vit=100;
this.atk=100;
this.def=100;
}
public void Fight() {
this.vit=0;
this.atk=0;
this.def=0;
}
public RoleStateMemento SaveRoleState() {//存档状态
return new RoleStateMemento(this.vit,this.atk,this.def);
}
public void RecoveryState(RoleStateMemento memento) {
this.vit=memento.Getvit();
this.atk=memento.GetAttack();
this.def=memento.GetDefense();
}
}
class RoleStateMemento{
private int vit;
public int Getvit() {
return vit;
}
public void Setvit(int vit) {
this.vit=vit;
}
private int atk;
public int GetAttack(){
return atk;
}
public void SetAttack(int atk) {
this.atk=atk;
}
private int def;
public int GetDefense() {
return def;
}
public void SetDefense(int def) {
this.def=def;
}
public RoleStateMemento(int vit, int atk, int def) {
super();
this.vit=vit;
this.atk=atk;
this.def=def;
}}
class RoleStateCaretaker{
RoleStateMemento rolestatememento;
public RoleStateMemento getRoleStateMemento() {
return rolestatememento;
}
public void setRoleStateMemento(RoleStateMemento rolestatememento) {
this.rolestatememento = rolestatememento;
}
}
public class Main{
public static void main(String[] args){
GameRole lixiaoyao=new GameRole();
lixiaoyao.GetInitState();
lixiaoyao.StateDisplay();
RoleStateCaretaker caretaker=new RoleStateCaretaker();
caretaker.setRoleStateMemento(lixiaoyao.SaveRoleState());
lixiaoyao.Fight();
lixiaoyao.StateDisplay();
lixiaoyao.RecoveryState(caretaker.getRoleStateMemento());
lixiaoyao.StateDisplay();
}
}