Skip to content

Instantly share code, notes, and snippets.

@einblicker
Created December 16, 2011 08:55
Show Gist options
  • Select an option

  • Save einblicker/1485205 to your computer and use it in GitHub Desktop.

Select an option

Save einblicker/1485205 to your computer and use it in GitHub Desktop.
An observer pattern library by family polymorphism
//library code
//references: http://lamp.epfl.ch/~odersky/papers/ScalableComponent.pdf
abstract class SubjectObserver {
type S <: Subject
type O <: Observer
abstract class Subject { this: S =>
private var observers: List[O] = List()
def subscribe(obs: O) =
observers = obs :: observers
def publish =
for (obs <- observers) obs.notify(this)
}
abstract class Observer {
def notify(sub: S): Unit
}
}
//user code
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) = {
value = v
publish
}
}
class Display extends Observer {
def println(s: String) = Predef.println(s)
def notify(sub: Sensor) =
println(sub.label + " has value " + sub.value)
}
}
object Test {
import SensorReader._
val s1 = new Sensor { val label = "sensor1" }
val s2 = new Sensor { val label = "sensor2" }
def main(args: Array[String]) = {
val d1 = new Display
val d2 = new Display
s1.subscribe(d1)
s1.subscribe(d2)
s2.subscribe(d1)
s1.changeValue(2)
s2.changeValue(3)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment