[jdom-interest] API Inertia

bob mcwhirter bob at werken.com
Wed May 2 14:40:22 PDT 2001


> Interesting. I'll have to read up on this pattern. Time to crack open 
> Gamma again. One question below:

Also, PLoP books have variations on the GoF visitor.

> Why don't the visit methods return the values requested instead of 
> void? Why the extra level of indirection?

Visitor is intended to implement double-dispatch, which is
the invokation of a method based upon the type of two
objects.

It says nothing about returned values, or anything.

In this case, the two types of objects defining what code
gets executed are:

	XPathVisitor
		and
	Element or Attribute or Document

With a static object model (such as JDOM), a Visitor can be
immensely useful.

The double-dispatch comes into play when you hand the
first object (the visitor) to the 2nd object.  This achieves
the first half of the dispatch, by definig that FooVisitor
is playing a part in the game.

The 2nd half is achieved when the target calls visitMe(this)
on the visitor.  Now, we know we're using a FooVisitor to
visit a Me object, thus, the double-dispatch.

Else, without double-dispatch/visitor, each Visitor must
directly do 'instanceof' checks in order to invoke the
right code for each element type, which isn't very OO,
since it entails cascading if() statements.

> And one final note: this sort of stuff is not well-understood in the 
> community we're targetting. Even simpler creational patterns like 
> Factory Method and Abstract Factory throw many programmers for a 
> loop. This is precisely why JDOM uses concrete classes rather than 
> interfaces like DOM, and the added simplicity has been much 
> appreciated.

The community we're targetting though, wouldn't *have* to use
the Visitor.  And for a little bit of effort on their part,
they *could* use the Visitor much easier than writing lots
of traversal code with bundles of 'instanceof' checks.

> I like taking advantage of design patterns in our implementation and 
> package private API. I'm not so sure I want to require people using 
> the API to be comfortable with patterns though.

javax.swing.* though, heartily displays patterns (MVC, Observer, etc)
in a public interface, and folks seem roughly okay by it.

	-bob




More information about the jdom-interest mailing list