diff --git a/patterns/design-patterns-2/pom.xml b/patterns/design-patterns-2/pom.xml index 17afff87ae..f99bb4dc9c 100644 --- a/patterns/design-patterns-2/pom.xml +++ b/patterns/design-patterns-2/pom.xml @@ -30,6 +30,12 @@ org.apache.commons commons-lang3 ${commons-lang3.version} + + + org.assertj + assertj-core + ${assertj.version} + test org.inferred @@ -49,6 +55,7 @@ 1.8 1.8 16.0.2 + 3.12.2 2.4.1 3.0.2 diff --git a/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextEditor.java b/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextEditor.java new file mode 100644 index 0000000000..37ac962773 --- /dev/null +++ b/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextEditor.java @@ -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); + } +} diff --git a/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextWindow.java b/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextWindow.java new file mode 100644 index 0000000000..08778561b0 --- /dev/null +++ b/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextWindow.java @@ -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()); + } +} diff --git a/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextWindowState.java b/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextWindowState.java new file mode 100644 index 0000000000..10015a84d1 --- /dev/null +++ b/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextWindowState.java @@ -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; + } +} diff --git a/patterns/design-patterns-2/src/test/java/com/baeldung/memento/TextEditorUnitTest.java b/patterns/design-patterns-2/src/test/java/com/baeldung/memento/TextEditorUnitTest.java new file mode 100644 index 0000000000..09f6df70a2 --- /dev/null +++ b/patterns/design-patterns-2/src/test/java/com/baeldung/memento/TextEditorUnitTest.java @@ -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"); + } +} \ No newline at end of file