[英]Akka actor implementation: extension or injection?
I am brand new to Akka (I'm using the Java lib v2.3.9), and am wondering what the tradeoff(s) is between two competing actor implementation strategies. 我是Akka的新手(我正在使用Java lib v2.3.9),并且想知道两个竞争的actor实现策略之间的权衡是什么。 On one hand I could implement my actors the standard way:
一方面,我可以用标准方式实现我的演员:
// Groovy pseudo-code
class StormTrooper extends UntypedActor {
@Override
void onReceive(Object message) {
if(message instanceof ExecuteOrder66) {
// Betray all Jedi, serve the Emperor!
}
}
}
class DarthVader extends UntypedActor {
@Override
void onReceive(Object message) {
if(message instanceof Feels) {
// Betray the Emperor, save your son!
}
}
}
class Emperor extends UntypedActor {
@Override
void onReceive(Object message) {
if(message instanceof Mace) {
// Transform into your true self!
}
}
}
...instead of doing that, I could add a layer of abstraction via injection: ...而不是这样做,我可以通过注入添加一层抽象:
class StarWarsCharacter extends UntypedActor {
@Inject // Injected behavior; implementation doesn't matter
BehaviorHandler behaviorHadler
@Override
void onReceive(Object message) {
behaviorHandler.handle(message)
}
}
interface BehaviorHandler {
void handle(Object message)
}
class StormTrooperBehaviorHandler implements BehaviorHandler {
@Override
void handle(Object message) {
// Betray all Jedi, serve the Emperor!
}
}
class DarthVaderBehaviorHandler implements BehaviorHandler {
@Override
void handle(Object message) {
// Betray the Emperor, save your son!
}
}
class EmperorBehaviorHandler implements BehaviorHandler {
@Override
void handle(Object message) {
// Transform into your true self!!
}
}
Are there performance/classloading benefits to only having one type of actor and injecting it with different BehavioHandler
implementations? 是否只有一种类型的actor并使用不同的
BehavioHandler
实现注入性能/类加载的好处? Is there a reason why I would not want to do this and use the "standard" actor implementation? 是否有一个原因,我不想这样做,使用“标准”实施的演员?
If you just want to use your Actor as a way of dispatching messages to handlers, then the 2nd (non standard) way will be fine, and could give you some benefits in terms of code structure - eg the fact that BehaviorHandler
is a single method interface would allow you to implement it with a lambda. 如果你只想使用你的Actor作为向处理程序发送消息的方式,那么第二种(非标准)方式就可以了,并且可以在代码结构方面给你一些好处 - 例如,
BehaviorHandler
是一个单一的方法接口将允许您使用lambda实现它。
However , Actors are not simply method dispatchers. 然而 ,演员并不是简单方法调度。 They provide a variety of methods that you will need as your system grows in complexity.
随着系统复杂性的增加,它们提供了各种方法。
See http://doc.akka.io/docs/akka/2.0/java/untyped-actors.html#untypedactor-api 见http://doc.akka.io/docs/akka/2.0/java/untyped-actors.html#untypedactor-api
A typical, moderate sized Akka system will need references to self
, sender
and context
which are defined on UntypedActor
. 典型的中等大小的Akka系统需要引用
UntypedActor
上定义的self
, sender
和context
。
In short: if you don't use inheritance then you are giving up more than you realise. 简而言之:如果你不使用继承,那么你放弃的比你意识到的要多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.