备忘录模式 一步步实现

//游戏存进度
/*class GameRole{
	private int vit;
	public int getVit() {
		return vit;
	}
	public void setVit(int vit) {
		this.vit = vit;
	}
	private int atk;
	public int getAtk() {
		return atk;
	}
	public void setAtk(int atk) {
		this.atk = atk;
	}
	private int def;
	public int getDef() {
		return def;
	}
	public void setDef(int def) {
		this.def = def;
	}
	public void StateDisplay() {
		System.out.println("当前角色状态:");
		System.out.println("体力:"+this.getVit());
		System.out.println("攻击力:"+this.getAtk());
		System.out.println("防御力:"+this.getDef());
	}
	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 g = new GameRole();
		  g.GetInitState();
		  g.StateDisplay();
		  GameRole g2 = new GameRole();
		  int a = g.getVit();
		  int b = g.getAtk();
		  int c = g.getDef();
		  g2.setVit(a);
		  g2.setAtk(b);
		  g2.setDef(c);
		  g.Fight();
		  g.StateDisplay();
		  g=g2;
		  g.StateDisplay();
	}
}*/
//备忘录模式
/*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;
	}
}
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();
		  Memento m = new Memento(o.getState());
		  m = o.CreateMemento();
		  c.setMemento(m);
		  o.setState("Off");
		  o.Show();
		  o.SetMemento(c.getMemento());
		  o.Show();
		  
	}
}*/
//游戏进度备忘
class GameRole{
	private int vit;
	public int getVit() {
		return vit;
	}
	public void setVit(int vit) {
		this.vit = vit;
	}
	private int atk;
	public int getAtk() {
		return atk;
	}
	public void setAtk(int atk) {
		this.atk = atk;
	}
	private int def;
	public int getDef() {
		return def;
	}
	public void setDef(int def) {
		this.def = def;
	}
	public void StateDisplay() {
		System.out.println("当前角色状态:");
		System.out.println("体力:"+this.getVit());
		System.out.println("攻击力:"+this.getAtk());
		System.out.println("防御力:"+this.getDef());
	}
	public RoleStateMemento SaveState() {
		return (new RoleStateMemento(vit,atk,def));
	}
	public void RecoveryState(RoleStateMemento memento) {
		this.vit = memento.getVit();
		this.atk = memento.getAtk();
		this.def = memento.getDef();
	}
	public void GetInitState() {
		this.vit = 100;
		this.atk = 100;
		this.def = 100;
	}
	public void Fight() {
		this.vit = 0;
		this.atk = 0;
		this.def = 0;
	}
}
class RoleStateMemento{
	private int vit;
	public int getVit() {
		return vit;
	}
	public void setVit(int vit) {
		this.vit = vit;
	}
	private int atk;
	public int getAtk() {
		return atk;
	}
	public void setAtk(int atk) {
		this.atk = atk;
	}
	private int def;
	public int getDef() {
		return def;
	}
	public void setDef(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{
	private RoleStateMemento memento;
	public RoleStateMemento getMemento() {
		return memento;
	}
	public void setMemento(RoleStateMemento memento) {
		this.memento = memento;
	}
}
public class Main {
	  public static void main(String args[])
	  {
		  GameRole g = new GameRole();
		  g.GetInitState();
		  g.StateDisplay();
		  RoleStateCaretaker r = new RoleStateCaretaker();
		  RoleStateMemento r1 = new RoleStateMemento(g.getVit(),g.getAtk(),g.getDef());
		  r.setMemento(r1);
		  g.Fight();
		  g.StateDisplay();
		  g.RecoveryState(r.getMemento());
		  g.StateDisplay();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值