Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New observer implementation #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions src/main/kotlin/oop/Observer2/Observer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package oop.Observer2

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


data class Player (val name: String,
Copy link
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") : Observer<String> {

override fun onChange(newValue: String) {
currentCategory = newValue
println("New category is $newValue")
}
}
20 changes: 20 additions & 0 deletions src/main/kotlin/oop/Observer2/RouletteObservable.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package oop.Observer2

import kotlin.properties.Delegates

class RouletteObservable {

private val observers = mutableListOf<Observer<String>>()

var newCategory: String by Delegates.observable("") { _, _, new ->
observers.forEach { it.onChange(new) }
}

fun addObserver(observer: Observer<String>) {
observers.add(observer)
}

fun removeObserver(observer: Observer<String>) {
observers.remove(observer)
}
}