patterncsharpMinor
Object factory from interface types
Viewed 0 times
fromfactoryinterfacetypesobject
Problem
I have a library that defines a bunch of
I made this rough implementation, but it makes my skin crawl. What would be a better approach to this problem?
Considering other alternatives, I guess I could create a method for every
ISprite interfaces, like IPieceSprite and ITileSprite. Then, it defines a global ISpriteFactory that should be supplied to the library upon initialization. ISpriteFactory should create concrete objects based on the interfaces mentioned.I made this rough implementation, but it makes my skin crawl. What would be a better approach to this problem?
public class SpriteFactoryMock: ISpriteFactory {
public SpriteType Create() where SpriteType: ISprite {
var type = typeof(SpriteType);
if (type == typeof(ITileSprite)) return (SpriteType)(Object)(new TileSprite());
if (type == typeof(IPieceSprite)) return (SpriteType)(Object)(new PieceSprite());
return default(SpriteType);
}
}Considering other alternatives, I guess I could create a method for every
ISprite interface, and that would work a bit better. I might do that if all hope for the previous approach is lost.Solution
There is a very simple way to handle this but that is all dependant on the fact you have only one implementation of each
Now there is alot wrong with this implementation as it assumes alot.
We could approach this by only scanning for the object once. But again we are stuck with the issues in point 1 and 3. This implementation is even uglier and (not tested) doesn't appear to be too thread safe.
What you are really after is some sort of dependency injection and do away with the
Now without depenency injection you could define your registrations some where else. Where although it is similar to your first approach does abstract the implementation. Something like.
Now what I have done is basically created static method to create registrations of your
All in all without a proper framework in place to take care of your dependency resolution and life-time scopes you are stuck with either having our initial 3 problems or writing out your object creation methods manually.
ISprite impelementation per interface type. If thats the case (and they all have parameterless constructors) you can simply use reflection to get your type and then construct it.class SpriteFactory : ISpriteFactory
{
public T Create() where T : ISprite
{
var type = typeof(T);
var spriteType = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p) && p.IsClass)
.FirstOrDefault();
if (spriteType == null)
return default(T);
return (T)Activator.CreateInstance(spriteType);
}
}Now there is alot wrong with this implementation as it assumes alot.
- That there is only one implenetation of the
ISpritetype passed in.
- It does an assembly scan everytime you need to create a new instance of this object.
- Requires all implementations to have a default parameterless constructor on the implementation.
We could approach this by only scanning for the object once. But again we are stuck with the issues in point 1 and 3. This implementation is even uglier and (not tested) doesn't appear to be too thread safe.
class SpriteFactory : ISpriteFactory
{
static object _locker = new object();
Dictionary registrations = new Dictionary();
public T Create() where T : ISprite
{
var type = typeof(T);
if (!registrations.ContainsKey(type))
{
lock (_locker)
{
var spriteType = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p) && p.IsClass)
.FirstOrDefault();
registrations[type] = spriteType;
}
}
var regType = registrations[type];
return regType != null ? (T)Activator.CreateInstance(regType) : default(T);
}
}What you are really after is some sort of dependency injection and do away with the
SpriteFactory all together. Dependency injection was made for this type of work and although the initial code is usually a bit more its typically more elegant. Especially when it comes to managing constructor injection, object disposal etc. The real power comes in when you need to start extending your application and adding more types. Now without depenency injection you could define your registrations some where else. Where although it is similar to your first approach does abstract the implementation. Something like.
interface ISpriteFactory
{
T Create() where T : ISprite;
}
class SpriteFactory : ISpriteFactory
{
static Dictionary> registrations = new Dictionary>();
public static void AddRegistration(Func aquire)
{
registrations[typeof(T)] = aquire as Func;
}
public T Create() where T : ISprite
{
if (registrations.ContainsKey(typeof(T)))
return (T)registrations[typeof(T)]();
return default(T);
}
}Now what I have done is basically created static method to create registrations of your
ISprite implementations. This can be used as such.class Program
{
static void Main(string[] args)
{
SpriteFactory.AddRegistration(() => new PieceSprite());
SpriteFactory.AddRegistration(() => new TileSprite());
var factory = new SpriteFactory();
var piece = factory.Create();
}
}All in all without a proper framework in place to take care of your dependency resolution and life-time scopes you are stuck with either having our initial 3 problems or writing out your object creation methods manually.
Code Snippets
class SpriteFactory : ISpriteFactory
{
public T Create<T>() where T : ISprite
{
var type = typeof(T);
var spriteType = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p) && p.IsClass)
.FirstOrDefault();
if (spriteType == null)
return default(T);
return (T)Activator.CreateInstance(spriteType);
}
}class SpriteFactory : ISpriteFactory
{
static object _locker = new object();
Dictionary<Type, Type> registrations = new Dictionary<Type, Type>();
public T Create<T>() where T : ISprite
{
var type = typeof(T);
if (!registrations.ContainsKey(type))
{
lock (_locker)
{
var spriteType = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p) && p.IsClass)
.FirstOrDefault();
registrations[type] = spriteType;
}
}
var regType = registrations[type];
return regType != null ? (T)Activator.CreateInstance(regType) : default(T);
}
}interface ISpriteFactory
{
T Create<T>() where T : ISprite;
}
class SpriteFactory : ISpriteFactory
{
static Dictionary<Type, Func<object>> registrations = new Dictionary<Type, Func<object>>();
public static void AddRegistration<T>(Func<T> aquire)
{
registrations[typeof(T)] = aquire as Func<object>;
}
public T Create<T>() where T : ISprite
{
if (registrations.ContainsKey(typeof(T)))
return (T)registrations[typeof(T)]();
return default(T);
}
}class Program
{
static void Main(string[] args)
{
SpriteFactory.AddRegistration<IPieceSprite>(() => new PieceSprite());
SpriteFactory.AddRegistration<ITileSprite>(() => new TileSprite());
var factory = new SpriteFactory();
var piece = factory.Create<IPieceSprite>();
}
}Context
StackExchange Code Review Q#121743, answer score: 5
Revisions (0)
No revisions yet.