Im trying to find a solution in which the class that processes the "message" is selected at runtime depending on the type of message. I know I can use something like this
if msg_type = "A" MsgAProcessor.execute(message); else if msg_type = "B" MsgBProcessoror = execute(message); .... .... ....
I do not want to use the above approach, because I do not want the code to know anything about the types of messages that I could handle. I want to be able to add a new message processor in the future for a new message type. The solution I'm thinking of now is as follows
There are currently 3 message processors
MsgAProcessor MsgBProcessor MsgBProcessor
All three classes have a method called execute that will process the message in their own way. I created the MsgProcessor interface and added the execute () method in the interface.
Now itβs hard for me to understand which message processor should call the caller without checking the type of message. For example, I canβt do it
MsgProcessor proc = new MsgAprocessor () proc.execute ()
The above should be indicated in the if statement, since it must be called immediately after determining the type of message. I would also like to avoid instantiating using an implementation class type.
Is there a better way to achieve the same?
I want to be able to just call MsgProcessor.execute from the interface and have a runtime that knows the implementation class to call based on the message type.
java java-ee class-design classloader
ziggy
source share