Scalable Component Abstractions

  • Read Date - 2020-02-16

  • Fixed SubjectObserver example,

abstract class SubjectObserver {
type S <: Subject
type O <: Observer
โ€‹
abstract class Subject {
self : S =>
โ€‹
private var observers: List[O] = List()
โ€‹
def subscribe(obs: O): Unit = observers = obs :: observers
โ€‹
def publish(): Unit =
for (obs <- observers) obs.notify(self)
}
โ€‹
abstract class Observer {
def notify(sub: S): Unit
}
}
โ€‹
object SensorReader extends SubjectObserver {
type S = Sensor
type O = Display
โ€‹
abstract class Sensor extends Subject {
val label: String
var value: Double = 0.0
โ€‹
def changeValue(v: Double): Unit = {
value = v
publish()
}
}
โ€‹
class Display extends Observer {
override def notify(sub: Sensor): Unit =
println(s"${sub.label} has value ${sub.value}")
}
โ€‹
}