I am constructing a new class which handles deciding which class should process the requests from a web service. I'm looking for a solution for a problem that I am having regarding extensibility with this new class. Each request will be processed differently, and the logic for processing them each are in their own class. The problem that I am hoping to solve is how to design the classes in such a way that adding a new type of request does not require any changes to existing code. Consider the code that I have below. Please pardon any typos in the following examples:
public interface IRequestData
{
}
public class OldRequestData : IRequestData
{
// Some properties holding data.
}
public class NewRequestData : IRequestData
{
// Some properties holding data.
}
public class ProcessOldRequestData
{
public void Process(OldRequestData Data)
{
// Some code to process the request.
}
}
public class ProcessNewRequestData
{
public void Process(NewRequestData Data)
{
// Some code to process the request.
}
}
public class RequestProcessor
{
public void ProcessRequest(IRequestData Data)
{
// Get and load base type of Data
// Based on if it's OldRequestData, NewRequestData, or *FutureRequestData*,
// Instantiate the appropriate class to process the request.
}
}
This is the model that I am currently working with. The problem is each new type of request requires changes to the RequestProcessor to call the new class. Is there a way to make this class "Know" which class it needs to call to process each type of request? If there is a better of handling this I would be very open for other suggestions.
Thank you in advance for your input.