Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - Function pointers (Callback functions) in Java ?

 
Thread Tools Search this Thread
Old 02-27-2005, 09:08 PM   #1
Default Function pointers (Callback functions) in Java ?


Any one knows how I can implement callback functions in Java ( I am
implementing an Observer design pattern and I need to register the
'update' functions of the "Observables".

Easy in C++ - don't know how in Java (other than possibly wrapping up
the function in an object ? - good grief please say it isn't so !)



exquisitus
  Reply With Quote
Old 02-27-2005, 09:27 PM   #2
Boudewijn Dijkstra
 
Posts: n/a
Default Re: Function pointers (Callback functions) in Java ?
"exquisitus" <> schreef in bericht
news:cvtcs7$6c9$...
> Any one knows how I can implement callback functions in Java ( I am
> implementing an Observer design pattern and I need to register the 'update'
> functions of the "Observables".
>
> Easy in C++ - don't know how in Java (other than possibly wrapping up the
> function in an object ? - good grief please say it isn't so !)


If you fear objects, you shouldn't program in Java.
Create an interface ("Observable") that defines the update method.




Boudewijn Dijkstra
  Reply With Quote
Old 02-27-2005, 09:28 PM   #3
HK
 
Posts: n/a
Default Re: Function pointers (Callback functions) in Java ?
exquisitus wrote:
> Any one knows how I can implement callback functions in Java ( I am
> implementing an Observer design pattern and I need to register the
> 'update' functions of the "Observables".
>
> Easy in C++ - don't know how in Java (other than possibly wrapping up


> the function in an object ? - good grief please say it isn't so !)


Yes, it is so, and it is sometimes a bit annoying,
but you get used to it. What most probably want to use
are inline class implementations (dunno the right term).

Lets assume your callbacks are defined in an
interface Oops like this:

public interface Oops {
void update(SomeStuff stuff);
}

Then you may anytime write something like this:

Oops callback = new Oops() {
public update(SomeStuff stuff) {
// perform update
};

This was a quick definition of a class and an object of
this class, right? Now you can pass your callback around.

Harald.



HK
  Reply With Quote
Old 02-27-2005, 09:38 PM   #4
Tilman Bohn
 
Posts: n/a
Default Re: Function pointers (Callback functions) in Java ?
In message <cvtcs7$6c9$>,
exquisitus wrote on Sun, 27 Feb 2005 21:08:23 +0000 (UTC):

> Any one knows how I can implement callback functions in Java ( I am
> implementing an Observer design pattern and I need to register the
> 'update' functions of the "Observables".
>
> Easy in C++ - don't know how in Java (other than possibly wrapping up
> the function in an object ? - good grief please say it isn't so !)


What, you're averse to using objects in an object-oriented language?
This is of course the standard way of handling callbacks in Java. Why do
you think this poses a problem? Before answering that, please look at
anonymous inner classes first. Callbacks are one of the reasons they
exist.

--
Cheers, Tilman

`Boy, life takes a long time to live...' -- Steven Wright


Tilman Bohn
  Reply With Quote
Old 02-27-2005, 10:16 PM   #5
Andrew McDonagh
 
Posts: n/a
Default Re: Function pointers (Callback functions) in Java ?
exquisitus wrote:
> Any one knows how I can implement callback functions in Java ( I am
> implementing an Observer design pattern and I need to register the
> 'update' functions of the "Observables".
>
> Easy in C++ - don't know how in Java (other than possibly wrapping up
> the function in an object ? - good grief please say it isn't so !)
>


Its actually very trivial in Java - much like it is in C++.

However, can you post some example code of how 'you' would implement the
Observer Pattern in C++. I'm curious because of the tone of your question.

As for a Java implementation, there is actually one within the JDK you
can use...

http://java.sun.com/j2se/1.4.2/docs/...bservable.html

... or roll your own like it.


Andrew McDonagh
  Reply With Quote
Old 02-27-2005, 10:24 PM   #6
Andrew McDonagh
 
Posts: n/a
Default Re: Function pointers (Callback functions) in Java ?
exquisitus wrote:
> Any one knows how I can implement callback functions in Java ( I am
> implementing an Observer design pattern and I need to register the
> 'update' functions of the "Observables".
>
> Easy in C++ - don't know how in Java (other than possibly wrapping up
> the function in an object ? - good grief please say it isn't so !)
>


oh, I think I just realised what the tone is about. You are asking for
call back 'functions' not call back objects.

To me 'functions' implies that you don't want to create an object that
registers itself to be called back (on a known Method). You want to
register a C/C++ function which may not be bound to an object.

Is this right?

If so, then you can't do exactly that in Java easily, unless you resort
to using Reflection. But that way would be messy and silly at best -
down right sackable at worst.

To echo the others 'why' question, why the aversion to using an Object?

Curious

Andrew



Andrew McDonagh
  Reply With Quote
Old 02-27-2005, 10:34 PM   #7
Andrew McDonagh
 
Posts: n/a
Default Re: Function pointers (Callback functions) in Java ?
HK wrote:
> exquisitus wrote:
>
>>Any one knows how I can implement callback functions in Java ( I am
>>implementing an Observer design pattern and I need to register the
>>'update' functions of the "Observables".
>>
>>Easy in C++ - don't know how in Java (other than possibly wrapping up

>
>
>>the function in an object ? - good grief please say it isn't so !)

>
>
> Yes, it is so, and it is sometimes a bit annoying,
> but you get used to it.


In my experience, people who don't understand objects, find it annoying
and tend never to fully 'get used to it'

>What most probably want to use
> are inline class implementations (dunno the right term).


Anonymous Inner Class

Which basically means you have defined an unnamed Inner class, because
it is trivial in size and behavior.

However, if the Anonymous inner class is to do something substantial,
then a proper named inner class or even a normal top level class (i.e.
one in its own .Java file) would be more appropriate.


>
> Lets assume your callbacks are defined in an
> interface Oops like this:
>
> public interface Oops {
> void update(SomeStuff stuff);
> }
>
> Then you may anytime write something like this:
>
> Oops callback = new Oops() {
> public update(SomeStuff stuff) {
> // perform update
> };



using the same example but giving the inner class a name would be...

class MyCallbackUsingClass {

public MyCallbackUsingClass() {
Oops callback = new OopsImplemenation();
}


public doSomething() {
callBack.update(someStuff);
}

private Oops callback;


class OopsImplementation implements Oops {

public update(SomeStuff stuff) {
// perform update
}

}
}



Andrew McDonagh
  Reply With Quote
Old 02-27-2005, 10:55 PM   #8
HK
 
Posts: n/a
Default OT: discussion style, WAS: Function pointers (Callback functions) in Java ?
Hi again.

exquisitus wrote:
> Easy in C++ - don't know how in Java (other than possibly wrapping up


> the function in an object ? - good grief please say it isn't so !)


It is sad that your "good grief" triggered
so many replies sounding as if
the writer felt offended.

I would kindly like to ask those people
to be more polite and not get upset just
because someone delivers the slightest
hint that Java might not be perfect.

In the end, having functions as first
class data types is not completely unheard
of, not only in C/C++.

Thanks,
Harald.



HK
  Reply With Quote
Old 02-27-2005, 11:07 PM   #9
Andrew McDonagh
 
Posts: n/a
Default Re: OT: discussion style, WAS: Function pointers (Callback functions)in Java ?
HK wrote:
> Hi again.
>
> exquisitus wrote:
>
>>Easy in C++ - don't know how in Java (other than possibly wrapping up

>
>
>>the function in an object ? - good grief please say it isn't so !)

>
>
> It is sad that your "good grief" triggered
> so many replies sounding as if
> the writer felt offended.
>
> I would kindly like to ask those people
> to be more polite and not get upset just
> because someone delivers the slightest
> hint that Java might not be perfect.
>
> In the end, having functions as first
> class data types is not completely unheard
> of, not only in C/C++.
>
> Thanks,
> Harald.
>


I can't speak for the two others who have replied, but I certainly
wasn't offended by the Ops post. I was curious however, as to his
aversion to using an object rather than a function.

I didn't take his posting to be a swipe at Java's lack of function
pointer. I took it as them wanting to know how to do something they have
done in another language, in Java.

That however, sparked a thought of my own, as to why a function pointer
is desirable in C++, when the same language supports Object call backs.

Its merely a question of OO-ness.


Andrew McDonagh
  Reply With Quote
Old 02-27-2005, 11:20 PM   #10
Brian Hetrick
 
Posts: n/a
Default Re: Function pointers (Callback functions) in Java ?
"exquisitus" <> wrote ...
> Any one knows how I can implement callback functions in Java ( I am
> implementing an Observer design pattern and I need to register the
> 'update' functions of the "Observables".
>
> Easy in C++ - don't know how in Java (other than possibly wrapping
> up the function in an object ? - good grief please say it isn't so
> !)


It is so. Anonymous innner classes are there exactly to provide
callbacks. Welcome to Java.




Brian Hetrick
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Give you enough string functions in Java web reporting tool freezea Software 0 10-08-2009 09:03 AM
Please explain this virtual functions matter (c++) smokey1401 General Help Related Topics 0 07-11-2008 11:53 PM
Help with loading several Java DLL's nonas Software 0 05-10-2007 06:22 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46