I currently trying to figure out how Memento Pattern works. And I stuck with Caretaker
class? Is it really important to have it? I mean I can use Memento without this class. Please see my code below.
public class Originator {
private String state;
private Integer code;
private Map<String, String> parameters;
// Getters, setters and toString were omitted
public Memento save() {
return new Memento(this);
}
public void restore(Memento memento) {
this.state = memento.getState();
this.code = memento.getCode();
this.parameters = memento.getParameters();
}
}
Here is the Memento implementation.
public class Memento {
private String state;
private Integer code;
private Map<String, String> parameters;
public Memento(Originator originator) {
Cloner cloner = new Cloner();
this.state = cloner.deepClone(originator.getState());
this.code = cloner.deepClone(originator.getCode());
this.parameters = cloner.deepClone(originator.getParameters());
}
// Getters and setters were omitted
}
This code works fine and Memento does its work perfect.
Caretaker
class here? – Orientation