Jump to content
  • Advertisement
Sign in to follow this  
skoco

[java] java callback design question

This topic is 4644 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello java wizards ;) I have a design question... As far as I know, hava does not have something like Function type, or equivalent of C++ pointer to (member) functions, which is kind of... ehmm pity. Well, what I need to do, is to have a java client receive some numbers through socket, let's say something like {2, 15, 4, ...}, where 2 indicate a class type (or interface), 15 means id of some conctrete object of that type, and 4 means some member function of that type or interface, ... are prameters to that member function. So that I can invoke a member function for some instance of class remotely, and the fastest way possible. Using Linj (Lisp In Java) I can have Linj translate this : (funcall (lambda (x y) (set-location x y)) 10 20) into something like this : new Procedure2() { public void funcall(Object x, Object y) { setLocation(((Number)x).doubleValue(), ((Number)y).doubleValue());}}. funcall(Bignum.valueOf(10), Bignum.valueOf(20)); which looks quite arcane, even without retyping to some specific class. Is there a better way to do it? How can I avoid hardwiring of retyping to specific class or interface in source? Thanks for some ideas !! skoco

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by skoco
As far as I know, hava does not have something like Function type, or equivalent of C++ pointer to (member) functions

But Java has java.lang.reflect.Method:
Quote:
From JavaDoc
A Method provides information about, and access to, a single method on a class or interface. The reflected method may be a class method or an instance method (including an abstract method).
A Method permits widening conversions to occur when matching the actual parameters to invoke with the underlying method's formal parameters, but it throws an IllegalArgumentException if a narrowing conversion would occur.

Maybe it is what you are searching for. However, you have to map the function number to a method name...

Share this post


Link to post
Share on other sites
Instead of using callback function pointers, Java uses event listener methodology instead. Look up how KetListener works and that should give you an idea how it works.

Share this post


Link to post
Share on other sites
Method 1. Remote method invocation.
Method 2. Switch statement.



BTW let's imagine, a program knows a location with exectutable code ... program would store it somewhere ... then the executable code would be relocated.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!