vendredi 6 mars 2015

Scala: Dispatch

I'm willing how to implement an extensible dispatch mechanism in Scala.


For example:


I have a trait called Sender (with a method 'send') and a bunch of classes that implement that trait (MailSender, IPhoneSender, AndroidSender). On top of them there is a class which implements the same trait but dispatches the message to the above senders depending the type of the message.


I know I can use pattern matching, but my problem with that approach is about extensibility: If someone wants to add another sender (i.e. WindowsPhoneSender), he must add a new case to the pattern matching method (thus breaking the open-closed principle). I don't want developers to modify the library's code, so I need this to be as extensible as possible.


I thought about a chain of responsibility approach (in Java I would do that), but there is a better way in Scala? (My knowledge in Scala is limited)


Thanks!


Aucun commentaire:

Enregistrer un commentaire