carfield.com.hkLeaf.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.visitor.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http:*www.mozilla.org/MPL/ or http:*aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.pattern.
*
* Contributor(s):
*/
/**
* Implements a concrete element of the aggregate strcuture. This is a
* terminal binary tree element (leaf).
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 06/13/02
*
* @see Node
* @see NodeVisitor
*/
public class Leaf implements Node {
/**
* the value stored in this leaf
*/
protected int value;
/**
* Accepts a visitor and calls <code>visitRegularNode(Node) on it.
*
* @param nv the NodeVisitor that is to be accepted.
*/
public void accept(NodeVisitor nv) {
nv.visitLeaf(this);
}
/**
* Creates a new Leaf with the given value.
*
* @param v the value of the leaf
*/
public Leaf(int v) {
value = v;
}
/**
* Accessor for the leaf's value
*
* @returns the leaf's value
*/
public int getValue() {
return value;
}
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZMain.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.visitor.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http:*www.mozilla.org/MPL/ or http:*aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.pattern.
*
* Contributor(s):
*/
/**
* Implements the driver for the Visitor design pattern example.<p>
*
* Intent: <i>Represents an operation to be performed on the elements of an
* object structure. Visitor lets you define a new operation without changing
* the classes of the elements on which it operates</i><p>
*
* Participating classes are <code>SumVisitor</code> and
* <code>TraversalVisitor</code> as <i>ConcreteVisitor</i>s, implementing the
* <code>NodeVisitor</code> interface. <BR>
* <code>RegularNode</code> and <code>Leaf</code> are <i>ConcreteElement</i>s,
* implementing the <code>Node</code> interface. <p>
*
* In this example, Node, RegularNode and Leaf build up a binary tree that has
* int values as leafs. SumVisitor is a NodeVisitor that collects the sum of
* elements in the leafs (should be 6). TraversalVisitor is a visitor that
* collects a description of the tree like {{1,2},3}
*
* <p><i>This is the Java version.</i><p>
*
* Note that <UL>
* <LI> Every visitor (even the inteface) has to know of each possible element
* type in the object structure.
* <LI> Nodes need to know of the visitor interface; they have to implement the
* accept(NodeVisitor) method.
* </UL>
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 06/13/02
*
* @see Node
* @see RegularNode
* @see Leaf
* @see NodeVisitor
* @see SumVisitor
* @see TraversalVisitor
*/
public class Main {
/**
* Implements the driver for the Visitor design pattern example.<p>
*
* In this example, Node, RegularNode and Leaf build up a binary tree that has
* int values as leafs. SumVisitor is a NodeVisitor that collects the sum of
* elements in the leafs (should be 6). TraversalVisitor is a visitor that
* collects a description of the tree like {{1,2},3}
*
* @param args the command-line parameters, unused
*/
public static void main(String[] args) {
System.out.println("Building the tree (1): leaves");
Leaf one = new Leaf(1);
Leaf two = new Leaf(2);
Leaf three = new Leaf(3);
System.out.println("Building the tree (1): regular nodes");
RegularNode regN = new RegularNode(one, two);
RegularNode root = new RegularNode(regN, three);
System.out.println("The tree now looks like this: ");
System.out.println(" regN ");
System.out.println(" / \\ ");
System.out.println(" regN 3 ");
System.out.println(" / \\ ");
System.out.println(" 1 2 ");
System.out.println("Visitor 1: SumVisitor, collects the sum of leaf");
System.out.println("values. Result should be 6.");
SumVisitor sumVisitor = new SumVisitor();
root.accept(sumVisitor);
System.out.println(sumVisitor.report());
System.out.println("Visitor 2: TraversalVisitor, collects a tree");
System.out.println("representation. Result should be {{1,2},3}.");
TraversalVisitor traversalVisitor = new TraversalVisitor();
root.accept(traversalVisitor);
System.out.println(traversalVisitor.report());
}
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZMyVisitor.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.visitor.aspectj;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http:*www.mozilla.org/MPL/ or http:*aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.pattern.
*
* Contributor(s):
*/
import ca.ubc.cs.spl.pattern.library.VisitorProtocol;
/**
* Implements a concrete visitor pattern instance. This aspect assings
* the roles to the participants.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 06/13/02
*/
public aspect MyVisitor extends VisitorProtocol {
/**
* Assigns the <code>VisitorNode</code> role to <code>Node</code>
*/
declare parents: Node implements VisitorNode;
/**
* Assigns the <code>VRegularNode</code> role to <code>RegularNode</code>
*/
declare parents: RegularNode implements VRegularNode;
/**
* Assigns the <code>VLeaf</code> role to <code>Leaf</code>
*/
declare parents: Leaf implements VLeaf;
}
</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZNode.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.visitor.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http:*www.mozilla.org/MPL/ or http:*aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.pattern.
*
* Contributor(s):
*/
/**
* Defines the interface for nodes that can accept visitors.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 06/13/02
*
* @see RegularNode
* @see Leaf
* @see NodeVisitor
*/
public interface Node {
/**
* Accepts a visitor according to GoF.
*
* @param nv the NodeVisitor that is to be accepted.
*/
public void accept(NodeVisitor nv);
}
</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZNodeVisitor.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.visitor.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http:*www.mozilla.org/MPL/ or http:*aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.pattern.
*
* Contributor(s):
*/
/**
* Defines the interface for visitors that operate on binary trees consisting
* of Leafs and RegularNodes.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 06/13/02
*
* @see Node
* @see Leaf
* @see RegularNode
*/
public interface NodeVisitor {
/**
* Visits a RegularNode, which is a non-terminal binary tree node.
*
* @param node the regular node
*/
public void visitRegularNode(Node node);
/**
* Visits a Leaf, which is a terminal tree node.
*
* @param node the leaf
*/
public void visitLeaf(Node node);
/**
* Returns the result of the visitor's operation
*
* @returns a string describing the result of this visitor's operation.
*/
public String report();
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZRegularNode.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.visitor.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http:*www.mozilla.org/MPL/ or http:*aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.pattern.
*
* Contributor(s):
*/
/**
* Implements a concrete element of the aggregate strcuture. This is a
* non-terminal binary tree element.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 06/13/02
*
* @see Node
* @see NodeVisitor
*/
public class RegularNode implements Node {
/**
* the left subtree
*/
protected Node left;
/**
* the right subtree
*/
protected Node right;
/**
* Accepts a visitor and calls <code>visitRegularNode(Node) on it.
*
* @param nv the NodeVisitor that is to be accepted.
*/
public void accept(NodeVisitor nv) {
nv.visitRegularNode(this);
}
/**
* Accessor for the left subtree.
*
* @returns the left subtree.
*/
public Node getLeft() {
return left;
}
/**
* Accessor for the right subtree.
*
* @returns the right subtree.
*/
public Node getRight() {
return right;
}
/**
* Creates a non-terminal node of a binary tree.
*
* @param l the new left subtree.
* @param l the new left subtree.
*/
public RegularNode(Node l, Node r) {
left = l;
right = r;
}
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZSumVisitor.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.visitor.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http:*www.mozilla.org/MPL/ or http:*aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.pattern.
*
* Contributor(s):
*/
/**
* Implements a concrete visitor that collects the sum of all leaf values in
* the tree.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 06/13/02
*
* @see Node
* @see Leaf
* @see RegularNode
*/
public class SumVisitor implements NodeVisitor {
/**
* the colleced sum of leaf values
*/
protected int sum = 0;
/**
* Visits a RegularNode, which is a non-terminal binary tree node.
*
* @param node the regular node
*/
public void visitRegularNode(Node node) {
RegularNode rnode = (RegularNode) node;
rnode.left.accept(this);
rnode.right.accept(this);
}
/**
* Visits a Leaf, which is a terminal tree node.
*
* @param node the leaf
*/
public void visitLeaf(Node node) {
Leaf leaf = (Leaf) node;
sum += leaf.getValue();
}
/**
* Returns the result of the visitor's operation
*
* @returns a string describing the result of this visitor's operation.
*/
public String report() {
return ">>> SumVisitor collected a sum of "+sum;
}
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZTraversalVisitor.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.visitor.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http:*www.mozilla.org/MPL/ or http:*aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.pattern.
*
* Contributor(s):
*/
/**
* Implements a concrete visitor that collects string representation of the
* tree.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 06/13/02
*
* @see Node
* @see Leaf
* @see RegularNode
*/
public class TraversalVisitor implements NodeVisitor {
/**
* contains the accumulated result
*/
protected String result = "";
/**
* Visits a RegularNode, which is a non-terminal binary tree node.
*
* @param node the regular node
*/
public void visitRegularNode(Node node) {
RegularNode rnode = (RegularNode) node;
result += "{";
rnode.getLeft().accept(this);
result += ",";
rnode.getRight().accept(this);
result += "}";
}
/**
* Visits a Leaf, which is a terminal tree node.
*
* @param node the leaf
*/
public void visitLeaf(Node node) {
Leaf leaf = (Leaf) node;
result += leaf.getValue();
}
/**
* Returns the result of the visitor's operation
*
* @returns a string describing the result of this visitor's operation.
*/
public String report() {
return ">>> TraversalVisitor traversed the tree to: "+result;
}
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZVisitor.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">public abstract aspect Visitor
{
// To make sure that all visitors are treated equally and can be
// passed on as arguments, we define an interface for them:
//
// Defining the method signatures here reduces reusability of
// the abstract aspect, but ensures type safety
interface VNode {}
interface VRegularNode extends VNode{}
interface VLeaf extends VNode {}
public interface NodeVisitor
{
public void visitRegularNode(VNode node);
public void visitLeaf(VNode node);
public String report();
}
// The individual accept() code is attached to the appropriate
// classes in the element structure:
public void VNode.accept(NodeVisitor nv)
{}
public void VRegularNode.accept(NodeVisitor nv)
{ nv.visitRegularNode(this); }
public void VLeaf.accept(NodeVisitor nv)
{ nv.visitLeaf(this); }
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00Zfiles.lst2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00Z