Hello everyone!
I have a slightly different implementation than Mosh’s and wondering if anyone can give me feedback on mine? So what I did was I am treating Document to hold current state and only add prevStates to the Caretaker class. With Mosh’s implementation, it feels as though we are saving the current state to the Caretaker (History) class so when we first call restoreState(), it’s actually restoring the current state which confused me…
Document Class
public class Document {
private String content;
private String fontName;
private int fontSize;
public Document() {
/**
* sets the default value when a new document is created to represent the real world
* document where initially the document is empty
**/
this.content = "";
this.fontName = "Arial";
this.fontSize = 12;
}
// restoring the internal state of Document to the previous state
public void undo(DocumentState docState) {
this.content = docState.getContent();
this.fontName = docState.getFontName();
this.fontSize = docState.getFontSize();
}
public String getContent() {
return content;
}
public DocumentState setContent(String content) {
DocumentState state = new DocumentState(this);
this.content = content;
return state;
}
public String getFontName() {
return fontName;
}
public DocumentState setFontName(String fontName) {
DocumentState state = new DocumentState(this);
this.fontName = fontName;
return state;
}
public int getFontSize() {
return fontSize;
}
public DocumentState setFontSize(int fontSize) {
DocumentState state = new DocumentState(this);
this.fontSize= fontSize;
return state;
}
@Override
public String toString() {
return "Document{" +
"content='" + content + '\'' +
", fontName='" + fontName + '\'' +
", fontSize=" + fontSize +
'}';
}
public void printCurrentState() {
System.out.println(this.toString());
}
}
DocumentState (Memento)
public class DocumentState {
private String content;
private String fontName;
private int fontSize;
public DocumentState(String content, String fontName, int fontSize) {
this.content = content;
this.fontName = fontName;
this.fontSize = fontSize;
}
public DocumentState(Document doc) {
this.content = doc.getContent();
this.fontName = doc.getFontName();
this.fontSize = doc.getFontSize();
}
public String getContent() {
return content;
}
public String getFontName() {
return fontName;
}
public int getFontSize() {
return fontSize;
}
}
StateManager (Caretaker)
public class StateManager {
private Stack<DocumentState> states = new Stack<>();
public void push(DocumentState state) {
states.add(state);
}
public DocumentState pop() {
DocumentState prevState = states.peek();
states.pop();
return prevState;
}
}
Driver class:
package com.codewithmosh.memento;
public class Driver {
public static void main(String[] args) {
Document document = new Document();
StateManager history = new StateManager();
history.push(document.setContent("hello how are you?"));
history.push(document.setFontName("calibri"));
document.printCurrentState();
document.undo(history.pop());
document.printCurrentState();
document.undo(history.pop());
document.printCurrentState();
}
}