[JAVA-14174] Renamed paterns to paterns-module (#12718)
* [JAVA-14174] Renamed paterns to paterns-module * [JAVA-14174] naming fixes Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.memento;
|
||||
|
||||
public class TextEditor {
|
||||
|
||||
private TextWindow textWindow;
|
||||
private TextWindowState savedTextWindow;
|
||||
|
||||
public TextEditor(TextWindow textWindow) {
|
||||
this.textWindow = textWindow;
|
||||
}
|
||||
|
||||
public void write(String text) {
|
||||
textWindow.addText(text);
|
||||
}
|
||||
|
||||
public String print() {
|
||||
return textWindow.getCurrentText();
|
||||
}
|
||||
|
||||
public void hitSave() {
|
||||
savedTextWindow = textWindow.save();
|
||||
}
|
||||
|
||||
public void hitUndo() {
|
||||
textWindow.restore(savedTextWindow);
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.memento;
|
||||
|
||||
public class TextWindow {
|
||||
|
||||
private StringBuilder currentText;
|
||||
|
||||
public TextWindow() {
|
||||
this.currentText = new StringBuilder();
|
||||
}
|
||||
|
||||
public String getCurrentText() {
|
||||
return currentText.toString();
|
||||
}
|
||||
|
||||
public void addText(String text) {
|
||||
currentText.append(text);
|
||||
}
|
||||
|
||||
public TextWindowState save() {
|
||||
return new TextWindowState(currentText.toString());
|
||||
}
|
||||
|
||||
public void restore(TextWindowState save) {
|
||||
currentText = new StringBuilder(save.getText());
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.memento;
|
||||
|
||||
public class TextWindowState {
|
||||
|
||||
private String text;
|
||||
|
||||
public TextWindowState(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.memento;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class TextEditorUnitTest {
|
||||
|
||||
@Test
|
||||
void givenTextEditor_whenAddTextSaveAddMoreAndUndo_thenSavecStateRestored() {
|
||||
TextEditor textEditor = new TextEditor(new TextWindow());
|
||||
textEditor.write("The Memento Design Pattern\n");
|
||||
textEditor.write("How to implement it in Java?\n");
|
||||
textEditor.hitSave();
|
||||
textEditor.write("Buy milk and eggs before coming home\n");
|
||||
textEditor.hitUndo();
|
||||
|
||||
assertThat(textEditor.print()).isEqualTo("The Memento Design Pattern\nHow to implement it in Java?\n");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user