Alex Rivera | Logout

Should actor messages extend a common trait?

Asked 2011-06-12T14:20:16.060
11

In most examples of proper usage of the scala (and akka) actor frameworks, people tend to derive every message from a single trait. For example:

trait Message
object Ping extends Message
object Pong extends Message

However, in both Scala and Akka, message reception is not typed at all. Is there any reason to implement a common trait ?

Edit
Report

1 Answer

0

This is not mandatory but it is an OO design stuff. It is a better design to have an abstract type for you application domain messages. So you can have polymorphism benefits when dealing with Messages in our application code.

  
trait Message

object Ping extends Message
objet Pong extends Message

object Stop
  

For example if somewhere in your application you have to deal with bunch of messages regardless of their specific types (Ping or Pong) you will treat them all as objects of type Message. It makes sense? No ?

answered 2011-06-12T15:16:10.830

Your Answer