将辅助静态方法提取到objec中

时间:2017-03-22 17:29:59

标签: scala object

我有一些课程:

class MyActor extends Actor{

    override def receive: Receive = {
        case s: String => doSome()
        case i: Int => //something else
    }

}

object MyActor{
    def doSome() = //some action
}

这是Scala的惯用方式吗?我这样做是为了简化Actor的测试。我不想创建ActorSystem并在我的特定情况下编写集成测试。

通常这样做吗?

1 个答案:

答案 0 :(得分:2)

不,惯用的方法是将这些方法提取到特征中,并让演员扩展该特征:

trait DoSomething {
   def doSome() = {
      println("Doing something")
   }
}

class MyActor extends Actor with DoSomething {
    override def receive: Receive = {
        case s: String => doSome()
        case i: Int => //something else
    }
}