Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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:

classController.doMarshalling();

I think this one is a bit smelly:

if(XMLReceived.isTypeOne())
    classController.doMarshallingOne();
else if(XMLReceived.isTypeTwo())
    classController.doMarshallingTwo();
else if(XMLReceived.isTypeThree())
    classController.doMarshallingThree();
else if ...

I hope my question is well understood

share|improve this question
2  
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 –  user61852 Nov 8 '12 at 20:03
add comment

3 Answers

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);
end
share|improve this answer
2  
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
add comment

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
add comment

Is this not a classic case for generics?

try
{
    ClassController<typeof(XmlReceived)>.DoMarshalling();
    // OR...
    ClassController.DoMarshalling<typeof(XmlReceived)>();

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

Your Answer

 
discard

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.