tags:

views:

176

answers:

3

I was toying with the idea of allowing module to with a class in a properties file ; something like

availableModules.properties
Contact=org.addressbook.ContactMain
Business=org.addressbook.BusinessMain
Notes=org.addressbook.Notes

...

My framework will use reflection to instantiate the relevant modules, and thereafter call methods on the relevant base classes, or pass the objects as parameters as required.

  • Is the above a good place to use reflection?
  • Are there any best practices on where to use reflection already posted on SO (I couldnt' locate one)? Could we start a list along those lines with any responses posted here?

EDIT Here's another example of the kind of scenarios I have in mind.

Some core code needed to determine the point of call. One application I saw achieved this by using reflection, another application used an exception. Would you deem the former to be a recommended scenario where reflection may be applied?

+4  A: 

For a great framework supporting your idea have a look at the IOC container of the spring framework.

tangens
Yeap- this question immediately made me think of IOC
RichardOD
+1 IOC: me too. Use Spring framework, once you understood everything you won't live without it any more.
Juri
Aw c'mon guys (+: I'm looking for a list of places to use Reflection. Anyplace where IOC is mandated pretty much covers it; but UI Frameworks are just one item in the list
Everyone
+2  A: 

Is the above a good place to use reflection?

I'd say no. If you want to do this kind of thing, you should probably be using one of the (many) existing mature frameworks that support Inversion of Control aka Dependency injection. Spring IOC is the most popular one, but there are many others. Google for "ioc framework java".

Underneath the hood, these frameworks most likely use reflection. But that doesn't mean you should reinvent the wheel.

Stephen C
A: 

I usually used reflection if I want to dynamically use a class which information (assembly name, class name, method name, method parameters, etc) are stored in a string (text files or database).

dkartopr