Skip to content

Commit 1f7cbc4

Browse files
author
evitwilly
committed
added Memento design pattern
1 parent 378d70d commit 1f7cbc4

File tree

9 files changed

+91
-0
lines changed

9 files changed

+91
-0
lines changed
Binary file not shown.
Binary file not shown.

.gradle/7.1/fileHashes/fileHashes.bin

500 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Content:
3535
* [Observer](/src/main/kotlin/design_patterns/Observer.kt)
3636
* [Dependency Injection](/src/main/kotlin/design_patterns/Dependency%20%20Injection.kt)
3737
* [Adapter](/src/main/kotlin/design_patterns/Adapter.kt)
38+
* [Memento](/src/main/kotlin/design_patterns/Memento.kt)
3839

3940
2. package <code>structures</code> - data structure
4041
* [Binary tree](/src/main/kotlin/structures/BinaryTree.kt)

README_ru.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* [Observer](/src/main/kotlin/design_patterns/Observer.kt)
3434
* [Dependency Injection](/src/main/kotlin/design_patterns/Dependency%20%20Injection.kt)
3535
* [Adapter](/src/main/kotlin/design_patterns/Adapter.kt)
36+
* [Memento](/src/main/kotlin/design_patterns/Memento.kt)
3637

3738
2. пакет <code>ru.structures</code> - структуры данных
3839
* [Бинарное дерево](/src/main/kotlin/structures/BinaryTree.kt)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

0 commit comments

Comments
 (0)