abstract class Health
{
private int blood;
private int mana;
public void setBlood(int blood)
{
this.blood=blood;
}
public int getBlood()
{
return blood;
}
public void setMana(int mana)
{
this.mana=mana;
}
public int getMana()
{
return mana;
}
public abstract Memento createMemento();
}
class Memento extends Health
{
public Memento createMemento()
{
System.out.println("nothing");
return null;
}
}
class Player extends Health
{
public Memento createMemento()
{
Memento aMemento=new Memento();
aMemento.setBlood(this.getBlood());
aMemento.setMana(this.getMana());
return aMemento;
}
public void retoreHealth(Memento memento)
{
this.setBlood(memento.getBlood());
this.setMana(memento.getMana());
}
}
class Taker
{
Memento memento=new Memento();
public void save(Memento memento)
{
this.memento=memento;
}
public Memento restore()
{
return this.memento;
}
}
public class MementoPatternTest
{
public static void main(String[] args)
{
Player aPlayer=new Player();
aPlayer.setBlood(99);
aPlayer.setMana(49);
System.out.println("before player blood:"+aPlayer.getBlood()+",mana:"+aPlayer.getMana());
Taker aTaker=new Taker();
aTaker.save(aPlayer.createMemento());
aPlayer.setBlood(1);
aPlayer.setMana(2);
System.out.println("after pk player blood:"+aPlayer.getBlood()+",mana:"+aPlayer.getMana());
aPlayer.retoreHealth(aTaker.restore());
System.out.println("after retore player blood:"+aPlayer.getBlood()+",mana:"+aPlayer.getMana());
}
}