Skip to main content
gamedev.net
🔒 Locked

[java] java callback design question

Started by skoco Jan 29, 2006 at 5:18 AM 4 replies 1k views
Original Post
skoco
skoco
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
haegarr
haegarr
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...
CaptainJester
CaptainJester
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.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
Raghar
Raghar
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.

skoco
skoco
Thank you guys, I think I have got some idea now.

skoco

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.