[jdom-interest] Design Patterns

Frank Sauer Frank.Sauer at trcinc.com
Wed May 2 13:20:23 PDT 2001


I'm sorry guys! I forgot the main thing...

What I meant to write was:

Alternative implementations of visit can then look like (in Element for
example):

public Object visit(Visitor aVisitor) {
	aVisitor.visitElement(this);
	for (Iterator i = getChildren().iterator();i.hasNext();) {
		Visitable v = (Visitable)i.next();
		v.visit(aVisitor);
	}
}

and Document can implement it as :

public Object visit(Visitor aVisitor) {
	aVisitor.visitDocument(this);
	// do something else to traverse the structure
}

In other words, the Visitable implementations do the traversal, and
the Visitor does whatever... This is where a common interface DOES
do something useful, see the cast in the first method above.


Frank Sauer
The Technical Resource Connection, Inc.
a wholly owned subsidiary of Perot Systems
Tampa, FL
http://www.trcinc.com
-----------------------------------------------
Java: The best argument for Smalltalk since C++


-----Original Message-----
From: Frank Sauer [mailto:Frank.Sauer at trcinc.com]
Sent: Wednesday, May 02, 2001 1:36 PM
To: 'jdom-interest at jdom.org'
Subject: RE: [jdom-interest] Design Patterns


Here's some more thoughts on this:

The Visitable interface could look like:

interface Visitable {
	public Object visit(Visitor aVisitor);
}

and the implementation in most cases will look like:

public Object visit(Visitor aVisitor) {
	return aVisitor.visit(this);
}

Alternatively, the Visitor could have methods like:

	visitElement(Element anElement)
	visitDocument(Document aDoc)

etc.

Alternative implementations of visit can then look like (in Element for
example):

public Object visit(Visitor aVisitor) {
	aVisitor.visitElement(this);
}

and Document can implement it as :

public Object visit(Visitor aVisitor) {
	aVisitor.visitDocument(this);
}

ALL the behavior is in the actual implementation of the
Visitor interface. For example, an OutputVisitor, JTreeBuilderVisitor,
etc. I;ve done some pretty complicated tree stuff using this pattern,
and once you get the hang of it, it's easy. You have to get used to the
one-line implementations of visit and the double dispatching.

Frank Sauer
The Technical Resource Connection, Inc.
a wholly owned subsidiary of Perot Systems
Tampa, FL
http://www.trcinc.com
-----------------------------------------------
Java: The best argument for Smalltalk since C++


Frank Sauer
The Technical Resource Connection, Inc.
a wholly owned subsidiary of Perot Systems
Tampa, FL
http://www.trcinc.com
-----------------------------------------------
Java: The best argument for Smalltalk since C++


-----Original Message-----
From: Frank Sauer [mailto:Frank.Sauer at trcinc.com]
Sent: Wednesday, May 02, 2001 1:15 PM
To: 'jdom-interest at jdom.org'
Subject: RE: [jdom-interest] Design Patterns


I think you're mistaken there. All that the visitor
pattern requires is that all objects being visited
implement some kind of Visitable interface accepting
a Visitor (interface) in some kind of a visit method.
This does not require a common base class and each class
being visited can have completely different implementations
of the visit method. Personally I think a Visitor pattern
could be the answer to the tree walking issues in relation to
this common Node discussion.

Frank Sauer
The Technical Resource Connection, Inc.
a wholly owned subsidiary of Perot Systems
Tampa, FL
http://www.trcinc.com
-----------------------------------------------
Java: The best argument for Smalltalk since C++


-----Original Message-----
From: Trimmer, Todd [mailto:todd.trimmer at trizetto.com]
Sent: Wednesday, May 02, 2001 12:05 PM
To: 'jdom-interest at jdom.org'
Subject: [jdom-interest] Design Patterns


More than one request to add functionality has been responded with "Use a
Decorator for this" or "Use a Visitor for that."

Both of these patterns assume a base class for a hierarchy of objects to be
acted upon. Does anyone realize how much harder these patterns are to
implement without an underlying interface? Wouldn't Node solve this problem?



Todd Trimmer
_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com
_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com
_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com



More information about the jdom-interest mailing list