Java Design Pattern: Memento

In future, time travel will be invented. Memento is the key to time travel. Basically, what it does is to allow an object to go back to a state.

In the following example, You can time travel to any era for your Life, and You can restore to a previous era you have been to.

Memento Design Pattern Class Diagram

memento design pattern

Memento Design Pattern Java Code

package designpatterns.memento;
import java.util.List;
import java.util.ArrayList;
class Life {
    private String time;
 
    public void set(String time) {
        System.out.println("Setting time to " + time);
        this.time = time;
    }
 
    public Memento saveToMemento() {
        System.out.println("Saving time to Memento");
        return new Memento(time);
    }
 
    public void restoreFromMemento(Memento memento) {
    	time = memento.getSavedTime();
        System.out.println("Time restored from Memento: " + time);
    }
 
    public static class Memento {
        private final String time;
 
        public Memento(String timeToSave) {
        	time = timeToSave;
        }
 
        public String getSavedTime() {
            return time;
        }
    }
}
 
public class You {
    public static void main(String[] args) {
        List<Life.Memento> savedTimes = new ArrayList<Life.Memento>();
 
        Life life = new Life();
 
        //time travel and record the eras
        life.set("2000 B.C.");
        savedTimes.add(life.saveToMemento());
        life.set("2000 A.D.");
        savedTimes.add(life.saveToMemento());
        life.set("3000 A.D.");
        savedTimes.add(life.saveToMemento());
        life.set("4000 A.D.");
 
        life.restoreFromMemento(savedTimes.get(0));   
 
    }
}

1 thought on “Java Design Pattern: Memento”

Leave a Comment