My system successfully passes objects from a client to servlet. However it is primitive as it was built to cater for Java 1.1. The message object it passes consists of an int (representing one of about seventy types) and a String of tokens which need to be parsed (the tokens may contain a list, a list of objects, etc). Not good!
So, I'm looking to refactor this to Java 1.5. Using an enum instead of an int is certainly an improvement, but I'm unsure how to send the rest of the message. Creating seventy different classes to represent each type surely isn't the correct way to go.
Any pointers on how I should refactor this?
There is no need to create a different class to represent each type of message. You create one message class with the properties that you need. Something like this:
public class Message implements Serializable{
private Long id;
private String msgText;
//other necessary properties
public Message(){
this(0, "default message");
}
public Message(Long id, String msgText){
setId(id);
setMsgText(msgText);
//etc
}
//getters and setters
}
And you then create objects as needed. For example:
Message m1 = new Message(9, "The Eagle has landed");
//serialize m1 to server
Message m2 = new Message(27, "The Wren has landed");
//serialize m2 to the server
and so on.