I have a generic class, let's call it MyClass<T>
, which will need to have a factory method so to abstract constructor details away from client code.
Which of the two options is correct? (example instantiation code included)
Static non-generic factory method on the original generic MyClass<T>
:
MyClass<SomeType> instance = MyClass<SomeType>.CreateNew();
Static generic factory method on a dedicated, non-generic static MyClass
implementation:
MyClass<SomeType> instance = MyClass.CreateNew<SomeType>();
On the first sight, looks like proper answer to your question is #1. This is because your class is MyClass<T>
and hence factory should also be T
-specific. But there is more into it than this simple answer.
Before proceeding, I would add the third possibility: non-static factory class. Object which relies on a factory would have a public property through which it receives a factory object. Getter of the property would instantiate default factory if no other instance has been assigned. This allows dependency injection later, and also helps write unit tests that rely on their own fake factory. Solution would look something like this (ignore generics for a moment):
public class ISomeFactory { ... }
public class DefaultSomeFactory: ISomeFactory { ... }
public class ClientClass
{
public ISomeFactory FactoryOfSome // Right place for dependency injection
{
get
{
if (_fact == null)
_fact = new DefaultFactory();
return _fact;
}
set { _fact = value; }
}
private ISomeFactory _fact;
}
Now as I said, your class goes with generic parameter MyClass<T>
and then factory should go with generic parameter as well: Factory<T>
. This solution is better than generic method on general factory, simply because creating an instance may be T
-specific. Solution with generic factory allows you this:
public class SpecialSomeFactory: DefaultSomeFactory<string> { ... }
In this way, you can override behavior of existing factory class and have an alternative way to generate instances that are specialized for strings. This is important because dealing with strings is often much different than dealing with primitive types like int or double. Having an opportunity to specialize the factory may be beneficial. But now you see why it might be a bad idea to have a static factory - statics cannot be extended.