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.

  • 1
    What language? Either way, one could use reflection to figure out what subclass to use for a message type. Attributes are also useful. If reflection is slow, then you can pre-cache it in a dictionary. This is, of course, if reflection is available in the language you're using
    – 9a3eedi
    Commented Jan 13, 2015 at 7:06
  • Invoking reflection in order to save the perceived inelegance of a switch is the programmer's equivalent to destroying a village in order to save it. Commented Jan 13, 2015 at 7:56

2 Answers 2


The Command pattern would likely best suit your needs in this case. Have a MessageHandler base class that has two methods.

The first would be called IsHandlerFor that takes as input the xml and returns true if it is the proper handler for that specific type of message. Then you'd have a second method called Handle that takes the xml and performs the proper action with it.

You would have a MessageBus class that initially instantiates a list of these handlers. If you wish to separate concerns further, you could use a factory, pass it to the constructor of MessageBus, or use reflection and a configuration file.

Then when a new message is received, you loop through all your MessageHandlers calling IsHandlerFor. If it returns true, you call its Handle method. If you're sure that there will only be one such handler, break out of your loop. Otherwise, continue for all handlers and without knowing how each handler works, you've managed to handle a message without using a switch and by modularizing your code.

Hope that helps.

  • The link you provided to the Command pattern has put me on a new track. I can probably use that for my current use case.
    – bh3333
    Commented Jan 14, 2015 at 3:28
  • @bh3333 Glad I could help.
    – Neil
    Commented Jan 14, 2015 at 8:54

There are three general approaches to deciding which sub-class to construct when you only have a serialized data stream.

  1. Hard-code the Message sub-classes and their identifying tags in the Factory class and select the right Message using a switch or if/else ladder.
  2. The Factory class keeps a dynamic registry of supported Message sub-classes and performs a linear search over the registry to select the correct Message sub-class. This solution requires that somehow the Message sub-classes get registered with the Factory and the Factory needs to be able to instantiate classes that it doesn't really know about (the Factory only knows about the base Message class). This can be done by registering a factory function or a prototype object for each Message sub-class.
  3. You use reflection to match the XML tag to a Message sub-class. This all but requires that the class names correspond to the XML tag names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.