principlecsharpMajor
Implementing factory design pattern with generics
Viewed 0 times
factorywithdesigngenericsimplementingpattern
Problem
In this new project I'm working on I need to create objects on runtime by data from the DB, right now I have two groups of classes, each group implementing a different interface.
I started working on a factory class, which will map id to a type, an abstract one so I can extend with a factory for each group.
The constructor parameter is the type of the interface common to all implementors of the group.
Then I noticed both extending factories look the same, and in the end with the use of generics I got to this class:
So I can just use like so:
```
Gene
I started working on a factory class, which will map id to a type, an abstract one so I can extend with a factory for each group.
The constructor parameter is the type of the interface common to all implementors of the group.
abstract class Factory
{
private Dictionary idsToTypes;
private Type type;
public Factory(Type _type)
{
idsToTypes = new Dictionary();
type = _type;
}
protected Object GetProduct(int id, params object[] args)
{
if (idsToTypes.ContainsKey(id))
{
return Activator.CreateInstance(idsToTypes[id], args);
}
else
{
return null;
}
}
public void RegisterProductType(int id, Type _type)
{
if (_type.IsInterface || _type.IsAbstract)
throw new ArgumentException(String.Format("Registered type, {0}, is interface or abstract and cannot be registered",_type));
if (type.IsAssignableFrom(_type))
{
idsToTypes.Add(id, _type);
}
else
{
throw new ArgumentException(String.Format("Registered type, {0}, was not assignable from {1}",_type,type));
}
}Then I noticed both extending factories look the same, and in the end with the use of generics I got to this class:
class GenericSingletonFactory : Factory
{
static public GenericSingletonFactory Instance = new GenericSingletonFactory();
private GenericSingletonFactory()
: base(typeof(T)) { }
public T GetObject(int id, params object[] args)
{
Object obj = base.GetProduct(id, args);
if (obj != null)
{
T product = (T)obj;
return product;
}
else return default(T);
}
}So I can just use like so:
```
Gene
Solution
Several considerations:
-
Base
-
Your singleton instance is stored in a public field. This means any part of your program can easily replace it. You should make it readonly, or (even better) expose it through a readonly property.
-
If you are not abstracting the factory (i.e. exposing the
-
you can either skip accessing
-
or, you can decide that you want to hide the implementation of the factory behind an interface (
-
If you add a new generic parameter to the
-
Are you sure that you need to pass the constructor parameters to the
First four points (simplified) would result in something like:
Usage:
Fifth point is worth considering. If you change your factory to this:
Then you can use it like this:
This delegates all construction logic to init time which allows complex scenarios for different object types. You can, for example, make it return a singleton on each call to
-
Base
Factory class does all the work, so I would simply merge it into the generic method.-
Your singleton instance is stored in a public field. This means any part of your program can easily replace it. You should make it readonly, or (even better) expose it through a readonly property.
-
If you are not abstracting the factory (i.e. exposing the
Instance property as an interface), it means there is no way to use any other factory in your program. This has two possible consequences:-
you can either skip accessing
Instance every time and use a plain static method (Factory.Instance.DoStuff(...) becomes Factory.DoStuff(...), which is slightly shorter)-
or, you can decide that you want to hide the implementation of the factory behind an interface (
IFactory, for example), in which case a concrete factory will be stored in an instance of a separate class, which would completely change the call to something like DAL.GetFactory().DoStuff(...) (DoStuff becomes an instance method). This leaves allows greater flexibility, since you can pass an IFactory to a part of code which doesn't need to know about your static DAL classes. But it would require additional redesign.-
If you add a new generic parameter to the
RegisterProductType method, you can use the where clause to limit the type to derived types at compile time. Getting a compile error is much better than getting a run-time one.-
Are you sure that you need to pass the constructor parameters to the
GetProduct method? This part might be slightly unusual (although there might be cases where you would want to instantiate your classes by passing a parameter). What it there is a specific derived type which accepts different parameters than other types? Activator.CreateInstance indicates that you are forcing all your callers to know which constructor your method will use.First four points (simplified) would result in something like:
public class Factory
{
private Factory() { }
static readonly Dictionary _dict = new Dictionary();
public static T Create(int id, params object[] args)
{
Type type = null;
if (_dict.TryGetValue(id, out type))
return (T)Activator.CreateInstance(type, args);
throw new ArgumentException("No type registered for this id");
}
public static void Register(int id) where Tderived : T
{
var type = typeof(Tderived);
if (type.IsInterface || type.IsAbstract)
throw new ArgumentException("...");
_dict.Add(id, type);
}
}Usage:
Factory.Register(1);
Factory.Register(2);
// this is the "suspicious" part.
// some instances may require different parameters?
Factory.Create(2, "Garfield");Fifth point is worth considering. If you change your factory to this:
public class Factory
{
private Factory() { }
static readonly Dictionary> _dict
= new Dictionary>();
public static T Create(int id)
{
Func constructor = null;
if (_dict.TryGetValue(id, out constructor))
return constructor();
throw new ArgumentException("No type registered for this id");
}
public static void Register(int id, Func ctor)
{
_dict.Add(id, ctor);
}
}Then you can use it like this:
Factory.Register(1, () => new Dog("Fido"));
Factory.Register(2, () => new Cat("Garfield", something_else));
// no need to pass parameters now:
IAnimal animal = Factory.Create(1);This delegates all construction logic to init time which allows complex scenarios for different object types. You can, for example, make it return a singleton on each call to
Create:// each call to Factory.Create(3) should return the same monkey
Factory.Register(3, () => Monkey.Instance);Code Snippets
public class Factory<T>
{
private Factory() { }
static readonly Dictionary<int, Type> _dict = new Dictionary<int, Type>();
public static T Create(int id, params object[] args)
{
Type type = null;
if (_dict.TryGetValue(id, out type))
return (T)Activator.CreateInstance(type, args);
throw new ArgumentException("No type registered for this id");
}
public static void Register<Tderived>(int id) where Tderived : T
{
var type = typeof(Tderived);
if (type.IsInterface || type.IsAbstract)
throw new ArgumentException("...");
_dict.Add(id, type);
}
}Factory<IAnimal>.Register<Dog>(1);
Factory<IAnimal>.Register<Cat>(2);
// this is the "suspicious" part.
// some instances may require different parameters?
Factory<IAnimal>.Create(2, "Garfield");public class Factory<T>
{
private Factory() { }
static readonly Dictionary<int, Func<T>> _dict
= new Dictionary<int, Func<T>>();
public static T Create(int id)
{
Func<T> constructor = null;
if (_dict.TryGetValue(id, out constructor))
return constructor();
throw new ArgumentException("No type registered for this id");
}
public static void Register(int id, Func<T> ctor)
{
_dict.Add(id, ctor);
}
}Factory<IAnimal>.Register(1, () => new Dog("Fido"));
Factory<IAnimal>.Register(2, () => new Cat("Garfield", something_else));
// no need to pass parameters now:
IAnimal animal = Factory<IAnimal>.Create(1);// each call to Factory<IAnimal>.Create(3) should return the same monkey
Factory<IAnimal>.Register(3, () => Monkey.Instance);Context
StackExchange Code Review Q#8307, answer score: 41
Revisions (0)
No revisions yet.