我的设计有问题。我只想这样做,但我无法做到。 Compilator抱怨MyProcessor中未实现的流程方法,因此MyProcessor应该是抽象的......
trait Event
abstract class EventProcessor extends Actor{
def receive ={
case evt : Event => process(evt)
case evts : Iterable[Event] => process(evts)
}
def process(event :Event): Iterable[SomeObject]
def process(events :Iterable[Event])={
events.flatMap(process)
}
}
case class MyEvent extends Event
class MyProcessor extends Processor{
def process(event :MyEvent)={
some processing...
}
}
我很确定这是一个众所周知的模式。你的方法是什么?
我需要两件事:
编辑:解决方案
trait Event
abstract class EventProcessor[T<:Event:ClassTag] extends Actor{
def receive ={
case evt : T=> process(evt)
case evts : Iterable[T] => process(evts)
}
def process(event :T): Iterable[SomeObject]
def process(events :Iterable[T])={
events.flatMap(process)
}
}
case class MyEvent extends Event
class MyProcessor extends Processor[MyEvent]{
def process(event :MyEvent)={
some processing...
}
}
答案 0 :(得分:1)
您需要一个类型参数:
abstract class EventProcessor[T <: Event] {
def process(event: T)
}
class MyProcessor extends Processor[MyEvent] {
def process(event: MyEvent)
}