I'm refactoring some code around a couple of ASP.NET Web Forms pages and decided to try out a variation of the Abstract Factory pattern of my own design. I need to create an implementer of an abstract class based on what query string value is available.
Here's (a simplified version of) what I have right now:
internal static class Factory
{
public static AbstractClass Create(NameValueCollection queryString)
{
if (queryString["class1"] != null)
{
return new Class1(queryString["class1"]);
}
if (queryString["class2"] != null)
{
return new Class2(queryString["class2"]);
}
if (queryString["class3"] != null)
{
return new Class3(queryString["class3"]);
}
return null;
}
}
I don't know how I feel about this implementation. It certainly is better than what was there before, but I have a strong sense this could be improved some how.
So now, in the relevant pages, I can simply do this:
AbstractClass item = Factory.Create(Request.QueryString);
item.DoStuff();
Does anyone have any suggestions? Or would this be considered good as-is?
?class1=someVal&class2=someOtherVal? That's something to consider, since querystrings are client-facing and can be modified by them. \$\endgroup\$Factoryclass? Why not make a static method on your Abstract class calledCreate? There are examples of this in the .NET Framework. \$\endgroup\$switchstatement might be more efficient (cite) \$\endgroup\$