This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
I'm working on an application that does a String to method call mapping. ie. A String parameter("doMethod") is passed into the application and a method( doMethod() ) is called based on that parameter. Currently we do this via reflection and we want to refactor out the reflection. Any suggetsions?
I think what you want to do is a callback function as it is called in C++. I've not do anything like this in Java yet, even I know it well in C++. Reflection is definitely one way to do it. However, I've heard "Core Java" book talked about it. It is not in my hand now. Just give you something to look for. Thanks! Roseanne Join our SCJD Study Group when certified
Here are several of the forces that are pushing us in the direction of refactoring out the reflection. (not in order of importance) * While debugging, the stack trace is practically useless. Methods that we call via reflection all have basically the same stack trace. * Performance issues. (Please correct me if I'm wrong, but reflection is rather cumbersome when it comes to performance, no?) * The piece using reflection is inellegant. Adding new methods to be called via our reflection methods is somewhat cumbersome.
Landon, I agree that reflection is an inelegant, and not very O-O, solution to most problems. You could use a large switch statement to select the method to call, but that's hardly more elegant. Without seeing any of your design, I can only make a vague suggestion. You might use polymorphism by creating several subclasses of a single base. They'd all have the same method but different implementations - you could select which one to instantiate based on the argument, and the code that calls the method could do so through a reference to their base class. If that doesn't help, post back some description of your design and what you're trying to accomplish. Jerry
landon manning
Ranch Hand
Joined: Nov 20, 2000
Posts: 47
posted
0
This piece of the code is used to call 100+ different methods (and this number will grow, consider it potentially infinite). Each of these methods do their own specific task and some are only superficially related. Currently, we store a Integer/String (method name) combo in a hash table. When the doMethod( Integer i ) is called, it takes the Integer, looks up the method name in the hash table and then uses reflection to call that function.
If you really need to pass the method name as a String, the best way to get rid of reflection is probably to use a HashMap that maps the String method name to an (anonymous) inner class that will call the desired method. All these inner classes will implement the same interface (call it MethodInvoker). This solution is much more efficient than reflection and more elegant than a switch, but bulky: you will have to initialise your HashMap with lots of little anonymous classes. Building a little application that automatically generates this initialisation code may be worth your while. If you don't need to pass the method name as a String then you might want to look into the GoF Command pattern. Hope this helps - Peter
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
Peter, I would like to pretend that I have memorized all of the Design Patterns, but sadly this is not true. Also, my copy of the book is at work. So please remind me - GoF???
Just out of curiousity, why is reflection not OO? I have definitely heard reflection has lousy performance but it seems like better OO since it is a way around that dreaded switch.
Originally posted by Jerry Pulley: Landon, I agree that reflection is an inelegant, and not very O-O, solution to most problems. You could use a large switch statement to select the method to call, but that's hardly more elegant. Without seeing any of your design, I can only make a vague suggestion. You might use polymorphism by creating several subclasses of a single base. They'd all have the same method but different implementations - you could select which one to instantiate based on the argument, and the code that calls the method could do so through a reference to their base class. If that doesn't help, post back some description of your design and what you're trying to accomplish. Jerry