Lets say I have 10 xml message types that come into my program via some type of input (file, port, etc). I have 10 subclasses of a type Message to handle the unique needs of each message type.
When a new message comes in, is there a better way than a switch statement that switches on a substring of the message, or a huge if/elseif tree to decide which subclass of Message I need to instantiate with the incoming message string?
I'd like to pass the string to a factory and have the proper subclass returned, but I am unable to find a way that does not use a switch statement.
edit: I did not specify a language because I've run into a similar situation in more than one language so I wanted a generic solution.
switch
is the programmer's equivalent to destroying a village in order to save it.