How to properly use akka actors in scala

后端 未结 2 1581
后悔当初
后悔当初 2021-01-16 19:33

I\'m relatively new to the idea of actors, and was wondering if I could get some critique on what I am doing. For part of a project, I need to have an actor that tells a col

相关标签:
2条回答
  • 2021-01-16 20:06

    You can't remove state since TimeManager should contain list of actors.

    You could hide it:

    class TimeManager extends Actor {
      def receive = getBehavior(Nil)
      def getBehavior(actors: List[ActorRef]): Receive = {
        case AdvanceTime() => actors foreach (_ ! DateTime.now)
        case AddListener(x) => context become getBehavior(x :: actors)
      }
    }
    
    0 讨论(0)
  • 2021-01-16 20:16

    The strength of Scala is its hybrid of OO and Functional programming, scala developer don't need hate var, if you do, you may need choose Haskell.

    In your case, I think the only thing you need to change is to make actors as private

    0 讨论(0)
提交回复
热议问题