Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have to add new funcionality in a web service legacy project and I'm thinking what is the best approach for a concrete situation.

The web service is simple: It receives a XML file, unmarshalling, generates response's objects, marshalling and finally it sends the response as a XML file. For every XML files received, the web service always responds with the same XML structure.

What I have to do is to generate a different XML file according to the XML received. So I have a controller class which has all marshalling/unmarshalling operations, but this controller class has to be an instance. Depending on XML received I need some marshalling methods or others.

Trying to make few changes on legacy source, what is the best approach? My first approach was to do a factory method pattern with the controller class, but this class has to be an instance.

I want to keep, as far as it goes, this structure:


I think this one is a bit smelly:

else if(XMLReceived.isTypeTwo())
else if(XMLReceived.isTypeThree())
else if ...

I hope my question is well understood

share|improve this question
Sorry, no, I haven't a clue what you're asking. Factory methods can, and often are, on an instance. – pdr Nov 8 '12 at 19:47
A class is never an instance. You create instances (objects) of classes when you "instantiate" them with new. Static and abstract classes cannot be instantiated. I guess when you say ` this controller class has to be an instance` you mean – Tulains Córdova Nov 8 '12 at 20:03
up vote 4 down vote accepted

This is a common problem, mapping input to a class that can process it. One common solution is to build a map from the input type to the processing class. To handle input, you determine the input type, use the map to look up the processing class, then instantiate the class and call the desired methods.

In Ruby:

processorByType = { 'type1' => Type1Processor, 
                    'type2' => Type2Processor, 

def handle(input)
    processorByType[input.type].new().process(input, context);
share|improve this answer
Essentially a Strategy pattern – MaximR Nov 8 '12 at 21:37
I think I will use Strategy pattern approach. – ilaz Nov 9 '12 at 11:16

Is this not a classic case for generics?

    // OR...

catch (...)
    ... handle if XmlReceived is not a supported type
share|improve this answer

You could use the Strategy pattern. It does exactly what you want, it executes different algorithms for different scenarios which are often related. The wikipedia article gives an example implementation for java.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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