2017-04-26 63 views
0

我有一個簡單的演員系統與默認配置。阿卡靜態使用

我有一個類擴展演員

class Test extend Actor { 
    def receive: Receive = { 
     case Foo(collection) => sender ! extract(collection) 
    } 

    private def extract(c: List[FooItem]): List[BarItem] = ??? 
} 

該角色有個同伴對象

object Test { 
    def props: Props = ??? 
} 

的是,有做安全功能的提取物是這樣的:

object Test { 
    def props: Props = ??? 
    def extract(c: List[FooItem]): List[BarItem] = ??? 
} 

和使用來自另一位演員?

回答

1

是的,可以在伴侶上定義方法,然後在演員類中導入和使用該方法。像這樣的東西會工作得很好:

object Test { 
    def props: Props = Props[Test] 
    def extract(c: List[FooItem]): List[BarItem] = { 
    . . . 
    } 
} 

class Test extend Actor { 
    import Test._ 
    def receive: Receive = { 
    case Foo(collection) => sender ! extract(collection) 
    } 
} 
+0

謝謝!我只是擔心多線程。這種情況下是否有一些演員? – HoTicE

+0

所示的示例 - 一個不依賴於actor的純函數 - 使用這種方式很好。如果你將'ActorContext'傳遞給'extract'函數,那麼你就不應該這樣做。 – Ryan

+0

謝謝,瑞恩!現在很清楚。 – HoTicE