2014-01-06 29 views
2

我正在努力創建具有自我類型的演員的新方法。創建包含自我類型的道具的演員

假設我有一個演員

trait BarPolicy { 
    val maxDrinksNumber:Int 
} 

trait ProductionPolicy extends BarPolicy { 
    val maxDrinksNumber = 5 
} 

object FooActor { 
    def apply() = new FooActor with ProductionPolicy 
} 

class FooActor extends Actor { 
    this: BarPolicy => 
} 

有了這樣的代碼,我可以寫這樣的事:

context.actorOf(Props(FooActor())) 

現在不贊成這種方式,我只是找不到正確這樣做的另一種方式。

「應用()」方法現在應該根據這個樣子什麼阿卡人建議:

def apply() : Props = Props(classOf[FooActor]) 

,但我在哪裏可以把混入?

回答

2
object FooActor { 
    private class ProductionFooActor extends FooActor with ProductionPolicy 
    def apply() : Props = Props(classOf[ProductionFooActor]) 
} 

也許這樣的事情?

+0

這個解決方案是在我腦海中浮現的東西,但我認爲這不是很優雅。但是,如果沒有其他選擇可用,我可以應付它。 – almendar

0

是這樣的?

object FooActor { 
    def apply() : Props = Props(new FooActor with ProductionPolicy) 
} 

val actor = system.actorOf(FooActor()) 
+0

此版本現在被akka勸阻: http://doc.akka.io/docs/akka/snapshot/scala/actors.html#Deprecated_Variants – almendar

+0

不知道,謝謝。 – Peter

+1

這裏顯示的用法不是有問題的情況之一(在Actor中使用時),我們將更新文檔以反映此情況。 –