Skip to Content.
Sympa Menu

gang-of-4-patterns - Re: [gang-of-4-patterns] Visitor Pattern

gang-of-4-patterns AT lists.cs.illinois.edu

Subject: Design Patterns discussion

List archive

Re: [gang-of-4-patterns] Visitor Pattern


Chronological Thread 
  • From: "Atsushi Tanabe" <ttn13w30a9 AT mx1.ttcn.ne.jp>
  • To: <gang-of-4-patterns AT cs.uiuc.edu>
  • Subject: Re: [gang-of-4-patterns] Visitor Pattern
  • Date: Sun, 20 Aug 2006 03:18:00 +0900
  • List-archive: <http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns>
  • List-id: Design Patterns discussion <gang-of-4-patterns.cs.uiuc.edu>

Hi,

Please try , traverse from a leaf to the root in Visitor.
A sample code is as follows.

Regards

Atsushi Tanabe(Japanese)

--------------------------------------------------
TreeVisitor.java
--------------------------------------------------
public class TreeVisitor {

public void Visit(TreeNode aNode){

if(aNode.isLeaf()){
String path = "";
TreeNode aCurrentNode = aNode;
while(aCurrentNode != null){
path = aCurrentNode.label + path;
aCurrentNode = aCurrentNode.parent;
}
System.out.println(path);
}

}
}

--------------------------------------------------
TreeNode.java
--------------------------------------------------
import java.util.ArrayList;

public class TreeNode {
public String label;
public TreeNode parent;

private ArrayList _children = new ArrayList();
TreeNode(String label){
this.label = label;
}

public TreeNode add(TreeNode aTreeNode){
aTreeNode.parent = this;
_children.add(aTreeNode);
return this;
}
public boolean isLeaf(){
return _children.isEmpty();
}
public void Accept(TreeVisitor v){
v.Visit(this);
for(int i=0;i<_children.size();i++){
((TreeNode)_children.get(i)).Accept(v);
}
}

}

--------------------------------------------------
Main.java
--------------------------------------------------
public class Main {

public static void main(String[] args) {
TreeNode aTree = new TreeNode("a");
aTree.add(new TreeNode("b"));
aTree.add((new TreeNode("c")).add((new TreeNode("d"))));

aTree.Accept(new TreeVisitor());
}

}

----- Original Message -----
From: "Lívio Cipriano"
<lcipriano AT iol.pt>
To: "Roman Neuhauser"
<neuhauser AT sigpipe.cz>
Cc:
<gang-of-4-patterns AT cs.uiuc.edu>
Sent: Saturday, August 19, 2006 7:30 PM
Subject: Re: [gang-of-4-patterns] Visitor Pattern


Hi Roman,

The original Visitor pattern only addresses the "application" of the Visitor
to one Node (in a tree structure). Of course that was only an example, cause
the basic utility of this pattern is to add operations to a set of related
Classes.

My problem is that I've a tree structure, I've Visitores and I need to
traversal the tree applying the Visitores, so I'm looking for an example how
to do it. Some idea?

Regards

Lívio Cipriano

----- Original Message -----
#
lcipriano AT iol.pt
/ 2006-08-19 10:48:13 +0100:
> I would like to find a pattern, maybe using the Visitor one, that can
> be used to traversal a tree.

What is your question?
_______________________________________________
gang-of-4-patterns mailing list
gang-of-4-patterns AT cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/gang-of-4-patterns






Archive powered by MHonArc 2.6.16.

Top of Page