Skip to content

Instantly share code, notes, and snippets.

@SimonRichardson
Created April 3, 2013 07:51
Show Gist options
  • Save SimonRichardson/5299264 to your computer and use it in GitHub Desktop.
Save SimonRichardson/5299264 to your computer and use it in GitHub Desktop.
Model View Controller using actors
class Model extends Actor {
private state: Int = 0
private listeners: List[Actor] = Nil
def act =loop {
react {
case AddListener(who) => listeners = who :: listeners
case RemoveListener(who) => listeners = listeners.remove(who.eq)
case SetState(st: Int) => state = st; listeners.foreach(_ ! TheState(this, state))
case GetState => reply(TheState(this, state))
}
}
}
case class AddListener(who: Actor)
case class RemoveListener(who: Actor)
case class SetState(st: Int)
case class TheState(model: Actor, state: Int)
case object GetState
class Controller(model: Actor) {
def incModel = (model !? GetState) match {
case TheState(_, st) => model ! SetState(st + 1)
}
}
class View(model: Actor) extends Actor {
model ! AddListener(this)
JFrame frame;
def act {
loop {
react {
case TheState(_, st) => frame.setSize(st, st)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment