java - Overhead of searching and calling a method with reflection -
I have a class that should read the message, its type should be removed from the envelope and then call the method on which To process is the message;
There are more than 30 message types.
One way to send a message is to use only one switch, but it's really ugly and error -Pron (have I already moved that matter forward? Do I have to make that case twice ?).
Another method is to define an interface with one method process (data) and to create a class for every message, which implements the interface, a map and register those sections, which The code should be processed, just call map.get (messageType). Process (data); But it's really annoying to create a 30+ square.
There is another way to use reflection: Define a function for each message, with the exact signature and pattern name, type the message as process; So map the message type (loaded with a simple search on getDeckaredMethods ()) and then do something like: map.get (messageType) .wink (this, data).
What method should you use? What is the use of reflection of Java?
If you are looking for good performance then there is a big overhead in the image. If your routine (message dispatch) is being used more often in the alternative of polymorphism then it is better than that, besides the IOMO, it is better to look at the OO perspective, as explained on the answer, it is easy to maintain And cheaper.
Your classes handling 30+ messages can easily increase up to 100+ messages, then you will have a class with 100+ methods, add personalized methods and it will be difficult to reuse codes and your The class is just messed up.
Polymorphism and the upper part of the image on the switch is something like this:
Comments
Post a Comment