Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b9b0977

Browse files
removed Fluent Interface Pattern and Monostate; refactored Memento and Observer design patterns
1 parent 1778d54 commit b9b0977

File tree

8 files changed

+55
-168
lines changed

8 files changed

+55
-168
lines changed

‎src/main/kotlin/design_patterns/Fluent Interface Pattern.kt

Lines changed: 0 additions & 37 deletions
This file was deleted.

‎src/main/kotlin/design_patterns/Memento.kt

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,50 @@ package design_patterns
22

33
/**
44
*
5-
* pattern: Memento
5+
* Memento is a behavioral design pattern that allows without violating encapsulation to capture
66
*
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
7+
* and save the internal state of an object so that it can be restored to this state later
98
*
109
*/
1110

1211

1312
class Bundle(val str: String)
1413

15-
/**
16-
* Android system emulating
17-
*/
14+
// Android system saves the application state in special bundles
15+
// I gave this as an example, Android system’s saving mechanism is much more complicated
1816
class AndroidSystem {
19-
private var bundle: Bundle = Bundle("")
17+
18+
private var savedBundle: Bundle = Bundle("")
2019

2120
fun saveBundle(bundle: Bundle) {
22-
this.bundle = bundle
21+
savedBundle = bundle
2322
}
2423

25-
fun restoreBundle() = bundle
24+
fun restoreBundle() = savedBundle
2625
}
2726

28-
/**
29-
* TextView is an Android component that draws text on the screen
30-
*/
31-
class TextView1 {
32-
private var text: String = ""
27+
// TextView is an Android component that draws text on the screen
28+
class TextView {
29+
30+
private var currentText: String = ""
3331

3432
fun setText(text: String) {
35-
this.text = text
33+
currentText = text
3634
}
3735

38-
fun text() = text
36+
fun text() = currentText
3937

4038
fun draw() {
41-
println(text)
39+
println(currentText)
4240
}
4341

42+
// saves the current state of TextView before re-creating it
4443
fun onSaveInstanceState(): Bundle {
45-
return Bundle(text)
44+
return Bundle(currentText)
4645
}
4746

47+
// restores the current state after TextView is recreated
4848
fun onRestoreInstanceState(bundle: Bundle) {
49-
text = bundle.str
49+
currentText = bundle.str
5050
}
5151
}

‎src/main/kotlin/design_patterns/Monostate.kt

Lines changed: 0 additions & 34 deletions
This file was deleted.

‎src/main/kotlin/design_patterns/Observer.kt

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,40 @@ package design_patterns
22

33
/**
44
*
5-
* pattern: Observer
5+
* Observer is is a behavioral design pattern that defines a one-to-many relationship between objects
66
*
7-
* using: used when we need to subscribe to changes of some object
8-
*
9-
* description: the object communicates its changes to observers
10-
* who previously subscribed to its changes
7+
* such that when the state of one object changes all dependent objects are automatically notified and updated
118
*
129
*/
1310

14-
fun interface Observer {
11+
fun interface PonyObserver {
1512
fun update(item: List<String>)
1613
}
1714

18-
/**
19-
* the interface of the object whose changes we will listen to
20-
*/
21-
interface Observable {
22-
fun observe(observer: Observer)
23-
fun cancel(observer: Observer)
15+
interface PonyObservable {
16+
fun addObserver(observer: PonyObserver)
17+
fun removeObserver(observer: PonyObserver)
2418
fun notifyObservers()
2519
}
2620

27-
/**
28-
* contains some data, when it changes we will notify observers
29-
*/
30-
class PonyList : Observable {
21+
// PonyList contains some data and when it changes we will notify observers
22+
class PonyList : PonyObservable {
3123

3224
private val ponies = mutableListOf<String>()
3325

34-
private val observers = mutableSetOf<Observer>()
26+
private val observers = mutableSetOf<PonyObserver>()
3527

3628
fun add(pony: String) {
3729
ponies.add(pony)
38-
// notify our observers that the data has changed
30+
// notify observers that the data has changed
3931
notifyObservers()
4032
}
4133

42-
override fun observe(observer: Observer) {
34+
override fun addObserver(observer: PonyObserver) {
4335
observers.add(observer)
4436
}
4537

46-
override fun cancel(observer: Observer) {
38+
override fun removeObserver(observer: PonyObserver) {
4739
observers.remove(observer)
4840
}
4941

‎src/test/kotlin/design_patterns/FluentInterfaceTest.kt

Lines changed: 0 additions & 20 deletions
This file was deleted.

‎src/test/kotlin/design_patterns/MementoTest.kt

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,23 @@ class MementoTest {
1010
// start Android system
1111
val androidOS = AndroidSystem()
1212

13-
val greetingText = TextView1()
14-
greetingText.setText(greeting)
15-
greetingText.draw()
13+
val greetingText = "Hello, World!"
14+
val greetingView = TextView()
15+
greetingView.setText(greetingText)
16+
greetingView.draw()
1617

1718
// rotating Android device (recreating application components)
18-
// saving state
19-
androidOS.saveBundle(greetingText.onSaveInstanceState())
19+
// Android system saves the states of running applications
20+
androidOS.saveBundle(greetingView.onSaveInstanceState())
2021

2122
// the state of the text was lost, but we saved it
22-
greetingText.setText("")
23+
greetingView.setText("")
2324

2425
// Android device has already rotated
25-
// restoring state
26-
greetingText.onRestoreInstanceState(androidOS.restoreBundle())
26+
// The system restores the states of running applications
27+
greetingView.onRestoreInstanceState(androidOS.restoreBundle())
2728

28-
assertEquals(greeting, greetingText.text())
29+
assertEquals(greetingText, greetingView.text())
2930
}
3031

31-
companion object {
32-
private const val greeting = "Hello, World!"
33-
}
3432
}

‎src/test/kotlin/design_patterns/MonostateTest.kt

Lines changed: 0 additions & 24 deletions
This file was deleted.

‎src/test/kotlin/design_patterns/ObserverTest.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,22 @@ internal class ObserverTest {
88
@Test
99
fun test() {
1010
val ponyList = PonyList()
11-
ponyList.observe { items ->
12-
assertEquals("Twillight Sparkle", items.first())
11+
12+
val observer1 = PonyObserver { items ->
13+
assertEquals(listOf("Twillight Sparkle"), items)
1314
}
15+
ponyList.addObserver(observer1)
16+
// we add the first item, observer1 is triggered
1417
ponyList.add("Twillight Sparkle")
18+
// we remove observer1 so that it is not called again
19+
ponyList.removeObserver(observer1)
20+
21+
val observer2 = PonyObserver { items ->
22+
assertEquals(listOf("Twillight Sparkle", "Starlight Glimmer"), items)
23+
}
24+
ponyList.addObserver(observer2)
25+
// we add the second item, observer2 is triggered
26+
ponyList.add("Starlight Glimmer")
1527
}
1628

1729
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /