![]() |
|
|
|||||||
![]() |
Java - Function pointers (Callback functions) in Java ? |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
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 |
|
|
|
|
#2 |
|
Posts: n/a
|
"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 |
|
|
|
#3 |
|
Posts: n/a
|
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 |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
|
|
#5 |
|
Posts: n/a
|
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 |
|
|
|
#6 |
|
Posts: n/a
|
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 |
|
|
|
#7 |
|
Posts: n/a
|
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 |
|
|
|
#8 |
|
Posts: n/a
|
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 |
|
|
|
#9 |
|
Posts: n/a
|
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 |
|
|
|
#10 |
|
Posts: n/a
|
"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 |
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |