[jdom-interest] Re: Subclassing Element vs Element Context

Patrick Dowler Patrick.Dowler at nrc.ca
Mon Nov 6 15:31:39 PST 2000


On Sat, 04 Nov 2000, Joseph Bowbeer wrote:
> Adding a userObject can accomplish a lot without forcing the user to
> subclass or to provide an external mapping.

On the other hand, subclassing for the purpose of adding a user object
and associated methods (ie. without overriding existiing behaviour) is
trivial and basically safe from delicate subclassing problems. As such,
you aren't saving very much. Further, any generic 

	protected Object _user_data;

is giving up all manner of type safety and requiring all sorts of casting, so
a sensible developer isn't really going to use it anyway. 

> I've used userObjects in Swing and X.  They're not elegant, but they're
> easy to use and they avoid the fragile subclass problem.

As I mentioned, adding your own data in a subclass doesn't make things
fragile per se. The only thing that can sting you is if a later version of
the base class suddenly includes a member/method of the same name.
A naming convention in the base class solves this, or put your (or company) 
initials in there.


class MyElement extends Element
{
	private MyType myData; // no one will use this - it is non-descript

	public void doit();             // likewise :-)
}

This is pretyt safe from the so-called "fragile subclass problem" - in practice.

-- 

Patrick Dowler
Canadian Astronomy Data Centre




More information about the jdom-interest mailing list