Abstract Factory Using Generics: Is Explicitly Converting a Specified Type to Generic a Bad Practice
- by Merritt
The question's title says it all. I like how it fits into the rest of my code, but does it smell?
public interface IFoo<T>
{
T Bar { get; set; }
}
public class StringFoo : IFoo<string>
{
public string Bar { get; set; }
}
public static class FooFactory
{
public static IFoo<T> CreateFoo<T>()
{
if (typeof(T) == typeof(string))
{
return new StringFoo() as IFoo<T>;
}
throw new NotImplementedException();
}
}
UPDATE: this is sort of a duplicate of
Is the StaticFactory in codecampserver a well known pattern?