carfield.com.hk Main.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.iterator.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.patterns. * * Contributor(s): */ import java.util.Iterator; /** * Implements the driver for the iterator design pattern example.&lt;p&gt; * * Intent: &lt;i&gt;Provide a way to access the elements of an aggregate object * sequentially without exposing its underlying representation.&lt;/i&gt;&lt;p&gt; * * Participating objects are &lt;code&gt;SimpleList&lt;/code&gt; as &lt;i&gt;Aggregate&lt;/i&gt;, * &lt;code&gt;OpenList&lt;/code&gt; as &lt;i&gt;ConcreteAggregate&lt;/i&gt;, &lt;code&gt; * java.util.Iterator&lt;/code&gt; as &lt;i&gt;Iterator&lt;/i&gt;, and &lt;code&gt;ReverseIterator * &lt;/code&gt; as &lt;i&gt;ConcreteIterator&lt;/i&gt;. * * In this example, the concrete aggregate is a list that gets filled with * five integer objects (1 to 5). The, the &lt;code&gt;ReverseIterator&lt;/code&gt; is * created and used to print all elements in reverse order. * * &lt;p&gt;&lt;i&gt;This is the Java version.&lt;/i&gt;&lt;p&gt; * * Aggregate and Iterator need to know about the pattern. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 07/13/02 * * @see SimpleList * @see OpenList * @see java.util.Iterator * @see ReverseIterator */ public class Main { /** * Prints all elements in the iterator to &lt;code&gt;System.out&lt;/code&gt;. * * @param iter the iterator whose elements should be printed */ private static void print(Iterator iter) { while(iter.hasNext()) { System.out.println(iter.next()); } } /** * Implements the driver for the iterator design pattern example.&lt;p&gt; * * In this example, the concrete aggregate is a list that gets filled with * five integer objects (1 to 5). The, the &lt;code&gt;ReverseIterator&lt;/code&gt; is * created and used to print all elements in reverse order. * * @param args command line paramters, unused */ public static void main(String[] args) { OpenList openList = new OpenList(); openList.append(new Integer(1)); openList.append(new Integer(2)); openList.append(new Integer(3)); openList.append(new Integer(4)); openList.append(new Integer(5)); System.out.println(&quot;List created, containing int objects 1, 2, 3, 4, 5.&quot;); Iterator iter = openList.createReverseIterator(); System.out.println(&quot;Using ReverseIterator to print list elements in reverse order...&quot;); print(iter); System.out.println(&quot;done.&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 OpenList.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.iterator.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.patterns. * * Contributor(s): */ import java.util.Iterator; /** * Implements a basic open list. This implementation is based on * &lt;code&gt;java.util.LinkedList&lt;/code&gt;. In essence, this class acts as an * adapter for the Java class. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 07/13/02 * * @see SimpleList */ public class OpenList implements SimpleList { java.util.LinkedList list = new java.util.LinkedList(); /** * Returns the number of elements in the list * * @returns the number of elements in the list */ public int count() { return list.size(); } /** * Appends an object to the list * * @param o the object to append * @returns true if successful, false otherwise */ public boolean append(Object o) { list.addLast(o); return true; } /** * Removes an object from the list * * @param o the object to remove * @returns true if successful, false otherwise */ public boolean remove(Object o) { return list.remove(o); } /** * Returns an object from the list * * @param index the position of the object * @returns the object at position index */ public Object get(int index) { return list.get(index); } /** * Returns a reverse iterator for this list. * * @returns the a reverse iterator for this list */ public Iterator createReverseIterator() { return new ReverseIterator(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 OpenListIteration.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.iterator.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.patterns. * * Contributor(s): */ import java.util.NoSuchElementException; import java.util.Iterator; /** * Implements a concrete instance of the Iterator design pattern. This * concrete aspect attaches a factory method to the &lt;i&gt;Aggregate&lt;/i&gt; class * and provides an implementation for the Iterator via an inner class. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 07/13/02 * * @see SimpleList * @see OpenList */ public aspect OpenListIteration { /** * Attaches the factory method to create the reverse iterator to the * OpenList class using the open classes mechanism. */ Iterator OpenList.createReverseIterator() { return new ReverseIterator(this); } /** * Another possibility of where to place the factory method. If we * have the factory method on the aspect, multiple instances of the * same pattern will not be confused an can use the same aggregate * (multiple factory methods won't conflict that way, even if they * have the same signature). This is the preferrable implementation. */ Iterator createIteratorFor(OpenList list) { return new ReverseIterator(list); } /** * Provides the implementation of the reverse iterator. Instead * of defining an inner class, one could also instantiate * &lt;code&gt;java.util.Iterator&lt;/code&gt; as an anonymous class and overwrite * the appropriate methods. */ private static class ReverseIterator implements Iterator { /** * the positition of the current element */ protected int current; /** * the list this iterator operates on */ protected SimpleList list; /** * Returns true if the iteration has more elements. * * @returns true if the iteration has more elements */ public boolean hasNext() { return (current &gt; 0); } /** * This opional method is not implemented for this iterator. */ public void remove() { throw new UnsupportedOperationException(&quot;remove() not supported&quot;); } /** * Returns the next element in the iteration. * * @returns the next element in the iteration. */ public Object next() { if (!hasNext()) { throw new ArrayIndexOutOfBoundsException(&quot;Iterator out of Bounds&quot;); } else { return list.get(--current); } } /** * Creates a new ReverseIterator from the given list. * * @param list the list to generate an iterator from */ public ReverseIterator(SimpleList list) { super(); this.list = list; current = list.count(); } } } </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 ReverseIterator.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.iterator.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.patterns. * * Contributor(s): */ import java.util.Iterator; /** * Implements a reverse iterator. This means that it will return elements in * reverse order. We chose not to define our own &lt;i&gt;Iterator&lt;/i&gt;, but to use * Java's &lt;code&gt;java.util.Iterator&lt;/code&gt; interface. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 07/13/02 * */ public class ReverseIterator implements Iterator { /** * the positition of the current element */ protected int current; /** * the list this iterator operates on */ protected SimpleList list; /** * Returns true if the iteration has more elements. * * @returns true if the iteration has more elements */ public boolean hasNext() { return (current &gt; 0); } /** * This opional method is not implemented for this iterator. */ public void remove() { throw new UnsupportedOperationException(&quot;remove() not supported&quot;); } /** * Returns the next element in the iteration. * * @returns the next element in the iteration. */ public Object next() { if (!hasNext()) { throw new ArrayIndexOutOfBoundsException(&quot;Iterator out of Bounds&quot;); } else { return list.get(--current); } } /** * Creates a new ReverseIterator from the given list. * * @param list the list to generate an iterator from */ public ReverseIterator(SimpleList list) { super(); this.list = list; current = list.count(); } }</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 SimpleList.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.iterator.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.patterns. * * Contributor(s): */ /** * Defines an interface for a basic list. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 07/13/02 * * @see OpenList */ public interface SimpleList { /** * Returns the number of elements in the list * * @returns the number of elements in the list */ public int count(); /** * Appends an object to the list * * @param o the object to append * @returns true if successful, false otherwise */ public boolean append(Object o); /** * Removes an object from the list * * @param o the object to remove * @returns true if successful, false otherwise */ public boolean remove(Object o); /** * Returns an object from the list * * @param index the position of the object * @returns the object at position index */ public Object get(int index); } </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