Here is the Class.
Code: Select all
class MessageFactory {
private MessageFactory() {
}
static Message createInboundMessage(String message, NetworkThread s) throws Exception {
try {
StringBuffer className = new StringBuffer("net.msnlib.");
if (Character.isDigit(message.charAt(0)) ) {
className.append('E');
}
className.append(message.substring(0, 3));
className.append("Message");
//Class m = MessageFactory.class.getClassLoader().loadClass(className.toString());
//Constructor con = m.getConstructor(new Class[] {String.class, NetworkThread.class});
//return (Message)con.newInstance(new Object[] {message,s});
return (Message)Class.forName(className.toString()).newInstance();
} catch (Exception e) {
e.printStackTrace();
throw new Exception("Un-known command");
}
}
}
For an example, if className.append(message.substring(0, 3)); appends "USR" to the className, the Factory method should produce a "USRMessage" type instance and return it. Then my application can call the processMessage() (which is an abstract method in Message class) of USRMessage class to process the USRMessage.
I can see that this is not going to work as I expected. Because I do not have the "message" and the relevant NetworkThread instance "s" in my Factory method created class. So how can I achieve this?.
It seems to me that I can use "switch" statements to handle the each type of Message type. But that is going to be a pain since I have about 73 Message types to handle. If this is not going to work I will have to reduce the functionality of the application by reducing the number of different message types it can handle. (This is a mobile app and I am considering it as an option. )