[2088] in java-interest

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

Re: Fn ptrs & ... callbacks!?

daemon@ATHENA.MIT.EDU (Gary Aitken)
Sun Sep 24 04:19:48 1995

Date: Sat, 23 Sep 1995 23:23:01 -0700
From: garya@village.org (Gary Aitken)
To: Andrew Wason <aw@bae.bellcore.com>, java-interest@java.sun.com
In-Reply-To: <9509222144.AA29569@jello>

>> 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.

This is all true.  However, it would be far better if the callblackable vector 
were part of the base classes.  If you think of code reuse and general systems 
which want to be able to deal with arbitrary objects, the last thing you want 
to have to do is derive a new class for every darn class someone writes.

Gary Aitken		garya@village.org
-
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