How do I use a Interface
I have a class called QueueMessageHandler that is a runnable thread which is passed a XML Document. I open the XML Document and based on the type of xml document I create what I call a Handler Class, ie...EmailMessageHandler, SMSMessageHandler, etc... which all implement a MessageHandler interface, that contains one method, boolean process().
My question is how can I have the QueueMessageHandler class use the MessageHandler interface to create the necessary Handler Classes, without having to explictly say new EmailMessageHandler(), or new SMSMessageHandler()? Is there some way I can utilize the MessageHandler Interface to do this for me?
Thanks.
[686 byte] By [
mglicks] at [2007-9-26 2:40:16]

There are a couple of ways you could handle this. No matter which you choose, you need a mechanism that turns some information about the XML doc. (by "type of xml docuement" do you mean something in the content, or some other meta-info?)
Method 1: Simple if statements, probably inside a factory method.
MessageHandler createMH(XMLDoc doc) {
if (doc.getSomeAttribute().equals("EMAIL")) {
return new EmailMessageHandler();
}
if (doc.getSomeAttribute().equals("SMS")) {
return new SMSMessageHandler();
}
}
MessageHandler mh = createMessageHandler(doc);
This way is a simple and direct, but it's also ugly and inflexible. If you add new message types to handle, you have to change that code. A better way would be if you didn't have to change existing code, but just add a new class. You can do that using reflection.
Method 2: Use reflection. This assumes that the name of the class you want is in the XML doc, or can be generated from the doc.
MessageHandler createMH(XMLDoc doc) {
// Note: I'm ignoring exceptions here, for brevity's sake
String className = doc.getClassNameFromContentsOrWhatever();
MessageHandler mh = (MessageHandler)Class.forName(className);
return mh;
}
jverd at 2007-6-29 10:14:06 >

Well, the problem is that something has to do the message-type decoding and call the appropriate constructor.
One thing you could do is create a MessageDecoder class that has a getMessageHandler(int type) method, which returns a handler of the appropriate type. You could make the method static so that you don't need another object reference around.
In fact, you might even move the message type stuff into the MessageDecoder, and let it figure out the message type. (If it returns null, then you have an unknown message type - for which there is no handler.
Thanks for your responses.
I decided to go with a MessageDecoder class, which accepts the notificationType and based on this it returns the string of the class to be loaded. The QueueMessageHandler then will dynamically load that Class and cast it to the MessageHandler interface, and call the process method.
It works great!
The MessageDecoder returns a string of a class, and that class implements the MessageHandler interface, so when I load the class dynamically I'm able to cast it to the MessageHandler interface.
I forgot to mention I did do that. Here's the code:
//get the appropiate message handler based on the notification type
String messageHandlerClass = messageDecoder.getMessageHandler( notificationType );
//dynamically load the class and cast it to the MessageHandler Interface
return (MessageHandler)Class.forName( messageHandlerClass ).newInstance();