Created
April 3, 2013 07:51
-
-
Save SimonRichardson/5299264 to your computer and use it in GitHub Desktop.
Model View Controller using actors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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