我必须演员类看起来类似于这种形式:
class ActorSupervisorOne(prop: Prop) extends Actor {
val dbSuper = context.actorOf(prop)
val subActor = context.actorOf(Props(new SubActorClass(dbSuper) with **SomeHandlersOne**))
def receive = {
case msg =>
subActor forward msg
}
}
class ActorSupervisorTwo(prop: Prop) extends Actor {
val dbSuper = context.actorOf(prop)
val subActor = context.actorOf(Props(new SubActorClass(dbSuper) with **SomeHandlersTwo**))
def receive = {
case msg =>
subActor forward msg
}
}
他们之间唯一的区别在于混合性状。使用类型参数或抽象类型成员对其进行抽象将不起作用。我已经尝试了以下解决方案,但它看起来很难看并且仍然有代码重复:
abstract class Super extends Actor {
_: {
val handler: Props
} =>
lazy val actor = context.actorOf(handler)
def receive = {
case msg =>
actor forward msg
}
}
class ActorSupervisorOne(val dbSuper: ActorRef) extends Super {
val handler = Props(new SubActorClass(dbSuper) with SomeHandlersOne)
actor
}
class ActorSupervisorTwo(val dbSuper: ActorRef) extends Super {
val handler = Props(new SubActorClass(dbSuper) with SomeHandlersTwo)
actor
}
但在这种情况下,我必须调用actor
以正确初始化它,否则它将不起作用。有没有其他解决方案可以减少这种情况?