[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:
@@ -0,0 +1,2 @@
|
||||
### Relevant Articles:
|
||||
- [Memento Design Pattern in Java](https://www.baeldung.com/java-memento-design-pattern)
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>design-patterns-behavioral-2</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>design-patterns-behavioral-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>patterns-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
</project>
|
||||
+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