File tree Expand file tree Collapse file tree 9 files changed +91
-0
lines changed
main/kotlin/design_patterns
test/kotlin/design_patterns Expand file tree Collapse file tree 9 files changed +91
-0
lines changed Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ Content:
35
35
* [ Observer] ( /src/main/kotlin/design_patterns/Observer.kt )
36
36
* [ Dependency Injection] ( /src/main/kotlin/design_patterns/Dependency%20%20Injection.kt )
37
37
* [ Adapter] ( /src/main/kotlin/design_patterns/Adapter.kt )
38
+ * [ Memento] ( /src/main/kotlin/design_patterns/Memento.kt )
38
39
39
40
2 . package <code >structures</code > - data structure
40
41
* [ Binary tree] ( /src/main/kotlin/structures/BinaryTree.kt )
Original file line number Diff line number Diff line change 33
33
* [ Observer] ( /src/main/kotlin/design_patterns/Observer.kt )
34
34
* [ Dependency Injection] ( /src/main/kotlin/design_patterns/Dependency%20%20Injection.kt )
35
35
* [ Adapter] ( /src/main/kotlin/design_patterns/Adapter.kt )
36
+ * [ Memento] ( /src/main/kotlin/design_patterns/Memento.kt )
36
37
37
38
2 . пакет <code >ru.structures</code > - структуры данных
38
39
* [ Бинарное дерево] ( /src/main/kotlin/structures/BinaryTree.kt )
Original file line number Diff line number Diff line change
1
+ package design_patterns
2
+
3
+ /* *
4
+ *
5
+ * pattern: Memento
6
+ *
7
+ * allows without violating encapsulation, to fix and save the state of an object
8
+ * in such a way as to restore it to this state
9
+ *
10
+ */
11
+
12
+
13
+ class Bundle (val str : String )
14
+
15
+ /* *
16
+ *
17
+ * Android system emulating
18
+ *
19
+ */
20
+ class AndroidSystem {
21
+ private var bundle: Bundle = Bundle (" " )
22
+
23
+ fun saveBundle (bundle : Bundle ) {
24
+ this .bundle = bundle
25
+ }
26
+
27
+ fun restoreBundle () = bundle
28
+ }
29
+
30
+ /* *
31
+ *
32
+ * TextView is an Android component that draws text on the screen
33
+ *
34
+ */
35
+ class TextView1 {
36
+ private var text: String = " "
37
+
38
+ fun setText (text : String ) {
39
+ this .text = text
40
+ }
41
+
42
+ fun text () = text
43
+
44
+ fun draw () {
45
+ println (text)
46
+ }
47
+
48
+ fun onSaveInstanceState (): Bundle {
49
+ return Bundle (text)
50
+ }
51
+
52
+ fun onRestoreInstanceState (bundle : Bundle ) {
53
+ text = bundle.str
54
+ }
55
+ }
Original file line number Diff line number Diff line change
1
+ package design_patterns
2
+
3
+ import org.junit.Test
4
+ import org.junit.jupiter.api.Assertions
5
+
6
+ class Memento {
7
+
8
+ @Test
9
+ fun test () {
10
+ // start Android system
11
+ val android = AndroidSystem ()
12
+
13
+ val greetingText = TextView1 ()
14
+ greetingText.setText(greeting)
15
+ greetingText.draw()
16
+
17
+ // rotating Android device (recreating Application components)
18
+ // saving state
19
+ android.saveBundle(greetingText.onSaveInstanceState())
20
+
21
+ // the state of the text was lost, but we saved it
22
+ greetingText.setText(" " )
23
+
24
+ // Android device has already rotated
25
+ // restoring state
26
+ greetingText.onRestoreInstanceState(android.restoreBundle())
27
+
28
+ Assertions .assertEquals(greeting, greetingText.text())
29
+ }
30
+
31
+ companion object {
32
+ private const val greeting = " Hello, World!"
33
+ }
34
+ }
You can’t perform that action at this time.
0 commit comments