문제

I just start trying myself out with Scala. I grow confident enough to start refactoring an ongoing multi-threaded application that i have been working on for about a year and half.

However something that somehow bother and i can't somehow figure it out, is how to expose the interface/contract/protocole of an Actor? In the OO mode, i have my public interface with synchronized method if necessary, and i know what i can do with that object. Now that i'm going to use actor, it seems that all of that won't be available anymore.

More specifically, I a KbService in my app with a set of method to work with that Kb. I want to make it an actor on its own. For that i need to make all the method private or protected given that now they will be only called by my received method. Hence the to expose the set of available behavior.

Is there some best practice for that ?

도움이 되었습니까?

해결책

To tackle this issue I generally expose the set of messages that an Actor can receive via a "protocol" object.

class TestActor extends Actor {
  def receive = {
    case Action1 => ???
    case Action2 => ???
  }
}

object TestActorProtocol {
  case object Action1
  case object Action2
}

So when you want to communicate with TestActor you must send it a message from its protocol object.

import example.TestActorProtocol._

 testActorRef ! TestActorProtocol.Action1

It can become heavy sometimes, but at least there is some kind of contract exposed.

Hope it helps

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top