Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/kotlin/oop/Observer2/CategoryObserver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package oop.Observer2

class CategoryObserver : Observer<String> {
override val list = mutableListOf<Player>()

override fun onChange(newValue: String){
list.forEachIndexed { _, player ->
player.notify(newValue)
}
}
}
6 changes: 6 additions & 0 deletions src/main/kotlin/oop/Observer2/Observer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package oop.Observer2

interface Observer<T>{
val list: MutableList<Player>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why must an Observer have a list of Players? This is an unnecessary coupling 😅

fun onChange(newValue: T)
}
9 changes: 9 additions & 0 deletions src/main/kotlin/oop/Observer2/Player.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package oop.Observer2


data class Player (val name: String,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Player is the observer. The Roulette is observable.

var currentCategory: String = "history"){
fun notify(newCategory: String){
println("$name: category changed to $newCategory")
}
}
12 changes: 12 additions & 0 deletions src/main/kotlin/oop/Observer2/Roulette.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package oop.Observer2

import kotlin.properties.Delegates

class Roulette{

var observer: CategoryObserver? = null
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to create a separated object in this case. I think it is more natural if Players are observing the Roulette directly 👍


var newCategory: String by Delegates.observable("") { _, _, new ->
observer?.onChange(new)
}
}