I'm developing an Eclipse RCP application. I have been told that the UI part will be created by a designer (?) with Window Builder PRO so I have to create the business logic for every view our designer creates.
My first idea was to create a Controller class for each view and handle the application logic开发者_如何学Go there with a little help from Reflection (I don't know what kind of Widgets the designer will create). I thought that this will be dynamic hence rejoicing but our leader told me that we won't use Reflection.
I heard a very short explanation which did not make any sense for me so my question is:
Why using Reflection considered bad / non-maintainable?
Java is a strongly typed language, with pros and cons. For all the cons, it gives you very good compile-time checking. Bugs/errors found at compile time are the cheapest to find and fix.
Reflection is a runtime thing. You can't have the compiler check anything you do using reflection, therefore if you use reflection you lose a lot of the pros of java. Specifically, you can write code that compiles but explodes at runtime. The term sometimes used for reflective type code is "stringly typed" - you are using the names for methods (Strings) rather than referring to actual methods.
Reflection is used of course, but whenever you use it, remember that you are subverting the strength of java. To use it extensively as in your case might actually be OK, as long as your units tests cover enough cases to give you confidence that your use of it is safe. I would sanction its use if the code that actually used it was a fairly small "utility" base of code.
精彩评论