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

  • 2
    Sorry, no, I haven't a clue what you're asking. Factory methods can, and often are, on an instance.
    – pdr
    Nov 8, 2012 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 Nov 8, 2012 at 20:03

3 Answers 3


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);
  • 2
    Essentially a Strategy pattern
    – MaximR
    Nov 8, 2012 at 21:37
  • I think I will use Strategy pattern approach.
    – ilazgo
    Nov 9, 2012 at 11:16

Is this not a classic case for generics?

    // OR...

catch (...)
    ... handle if XmlReceived is not a supported type

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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