277294.ijbsn.asia Directory.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.testcomposite.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 &quot;License&quot;); 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 &quot;AS IS&quot; 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 &lt;i&gt;Composite&lt;/i&gt;. Note that in this AspectJ version, the * participants are decoupled from the pattern. Thus, this composite does * not need to implements an interface or even keep track of its children. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see Component * @see LeafB */ public class Directory { /** * stores an ID for this composite */ private int id = 0; /** * Creates a new CompositeA with a given ID * * @param id the ID for the new object */ public Directory(int id) { this.id = id; } /** * Overwrites the &lt;code&gt;toString()&lt;/code&gt; method from &lt;code&gt;Object&lt;/code&gt; * to print information about this object */ public String toString() { return (&quot;I am a directory (ID &quot;+id+&quot;)&quot;); } } </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:00Z File.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.testcomposite.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 &quot;License&quot;); 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 &quot;AS IS&quot; 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 &lt;i&gt;Leaf&lt;/i&gt;. Note that in this AspectJ version, the * participants are decoupled from the pattern. Thus, this leaf does * not need to implements an interface. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see Component * @see CompositeA */ public class File { /** * stores an ID for this composite */ protected int id = 0; /** * Creates a new CompositeA with a given ID * * @param id the ID for the new object */ public File(int id) { this.id = id; } /** * Overwrites the &lt;code&gt;toString()&lt;/code&gt; method from &lt;code&gt;Object&lt;/code&gt; * to print information about this object */ public String toString() { return (&quot;I am File (size &quot;+id+&quot;)&quot;); } } </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:00Z Leaf.java 2004-03-24T16:00:00Z 2004-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 &quot;License&quot;); 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 &quot;AS IS&quot; 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 &lt;code&gt;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:00Z Main.java 2004-03-24T16:00:00Z 2004-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 &quot;License&quot;); 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 &quot;AS IS&quot; 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.&lt;p&gt; * * Intent: &lt;i&gt;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&lt;/i&gt;&lt;p&gt; * * Participating classes are &lt;code&gt;SumVisitor&lt;/code&gt; and * &lt;code&gt;TraversalVisitor&lt;/code&gt; as &lt;i&gt;ConcreteVisitor&lt;/i&gt;s, implementing the * &lt;code&gt;NodeVisitor&lt;/code&gt; interface. &lt;BR&gt; * &lt;code&gt;RegularNode&lt;/code&gt; and &lt;code&gt;Leaf&lt;/code&gt; are &lt;i&gt;ConcreteElement&lt;/i&gt;s, * implementing the &lt;code&gt;Node&lt;/code&gt; interface. &lt;p&gt; * * 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} * * &lt;p&gt;&lt;i&gt;This is the Java version.&lt;/i&gt;&lt;p&gt; * * Note that &lt;UL&gt; * &lt;LI&gt; Every visitor (even the inteface) has to know of each possible element * type in the object structure. * &lt;LI&gt; Nodes need to know of the visitor interface; they have to implement the * accept(NodeVisitor) method. * &lt;/UL&gt; * * @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.&lt;p&gt; * * 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(&quot;Building the tree (1): leaves&quot;); Leaf one = new Leaf(1); Leaf two = new Leaf(2); Leaf three = new Leaf(3); System.out.println(&quot;Building the tree (1): regular nodes&quot;); RegularNode regN = new RegularNode(one, two); RegularNode root = new RegularNode(regN, three); System.out.println(&quot;The tree now looks like this: &quot;); System.out.println(&quot; regN &quot;); System.out.println(&quot; / \\ &quot;); System.out.println(&quot; regN 3 &quot;); System.out.println(&quot; / \\ &quot;); System.out.println(&quot; 1 2 &quot;); System.out.println(&quot;Visitor 1: SumVisitor, collects the sum of leaf&quot;); System.out.println(&quot;values. Result should be 6.&quot;); SumVisitor sumVisitor = new SumVisitor(); root.accept(sumVisitor); System.out.println(sumVisitor.report()); System.out.println(&quot;Visitor 2: TraversalVisitor, collects a tree&quot;); System.out.println(&quot;representation. Result should be {{1,2},3}.&quot;); 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:00Z MyVisitor.java 2004-03-24T16:00:00Z 2004-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 &quot;License&quot;); 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 &quot;AS IS&quot; 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 &lt;code&gt;VisitorNode&lt;/code&gt; role to &lt;code&gt;Node&lt;/code&gt; */ declare parents: Node implements VisitorNode; /** * Assigns the &lt;code&gt;VRegularNode&lt;/code&gt; role to &lt;code&gt;RegularNode&lt;/code&gt; */ declare parents: RegularNode implements VRegularNode; /** * Assigns the &lt;code&gt;VLeaf&lt;/code&gt; role to &lt;code&gt;Leaf&lt;/code&gt; */ 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:00Z Node.java 2004-03-24T16:00:00Z 2004-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 &quot;License&quot;); 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 &quot;AS IS&quot; 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:00Z NodeVisitor.java 2004-03-24T16:00:00Z 2004-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 &quot;License&quot;); 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 &quot;AS IS&quot; 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:00Z RegularNode.java 2004-03-24T16:00:00Z 2004-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 &quot;License&quot;); 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 &quot;AS IS&quot; 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 &lt;code&gt;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:00Z SampleComposite.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.testcomposite.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 &quot;License&quot;); 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 &quot;AS IS&quot; 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 java.io.PrintStream; import java.util.Enumeration; import ca.ubc.cs.spl.pattern.library.CompositeProtocol; /** * Implements a concrete instance of the composite design pattern.&lt;p&gt; * * It maintains the mapping between composites and their children, defines the * component, composite, and leaf roles, and implements facilities to * implements methods that work on the whole aggregate structure. * * &lt;p&gt;&lt;i&gt;This is the AspectJ version.&lt;/i&gt;&lt;p&gt; * * Each concrete subaspect does the following things: &lt;UL&gt; * &lt;LI&gt; Define which classes are Components and Leafs * &lt;LI&gt; (optional) Define methods that operate on the whole aggregate * structure (using visitors) * &lt;/UL&gt; * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see Component * @see CompositeA * @see LeafB */ public aspect SampleComposite extends CompositeProtocol { /** * Assigns the Composite role to &lt;code&gt;CompositeA&lt;/code&gt; */ declare parents: Directory implements Composite; // Role Pattern: Assigning Roles /** * Assigns the Leaf role to &lt;code&gt;LeafB&lt;/code&gt; */ declare parents: File implements Leaf; // Role Pattern: Assigning Roles // Test1: Attaching an operation with arguments /** * helper variable to store recursion depth for pretty printing */ private static int indent = 0; /** * Print a number of spaces according to the current recursion depth */ private static void indent() { for (int i=0; i&lt;indent; i++) System.out.print(&quot; &quot;); } /* public int sizeOnDisk(Component c) { return c.sizeOnDisk(); } private int Component.sizeOnDisk() { Enumeration e = SampleComposite.aspectOf().recurseFunction(this, new FunctionVisitor() { public Object doFunction(Component c) { return new Integer(c.sizeOnDisk()); } }); int sum =0; while (e.hasMoreElements()) { sum += ((Integer)e.nextElement()).intValue(); } return sum; } private int File.sizeOnDisk() {return id;} */ public int sizeOnDisk(Component c) { // Does not work return c.sizeOnDisk(); } private int Component.sizeOnDisk() {return 0;} private int Directory.sizeOnDisk() { int sum = 0; java.util.Enumeration children; for (children = SampleComposite.aspectOf().getAllChildren(this); children.hasMoreElements(); ) { sum += ((Component)children.nextElement()).sizeOnDisk(); } return sum; } private int File.sizeOnDisk() { return id; } /* public int sizeOnDisk(Component c) { // Works int sum = 0; if (c instanceof File) { sum = ((File)c).id; } else if (c instanceof Directory) { java.util.Enumeration children; for (children = SampleComposite.aspectOf().getAllChildren(c); children.hasMoreElements(); ) { sum += sizeOnDisk((Component)children.nextElement()); } } return sum; } */ /** * Provides a client-accessible method that pretty-prints the * structure of the aggregate structure using a Visitor * * @param s the PrintStram to print to */ public void Component.printStructure(PrintStream s) { indent(); s.println(&quot;&lt;Component&gt;&quot;+this); } /** * Implements &lt;code&gt;printStructure&lt;/code&gt; for Composites: The indent * is appropriately updated and the method call is forwarded to all * children. * * @param s the PrintStram to print to */ public void Composite.printStructure(final PrintStream s) { indent(); s.println(&quot;&lt;Composite&gt;&quot;+this); indent +=4; SampleComposite.aspectOf().recurseOperation(this, new Visitor() { public void doOperation(Component c) { c.printStructure(s); } } ); indent -=4; } /** * Implements &lt;code&gt;printStructure&lt;/code&gt; for Leafs. * * @param s the PrintStram to print to */ public void Leaf.printStructure(PrintStream s) { indent(); s.println(&quot;&lt;Leaf&gt;&quot;+this); } // Test2: Collecting statistics on the structure (aggregation) /** * Provides a client-accessible method that pretty-prints the * structure of the aggregate structure using a FunctionVisitor. * Calculates the sum of all Leaf IDs in the structure * * @returns the sum of leaf ids of all elements in this structure */ public int Component.subSum() { return 0; } /** * Implements &lt;code&gt;subSum()&lt;/code&gt; for Composites: The method call * is forwarded to all children, then the results are summed up. * * @returns the sum of leaf ids of all elements in this structure */ public int Composite.subSum() { // Collects the sum of all Leaves in the structure Enumeration enum = SampleComposite.aspectOf().recurseFunction(this, new FunctionVisitor() { public Object doFunction(Component c) { return new Integer(c.subSum()); } }); int sum = 0; while (enum.hasMoreElements()) { sum += ((Integer) enum.nextElement()).intValue(); System.out.println(&quot;Sum = &quot;+sum); } return sum; } /** * Implements &lt;code&gt;subSum()&lt;/code&gt; for Leafs: Simply returns * the Leaf's ID. * * @returns the leaf id */ public int File.subSum() { return id; } } </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:00Z SumVisitor.java 2004-03-24T16:00:00Z 2004-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 &quot;License&quot;); 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 &quot;AS IS&quot; 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 &quot;&gt;&gt;&gt; SumVisitor collected a sum of &quot;+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:00Z TraversalVisitor.java 2004-03-24T16:00:00Z 2004-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 &quot;License&quot;); 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 &quot;AS IS&quot; 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 = &quot;&quot;; /** * 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 += &quot;{&quot;; rnode.getLeft().accept(this); result += &quot;,&quot;; rnode.getRight().accept(this); result += &quot;}&quot;; } /** * 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 &quot;&gt;&gt;&gt; TraversalVisitor traversed the tree to: &quot;+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:00Z Visitor.java 2004-03-24T16:00:00Z 2004-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:00Z files.lst 2004-03-24T16:00:00Z 2004-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