简体   繁体   中英

Why we create Actors and messages in akka as static classes?

I am learning Akka and in akka docs,they have used static classes for actors and messages so I want to know why we create Actors and messages in akka as static classes.And this is what I know:- We cannot create toplevel static class and since we can create only a inner static class and a inner static class instance is not linked to a particular enclosing class instance. And this means multiple instances share same static class instance. So if we are creating Actors and messages static then there will be only one instance per static class.

You don't HAVE to create the messages as static classes.

There are two main reasons for it:

One is what Snickers was mentioning, ie we want to make messages immutable and it's sintactically easier to do so in Java via static classes. You could do without this by setting all the fields as private and/or final, but that might make your serialization/deserialization more complicated (@JsonCreator if you are using Jackson for example...).

The second is that my declaring the messages as inner classes of the actor that accepts them, you establish a semantical relation between the two making it easier to know what actor accepts what messages.

But you can easily disregard all these suggestions and build them as normal classes and you yourself making sure that they are not modified by anyone in other ways.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM