Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java callback interface  RSS feed

 
Alin Andrei
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
Well, I've been struggling myself for some time to understand how this callback example works but couldn't make it!
Here is the code!
The idea behind it is I want my class to be notified when a specific event happened...so I have to implement the specific interface, Interesting Event...what's with all those instance variables of type reference?
Why does EventNotifier class needs to have instance variable InterestingEvent? Actually, I didn't work with composition but more with inheritance and maybe that's why I'm so puzzled! I hope someone of you will make this clearer to me, thanks in advance!

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The interface InterestingEvent has a wrong name, which makes the idea harder to understand.

The interface really defines the handler for the event, and is not itself the event. I would rename it to InterestingEventHandler.

The EventNotifier calls the handler whenever an interesting event happens.


 
Alin Andrei
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is were I got the example from...

http://www.javaworld.com/article/2077462/learn-java/java-tip-10--implement-callback-routines-in-java.html; maybe after reading the whole explanation, you'll find it correct too .
Anyway I found a better example here: http://homepage.cs.uiowa.edu/~slonnegr/wpj/Callbacks.pdf; This is really clear...all I need is have patience when reading ...Anyhow, is pretty difficult to a java beginner to understand that type of code, I guess
 
Alin Andrei
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, how would the main class look like?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alin Andrei wrote:Well, I've been struggling myself for some time to understand how this callback example works but couldn't make it!

I don't know whether this will help, but I think of a callback as ... well ... exactly that.

Suppose I'm waiting for a parcel to arrive in the mail, and I need to know as quickly as possible when it does, what do I do?
I could phone the Parcels Office three or 4 times a day to see if it's arrived, but this takes a lot of effort and might well annoy the poor, overworked (?) postal staff.

Alternatively, I could phone them once, give them my name, address and phone number, and ask them to phone me back when a parcel arrives for me.
Only two phone calls, job done - and it might well be quicker than the first system.

In that scenario the parcel is the Event, the Parcel Office is the Notifier, and I am the Handler (I personally prefer the term 'Listener', because 'Handler' is a bit vague).
  en = new EventNotifier(this) - is my phonecall to the parcel office;
  handler.handleInterestingEvent() - is the parcel office's phonecall back to me; the interface being my name & address, so the Notifier knows what to look for, and 'this' (or 'handler') being my phone number.
And in many cases that last step actually includes the event that caused the callback.

Have another look at the example and see if it makes sense on that basis.

HIH

Winston
 
Piet Souris
Rancher
Posts: 1979
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I find unclear here is: what is the object that actually
informs about the event (which one?).
In Wistons example: how does the post office know that
it has to call whoever should be called, and with what message?

But why not simply use PropertyChangeListeners and the lot?
The post office can simpy indicate what properties they have
on offer (like "package arrived", "letter arrived", et cetera),
and offer a service that allows you to be informed of a change
in that property. Well, that would actually be a bit more involved,
since I would not like my neighbor to be informed when my package
has arrived.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:What I find unclear here is: what is the object that actually informs about the event (which one?).

The Notifier - hence the name.

In Wistons example: how does the post office know that it has to call whoever should be called, and with what message?

Hope I didn't muddy the waters with my analogy, but look at that
  en = new EventNotifier(this);
statement. What does it do?

The Handler (ie, the thing that wants to "know" about the event) creates an EventNotifier and passes its own reference so the Notifier can call it back when it needs to - like me giving the parcel office my phone number. The Notifier knows from the fact that its c'tor takes an InterestingEventHandler that the object it has to call back has a handleInterestingEvent() method, which is what it calls.

Any help?

But why not simply use PropertyChangeListeners and the lot?

Because we're not changing properties. It's a general framework for communication, not purpose-built.

The post office can simpy indicate what properties they have on offer (like "package arrived", "letter arrived", et cetera)...

I hope you're not taking my example too literally. I only chose it to indicate what a callback achieves, not to have it examined too closely.

However, in practise the callback often sends an Event object containing all the information that the Handler needs to know, viz:
  handler.handleInterestingEvent(interestingEvent);

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!