jeudi 1 janvier 2015

How to create a factory, which creates classes according to interface type in Java?

I'm working on migrating a C# project to java.


In the C# project I have a factory class holding a dictionary between Type, and a delegate which creates appropriate type.


I expose a generic method:



T Create<T>()


So the user can write:



var someObject = factory.Create<ISomeInterface>();


The Create method code is something like:



return (T)dictionary[typeof(T)]();


The question is, How can I do something similar in Java (version 7)?


Thanks a lot for anyone who would help!






Aucun commentaire:

Enregistrer un commentaire