[2076] in java-interest

home help back first fref pref prev next nref lref last post

Re: Fn ptrs & ... callbacks!?

daemon@ATHENA.MIT.EDU (Andrew Wason)
Fri Sep 22 20:18:11 1995

To: java-interest@java.sun.com
In-Reply-To: Message from Sal Cataudella <salc@ovid.com> 
   of "Fri, 22 Sep 95 14:25:42 -0400."   <Pine.A32.3.91.950922135530.30669A-100000@psyche.ovid.com> 
Date: Fri, 22 Sep 95 17:44:47 -0400
From: Andrew Wason <aw@bae.bellcore.com>

Sal Cataudella <salc@ovid.com>  writes:
> 
> Let's say you need to create 2 sliders, and have each one of them invoke
> a different method. With the method ptr callback technique, you can create
> 2 instances of sliders and simply register the method and object pointers
> you wish to have invoked. Thus , you are reusing code (the slider's)
> by instantion, not by subclassing. Without the method callback technique,
> you are *forced* to subclass *twice*, in order to reuse code.

You could define a new interface, Callbackable that defines a
callback method. Then create a single slider subclass that
manages a Vector of Callbackable instances.  In the sliders
callback method, call the callbackable method of each
Callbackable in the Vector.

So now you can give every slider one or more different
Callbackable objects that implement your callback functionality.
Your callback functionality is independent of your slider class.

If you change all your UI components that use callbacks (Button
etc.)  to manage a Callbackable Vector, then you can even share
Callbackable instances among different UI instances (e.g. I write
a Callbackable that pops up some dialog, I can stick that in a
menu item and a button - when either one is clicked it will
execute the same shared code and pop up a dialog).

I don't see how this is worse than function pointer callbacks, I
prefer it. In C you typically need to register some void* pointer
along with your function pointer to give your function some
context.  With Callbackable, you're encapsulating that data in
the Callbackable object in a typesafe way.


Andrew

_______________________________________________________________________________

Andrew Wason                                       Bell Communications Research
aw@bae.bellcore.com                                Piscataway, NJ
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post