I am making a network application. Here is the problem:
I have a Messenger class. The Messenger has several module classes named ModuleA,B,C etc.
The messenger sends MessagePacks (to other Messengers). Each MessagePack has a module destination meaning once it reaches Messenger it is forwarded to the correct Module(A,B,C etc).
At the moment in order to forward to the correct module I’m using an if-else checking for a tag on MessagePack to decide where to forward.
What I would like to do is have MessagePack subclasses instead of using tags(tags are Strings).So TypeAMessage goes to ModuleA etc. The only way I can think to do that is having an instance of Messenger in the MessagePack and call a method like this: Messenger.fowardToModuleA(this); but it doesn’t make sense(and probably causes problems) to have an instance of Messenger on MessagePack.
Can anyone think of a way to complete the task I want without using the if-else checking for tag strings and preferably using MessagePack subclasses?
An(other) option would be to let the
Modules decide whichMessagePackthey accept and which not. So yourMessengerwould still contain/link to severalModuleclasses, but just forward every incomingMessagePacktowards allModuleslikeWhether or not those module’s use a String identifier on the messagepack, or only accept certain specific implementations of a messagepack interface is up to you. With this approach you have
Messengerclass which needs no adjustments when you add modules or messagepack typesNot sure whether this is an official design pattern with an official name, but that is how I would handle this