carfield.com.hk CompositeA.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.composite.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 CompositeA { /** * 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 CompositeA(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 (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 LeafB.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.composite.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 LeafB { /** * 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 LeafB(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 B (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 Main.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.composite.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.util.Enumeration; /** * Implements the driver for the composite design pattern example.&lt;p&gt; * * Intent: &lt;i&gt;Compose objects into tree structures torepresent part-whole * hierarchies. Composite lets clients treat individual objects and * compositions of objects uniformly.&lt;/i&gt;&lt;p&gt; * * Participatng classes are &lt;code&gt;CompositeA&lt;/code&gt;s as &lt;i&gt;Composite&lt;/i&gt;s, * and a &lt;code&gt;LeafB&lt;/code&gt; as &lt;i&gt;Leaf&lt;/i&gt;. Both implement the * &lt;i&gt;Component&lt;/i&gt; interface.&lt;p&gt; * * This example creates a simple structure as follows: Composite c1 has * three children: l1, c2, and l3. c2 has l2 as a child. * Compact notation: c1(l1, c2(l2), l3) * * &lt;p&gt;&lt;i&gt;This is the AspectJ version.&lt;/i&gt;&lt;p&gt; * * Composites and Leafs do not need to know about their role in the pattern. * This example also illustrates how to define methods that collect * information from the whole aggreagate structure (using visitors). * One of them prints the compiste structure, the other one collects the * sum of the values of all leaves in the structure. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see Component * @see CompositeA * @see LeafB */ public class Main { /** * helper variable to store recursion depth for pretty printing */ 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;); } /** * Pretty-prints a recursive composite structure * * @param comp the component denoting the entry point into the structure */ private static void printStructure(SampleComposite.Component comp) { indent(); System.out.println(&quot;Showing: &quot;+comp); indent +=4; for (Enumeration enum = SampleComposite.aspectOf().getAllChildren(comp); enum.hasMoreElements();) { printStructure((SampleComposite.Component) enum.nextElement()); } indent -= 4; } /** * This example creates a simple structure as follows: Composite c1 has * three children: l1, c2, and l3. c2 has l2 as a child. * Compact notation: c1(l1, c2(l2), l3) &lt;p&gt; * * Also, this example illustrates how to define methods that collect * information from the whole aggreagate structure (using visitors). * One of them prints the compiste structure, the other one collects the * sum of the values of all leaves in the structure. */ public static void main(String[] args) { System.out.println(&quot;\n&lt;&lt;&lt; Testing implementation AOP3 of Composite pattern &gt;&gt;&gt;\n&quot;); System.out.print (&quot;Creating Composite structure ...&quot;); CompositeA composite1 = new CompositeA(1); CompositeA composite2 = new CompositeA(2); LeafB leaf1 = new LeafB(1); LeafB leaf2 = new LeafB(2); LeafB leaf3 = new LeafB(3); SampleComposite.aspectOf().addChild(composite1, leaf1); SampleComposite.aspectOf().addChild(composite1, composite2); SampleComposite.aspectOf().addChild(composite2, leaf2); SampleComposite.aspectOf().addChild(composite1, leaf3); System.out.println(&quot;done.&quot;); System.out.println(&quot;This is the Structure:&quot;); printStructure(composite1); System.out.println(&quot;\nCalling printStructure(PrintStream) on Composition.Components ...&quot;); composite1.printStructure(System.out); System.out.println(&quot;... done.&quot;); System.out.println(&quot;\nCalling subSum():int on the structure ...&quot;); System.out.println(&quot;The leaf id sum is: &quot;+composite1.subSum()); System.out.println(&quot;... done.&quot;); System.out.println(&quot;\n&lt;&lt;&lt; Test completed &gt;&gt;&gt;\n&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 SampleComposite.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.composite.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.1, 11/25/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: CompositeA implements Composite; // Role Pattern: Assigning Roles /** * Assigns the Leaf role to &lt;code&gt;LeafB&lt;/code&gt; */ declare parents: LeafB 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;); } /** * 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(); } return sum; } /** * Implements &lt;code&gt;subSum()&lt;/code&gt; for Leafs: Simply returns * the Leaf's ID. * * @returns the leaf id */ public int LeafB.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 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