作曲演员

发布于 2024-08-24 16:21:37 字数 720 浏览 10 评论 0原文

我已经实现了可添加到 Actor 的 Listenable/Listener 特征。我想知道是否可以将这种风格的特征附加到演员身上,而不必显式调用 listenerHandler 方法?

我也期待在 Akka 库中找到这个功能。我是否遗漏了某些内容,或者 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())
    }
}

I've implemented a Listenable/Listener trait that can be added to Actors. I'm wondering if it's possible to attach this style of trait to an actor without it having to explicitly call the listenerHandler method?

Also I was expecting to find this functionality in the Akka library. Am I missing something or is there some reason that Akka would not not include this?

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())
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

纵情客 2024-08-31 16:21:37

为什么不直接扩展 Actor ,或者如果您希望非 Actor 也可监听,请创建一个 ListenableActor 来扩展具有 Listenable 的 Actor ?

然后,您将像上面所做的那样覆盖 Actor 中的 receive (除非您还想调用 super.receive ,不是吗?--您会只是想修改传入的函数)。

Why not extend Actor directly, or if you want non-Actors to be Listenable also, create a ListenableActor that extends Actor with Listenable?

You then would override receive in Actor as you've done above (except you'd want to call super.receive also, wouldn't you?--you'd just want to modify the function that's passed in).

仙女山的月亮 2024-08-31 16:21:37

我建议您扩展 Actor 并使用抽象覆盖

I suggest you extend Actor and use an abstract override.

回眸一遍 2024-08-31 16:21:37

为什么我以前没有见过这个问题,呃,好吧,迟到总比没有好:

http://doc.akka.io/docs/akka/snapshot/scala/routing.html

Why haven't I seen this question before, erm, well, better late than never:

http://doc.akka.io/docs/akka/snapshot/scala/routing.html

把梦留给海 2024-08-31 16:21:37

这是一个解决方案(Beginning Scala 示例的修改版本):

import se.scalablesolutions.akka.actor.Actor

case class AddListener(who: Actor)
case class RemoveListener(who: Actor)

class ListenableActor extends Actor {
    private var listeners: List[Actor] = Nil

    def receive = {
        case AddListener(who) => listeners ::= who
        case RemoveListener(who) => listeners.filter(_ ne who)
    }

    protected def notifyListeners(event: Any) = {
        listeners.foreach(_.send(event))
    }
}

class ImplementingActor extends ListenableActor {
    override def receive = {
        super.receive orElse {
            case Message(content) => println(content)
        }
    }
}

Here is a solution (a modified version of the example from Beginning Scala):

import se.scalablesolutions.akka.actor.Actor

case class AddListener(who: Actor)
case class RemoveListener(who: Actor)

class ListenableActor extends Actor {
    private var listeners: List[Actor] = Nil

    def receive = {
        case AddListener(who) => listeners ::= who
        case RemoveListener(who) => listeners.filter(_ ne who)
    }

    protected def notifyListeners(event: Any) = {
        listeners.foreach(_.send(event))
    }
}

class ImplementingActor extends ListenableActor {
    override def receive = {
        super.receive orElse {
            case Message(content) => println(content)
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文