Best Practices: What Should You Use Reflection For?

I have been playing around with the idea of ​​allowing a module 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 appropriate modules and then call methods on the appropriate base classes or pass objects as required parameters.

  • Is this a good place to use reflection?
  • Is there any guidance as to where to use reflection already posted on SO (I couldn't find it)? Can we start the list along these lines with any feedback posted here?

EDIT Here is another example of the scripts I have in mind.

Some basic codes are required to define a dial peer. One application I have seen achieved this using reflection, another application was using an exception. Do you think the former is the recommended scenario in which reflection can be applied?

+2


source to share


3 answers


For a great foundation supporting your idea, have a look at the IOC container in the spring framework .



+5


source


Is this a good place to use reflection?

I would say no. If you want to do things like this, you should probably use one of the (pre-existing) mature frameworks that support inversion of control, such as dependency injection. Spring IOC is the most popular, but there are many others. Google for "ioc framework java".



Under the hood, these frames most likely use reflection. But that doesn't mean you have to reinvent the wheel.

+2


source


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

0


source







All Articles