Skip to content
laforge49 edited this page Jul 11, 2011 · 8 revisions

Components are used to extend the capabilities of Actors. For example, if we have an actor which implements getter/setter message processing, it is easy to add a component which doubles the value held by the actor.

The code for getting and setting a value is pretty straight forward.

case class Set(value: Int)
case class Get()

class Saver(mailbox: Mailbox, factory: Factory) extends Actor(mailbox, factory) {
  var i = 0

  bind(classOf[Set], setFunc)
  private def setFunc(msg: AnyRef, rf: Any => Unit) {
    i = msg.asInstanceOf[Set].value
    rf(null)
  }

  bind(classOf[Get], getFunc)
  private def getFunc(msg: AnyRef, rf: Any => Unit) {
    rf(i)
  }
}

The code for the component which will double the value is similar.

case class Times2()

class DoubleComponent(saver: Saver) extends Component(saver, null) {
  bind(classOf[Times2], doubleFunc)
  private def doubleFunc(msg: AnyRef, rf: Any => Unit) {
    saver.i *= 2
    rf(null)
  }
}

Clone this wiki locally