2
我有一個演員,我需要做的基於消息hierarhy事案件之間共享邏輯。你可以看到,updateOneAndTwoLogic()
調用兩次:從Java API如何在模式匹配
class MyActor extends Actor {
def receive: PartialFunction[Any, Unit] = {
case UpdateOne() => {
updateOneAndTwoLogic() //repeated
updateOne()
}
case UpdateTwo() => {
updateOneAndTwoLogic() //repeated
updateTwo()
}
case Other() => {
...
}
}
}
在UntypedActor
,我可以這樣做:
public void onReceive(Object message) {
if (message instanceof UpdateMessage)
updateOneAndTwoLogic();
if (message instanceof UpdateOne)
updateOne();
if (message instanceof UpdateTwo)
UpdateTwo();
if (message instanceof Other)
...
}
其中updateOneAndTwoLogic()
不重複。
如何刪除scala版本中的重複調用?