package com.lonton.designpatterns;
import java.util.List;
import java.util.ArrayList;
class Life
{
private String time;
private List<Mememto> mememtos = new ArrayList<Mememto>();
public void setTime(String time)
{
System.out.println("Setting time to " + time);
this.time = time;
}
public void saveToMememto()
{
System.out.println("Saving time: " + time + " to Memento");
mememtos.add(new Mememto(time));
}
public void restoreFromMememto(int index)
{
time = mememtos.get(index).getSavedTime();
System.out.println("Time restored from Memento: " + time);
}
class Mememto
{
private final String time;
public Mememto(String time)
{
this.time = time;
}
public String getSavedTime()
{
return time;
}
}
}
public class MememtoTest
{
public static void main(String[] args)
{
Life life = new Life();
life.setTime("2000 B.C");;
life.saveToMememto();
life.setTime("1000 B.C");;
life.saveToMememto();
life.setTime("2000 A.D");;
life.saveToMememto();
life.setTime("2008 A.D");;
life.saveToMememto();
life.restoreFromMememto(2);
}
}