我已经实现了一个可以添加到Actor的Listenable/Listener特质。我想知道是否可以将这种风格的特质附加到演员身上,而不必明确地调用监听器处理器方法?创作演员
此外,我期待在Akka库中找到这个功能。我是否错过了某些东西,或者有什么原因让阿卡不会包含这个?
trait Listenable { this: Actor =>
private var listeners: List[Actor] = Nil
protected def listenerHandler: PartialFunction[Any, Unit] = {
case AddListener(who) => listeners = who :: listeners
}
protected def notifyListeners(event: Any) = {
listeners.foreach(_.send(event))
}
}
class SomeActor extends Actor with Listenable
{
def receive = listenerHandler orElse {
case Start => notifyListeners(Started())
case Stop => notifyListeners(Stopped())
}
}
嗯,这是东西,在这两种情况下,我会记得打电话EIT她的super.receive或* listenerHanlder *。我想知道在Scala中是否有一种机制,或者是任何允许演员实现Listenable的Actor库都不需要执行任何操作,只需说出:* with Listenable * – 2010-03-09 22:11:35
但是,您只需要说'extends ListenableActor'而不是'扩展Actor';在'ListenableActor'里面,你已经重写了'receive'(并且可能是'receiveWithin')。又见但以理的回答。 – 2010-03-10 15:02:02