Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: Adding functions to a class... without extending it

Thread Tools

Re: Adding functions to a class... without extending it

Steve Claflin
Posts: n/a
Loic Minier wrote:
> * DemmeGod <(E-Mail Removed)>,
> 15 Aug 2003 22:24:52 -0700:
> > Ok, so I've got this class... It's called "node". Several of the
> > methods in the node class return either node or node arrays (node[]).
> > Node is an extremely general class, and it can be used alot of ways,
> > so I want the ability to add functionality (methods), that are program
> > specific, to it without modifying it. Normally, the (obvious) way to
> > do this would be to extend it. My problem is that I'd want the
> > inherited methods from node not to return node or node array, but to
> > return the subclass, which we'll call superNode, for lack of a better
> > name.

> You can't, you have to cast. But keep in mind that the returned
> reference to node objects are in fact references to your supernode
> object, only the 'metainformation' of the class is returned wrong, so
> you have to check with instanceof and cast.
> I would use an interface to define which objects can be stored in your
> noded structure, and make the objects I want to store implement that
> interface.
> > My first thought was that I could simply create wrapper functions...

> Yes, good idea, just create a wrapper that calls all the functions and
> casts. But it's a painful work, as long as you know what kind of
> objects you are expecting, I don't see the problem of casting after
> each call. So it would be a one shot work, won't be reusable.
> Consider the Vector class, when you store an object into it, you get
> a reference to a java.lang.Object at retrieval time. You have to cast
> it explicitely (and you can check the specific type with instanceof if
> you want to be sure and avoir ClassCastException).
> > My current line of thinking is (vaguely) that node can have some sort
> > of array of method object instances, which it can call. The problem
> > with this is that one would have to run some ugly function like
> > node.runMethod(String name, Object[] args). I'm assuming there's no
> > way of running some sort of "catch-all" method in java that would run
> > if a method name is not found... Of course if that existed then
> > there'd have to be some way of telling the compiler that functions in
> > that class actually DO exist whenever something needs to call a new
> > function.

> You want automatic code generation. For example you could design
> Templates to generate code for a set of functions returning the type
> you choose, ie superNode.
> This is not in the optic of all you see in the JDK functions right now,
> for example HashMap doesn't rely on a specific type being used.

> > This one's kinda got me stumped.... Anybody got any bright ideas?

> Hey it's your code, it depends what you want to make out of it. I'd say
> Java-style suggests you should use a generic type and cast it from your
> application. I recently had to deal with a very similar problem, but
> I had to access classes-specific functions without knowing exactly
> which class would be returned. So in each of my functions, I had 1 to
> 5 instanceof checks depending on the types I could expect to see in
> the context, and then I casted each time and used the class-specific
> functions.
> Good luck,
> --
> Lo´c Minier <(E-Mail Removed)>

From its name, it seems like node is a general class, the purpose of
which is to store an item, not be the item. And, as Lo´c said, the
usual approach is to not predefine the type of object stored. Perhaps
you should think of a structure similar to Model-View-Controller,
although in this case it would be Model-StorageMechanism-Controller.
Your class could have a reference to the object it stores, and a
getStoredObject method that returns that reference. Then you could do
with it what you wish. The difference here is that in MVC the model
implements an interface where you know what the available methods are
(and they are the same for each subtype). In your case you'd still need
the check and cast mechanism.

The benefit of that is if you subclass node to use a different storage
mechanism, the stored object reference capability automatically comes
along with it. I often wish that Sun had added something like a
component model to JComponent, so that all the API subclasses like
JLabel, JTextField, etc., would get the model reference. As it is, I
often find myself adding virtually the same code to extensions of each
of those classes (like an event handler for moving to a new record that
calls setText on the component based on a field from the record -- kind
of like the VB "D" versions of the GUI components).

Reply With Quote
Posts: n/a
Actually, in this case, node is the object. Is has the data, and
various methods to interface with the data. It also has a few methods
which are how it interacts with and accesses other nodes. The
functions I wish to add (usually) are of this type. Extending node to
create other types of data, while it can be done, is not encouraged.
This is a case where a typical hierarchy is perfect. EVERYTHING is a
node first... Various nodes, however, can be used in different ways...
This is dictated by their placement in the database.

At one point I considered making node an interface, and doing
something similar to what you are discussing but ultimately dismissed
the idea because the hierarchy model works so well with it; with this
one exception, of course.

Reply With Quote

Thread Tools

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
extending python with array functions Janwillem Python 4 02-05-2008 07:57 PM
Extending embedded Python: Adding single methods Torsten Bronger Python 3 03-09-2006 07:24 AM
Extending Math Functions Javascript 16 06-29-2005 11:45 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Re: Adding functions to a class... without extending it Roedy Green Java 5 08-17-2003 11:56 PM