File tree Expand file tree Collapse file tree 5 files changed +70
-0
lines changed
main/java/pl/mperor/lab/java/design/pattern/behavioral/memento
test/java/pl/mperor/lab/java/design/pattern/behavioral/memento Expand file tree Collapse file tree 5 files changed +70
-0
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ with practical examples and best practices for using design patterns to create r
12
12
- [ Chain of Responsibility] ( src/main/java/pl/mperor/lab/java/design/pattern/behavioral/chain/of/responsibility ) 🔗
13
13
- [ Command] ( src/main/java/pl/mperor/lab/java/design/pattern/behavioral/command ) 📝
14
14
- [ Execute Around Method (EAM)] ( src/main/java/pl/mperor/lab/java/design/pattern/behavioral/eam ) ⭕
15
+ - [ Memento] ( src/main/java/pl/mperor/lab/java/design/pattern/behavioral/memento ) 💾
15
16
- [ Observer] ( src/main/java/pl/mperor/lab/java/design/pattern/behavioral/observer ) 👀
16
17
- [ State] ( src/main/java/pl/mperor/lab/java/design/pattern/behavioral/state ) 📜
17
18
- [ Strategy] ( src/main/java/pl/mperor/lab/java/design/pattern/behavioral/strategy ) 🎯
Original file line number Diff line number Diff line change
1
+ package pl .mperor .lab .java .design .pattern .behavioral .memento ;
2
+
3
+ import java .util .ArrayDeque ;
4
+ import java .util .Deque ;
5
+
6
+ class History {
7
+
8
+ private final Deque <TextMemento > history = new ArrayDeque <>();
9
+
10
+ void save (TextEditor editor ) {
11
+ history .push (editor .save ());
12
+ }
13
+
14
+ void undo (TextEditor editor ) {
15
+ if (!history .isEmpty ()) {
16
+ editor .restore (history .pop ());
17
+ }
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ package pl .mperor .lab .java .design .pattern .behavioral .memento ;
2
+
3
+ class TextEditor {
4
+
5
+ private StringBuilder content = new StringBuilder ();
6
+
7
+ TextEditor write (String text ) {
8
+ content .append (text );
9
+ return this ;
10
+ }
11
+
12
+ TextMemento save () {
13
+ return new TextMemento (content .toString ());
14
+ }
15
+
16
+ void restore (TextMemento memento ) {
17
+ content = new StringBuilder (memento .content ());
18
+ }
19
+
20
+ String getContent () {
21
+ return content .toString ();
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ package pl .mperor .lab .java .design .pattern .behavioral .memento ;
2
+
3
+ record TextMemento (String content ) {
4
+ }
Original file line number Diff line number Diff line change
1
+ package pl .mperor .lab .java .design .pattern .behavioral .memento ;
2
+
3
+ import org .junit .jupiter .api .Assertions ;
4
+ import org .junit .jupiter .api .Test ;
5
+
6
+ public class TextEditorMementoTest {
7
+
8
+ @ Test
9
+ public void testSavingAndRestoringWithTextMemento () {
10
+ var editor = new TextEditor ();
11
+ var history = new History ();
12
+
13
+ editor .write ("⭐" );
14
+ Assertions .assertEquals ("⭐" , editor .getContent ());
15
+ history .save (editor );
16
+
17
+ editor .write ("1️⃣" ).write ("2️⃣" ).write ("3️⃣" );
18
+ Assertions .assertEquals ("⭐1️⃣2️⃣3️⃣" , editor .getContent ());
19
+
20
+ history .undo (editor );
21
+ Assertions .assertEquals ("⭐" , editor .getContent ());
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments