First of all, it is not clear why you are creating classes derived from your common class, instead of directly using the common class. It is also not clear why you need a factory, and not just create any classes that you need directly. These patterns are important and useful, often the only way to solve certain problems, but they are not requirements. You must have good reasons for their implementation.
I assume that you really have good reasons why you left for the sake of brevity.
The point of using the factory pattern is that some code creates an instance of the correct class without this code, knowing the correct class. Note that in your example, this separation is missing : the one who calls MyClassFactory.CreateMyClass<T> () must know the correct class, since this code must pass the type as a general parameter that defines the correct class. If I know enough to call CreateMyClass<int> () , then I know enough to call new MyDerivedIntClass () .
There are two main ways to implement a factory pattern: static functions and factory classes.
In both methods, you will need an abstract base class or generic interface:
public interface IMyInterface { string Foo (); } public abstract class MyGenericBaseClass<T> : IMyInterface {
Using a static function, you will need a specific delegate type (the scope of the class is omitted):
and classes can implement them to return the correct type.
public sealed class MyDerivedIntClass : MyGenericBaseClass<int> { // ... static IMyInterface CreateObject () { return new MyDerivedIntClass (); } } public sealed class MyDerivedStringClass : MyGenericBaseClass<string> { // ... static IMyInterface CreateObject () { return new MyDerivedStringClass (); } }
Passing a static function to a function that instantiates an object:
// ... somewhere else in the code ... // create a IMyInterface object using a factory method and do something with it void Bar (MyInterfaceFactory factory) { IMyInterface mySomething = factory (); string foo = mySomething.Foo (); } // ... somewhere else in the code ... void FooBarAnInt () { Bar (MyDerivedIntClass.CreateObject); }
The second way is to use factory classes:
public interface IMyInterfaceFactory { IMyInterface CreateObject (); } public class MyDerivedIntFactory : IMyInterfaceFactory { public IMyInterface CreateObject () { return new MyDerivedIntClass (); } } public class MyDerivedStringFactory : IMyInterfaceFactory { public IMyInterface CreateObject () { return new MyDerivedStringClass (); } }
Note that you can (and probably should) also make factory classes single, but that is another problem. Alternatively, you can use an abstract base class instead of an interface; you need to add abstract (or virtual ) and override as needed.
In fact, someone, somewhere, somehow needs to know the correct type of object to instantiate. The point of factory objects is not to completely distract this knowledge, but to separate the knowledge of what type of object to create from the code that actually creates the object. Unless you need this separation, the factory pattern is not particularly useful.