Skip to content

Component Factories

laforge49 edited this page Jul 12, 2011 · 6 revisions

The order in which the components of an actor are instantiated becomes important when you have components which depend on other components. This is addressed by the ComponentFactory's addDependency function and the Actor's component function. For example, you might have a component, SaverComponent, which support a get and set on a value and another component, DoubleComponent, which directly accesses the value held by SaverComponent.

Let's start by looking at the Saver component and its factory.

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

class SaverComponentFactory extends ComponentFactory {
  override def instantiate(actor: Actor) = new SaverComponent(actor, this)
}

class SaverComponent(actor: Actor, cf: SaverComponentFactory) extends Component(actor, cf) {
  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 SaverComponentFactory simply creates the component, as this component does not depend on any other component. Now let's look at the Double component and its factory.

case class Times2()

class DoubleComponentFactory extends ComponentFactory {
  addDependency(classOf[SaverComponentFactory])

  override def instantiate(actor: Actor) = new DoubleComponent(actor, this)
}

class DoubleComponent(actor: Actor, cf: DoubleComponentFactory) extends Component(actor, cf) {
  val saver = actor.component(classOf[SaverComponentFactory]).asInstanceOf[SaverComponent]

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

The Double component factory establishes the dependency on the Saver component by calling the addDependency function. This function takes one parameter, the Saver component factory class.

Clone this wiki locally