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.state.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 the driver for the State design pattern example.&lt;p&gt; * * Intent: &lt;i&gt;Allow an object to alter its behavior when its internal state * changes. The object will appear to change its class&lt;/i&gt;&lt;p&gt; * * Participatng objects are &lt;code&gt;Queue&lt;/code&gt; as &lt;i&gt;Context&lt;/i&gt;, and * &lt;code&gt;QueueNormal&lt;/code&gt;, &lt;code&gt;QueueEmpty&lt;/code&gt;, and &lt;code&gt;QueueFull * &lt;/code&gt; as &lt;i&gt;ConcreteState&lt;/i&gt;s. The &lt;i&gt;State&lt;/i&gt; interface is defined in * &lt;code&gt;QueueState&lt;/code&gt;. * &lt;p&gt; * * This example of the State design pattern models a Queue ADT with * a limited capacity that has three different states: * &lt;UL&gt; * &lt;LI&gt;Empty: The queue is empty * &lt;LI&gt;Normal: The queue is neither empty nor full * &lt;LI&gt;Full: The queue is full (# of elements = capacity) * &lt;/UL&gt; * The queue will store Objects to preserve generality. The following * operations are defined on Queue: * &lt;UL&gt; * &lt;LI&gt;&lt;code&gt;insert(Object)&lt;/code&gt; Inserts a new Object into the queue * &lt;LI&gt;&lt;code&gt;getFirst():Object&lt;/code&gt; Returns the first element in the queue * &lt;LI&gt;&lt;code&gt;removeFirst()&lt;/code&gt; Removes the first elelemts from the queue * &lt;/UL&gt; * These are the pattern roles: * &lt;UL&gt; * &lt;LI&gt;Queue: Context * &lt;LI&gt;QueueState: State interface * &lt;LI&gt;QueueEmpty: ConcreteStateA * &lt;LI&gt;QueueNormal: ConcreteStateB * &lt;LI&gt;QueueFull: ConcreteStateC * &lt;/UL&gt; * This implementation uses a concrete aspect to take care of all the state * transistions in the system. States are no longer tangled with each other * as they do not have to know about their successor states. All state * transitions are localized. * * &lt;p&gt;&lt;i&gt;This is the AspectJ version.&lt;/i&gt;&lt;p&gt; * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 06/13/02 * * @see Queue * @see QueueState * @see QueueEmpty * @see QueueNormal * @see QueueFull * @see QueueStateAspect */ public class Main { /** * Implements insertion into a queue. Prints out status messages. * * @param queue the queue to insert into * @param s the string to insert into the queue */ public static void testInsert(Queue queue, String s) { System.out.print(&quot; Trying to insert [&quot;+s+&quot;] into the queue ... &quot;); boolean status = queue.insert(s); if (status == true) { System.out.println(&quot;successful&quot;); } else { System.out.println(&quot;NOT successful, queue full?&quot;); } } /** * Implements deletion from a queue. Prints out status messages. * * @param queue the queue to insert into */ public static void testRemove(Queue queue) { System.out.print(&quot; Trying to remove 1st element of the queue ... &quot;); String item = (String) queue.getFirst(); boolean status = queue.removeFirst(); if (status == true) { System.out.println(&quot;successful: &quot;+item); } else { System.out.println(&quot;NOT successful: &quot;+item); } } /** * Implements the driver for the State design pattern example.&lt;p&gt; * * This example of the State design pattern models a Queue ADT with * a limited capacity that has three different states: * &lt;UL&gt; * &lt;LI&gt;Empty: The queue is empty * &lt;LI&gt;Normal: The queue is neither empty nor full * &lt;LI&gt;Full: The queue is full (# of elements = capacity) * &lt;/UL&gt; * The queue will store Objects to preserve generality. The following * operations are defined on Queue: * &lt;UL&gt; * &lt;LI&gt;&lt;code&gt;insert(Object)&lt;/code&gt; Inserts a new Object into the queue * &lt;LI&gt;&lt;code&gt;getFirst():Object&lt;/code&gt; Returns the first element in the queue * &lt;LI&gt;&lt;code&gt;removeFirst()&lt;/code&gt; Removes the first elelemts from the queue * &lt;/UL&gt; * These are the pattern roles: * &lt;UL&gt; * &lt;LI&gt;Queue: Context * &lt;LI&gt;QueueState: State interface * &lt;LI&gt;QueueEmpty: ConcreteStateA * &lt;LI&gt;QueueNormal: ConcreteStateB * &lt;LI&gt;QueueFull: ConcreteStateC * &lt;/UL&gt; * This implementation uses a concrete aspect to take care of all the state * transistions in the system. States are no longer tangled with each other * as they do not have to know about their successor states. All state * transitions are localized. * * @param args the command line paramters, unused */ public static void main(String[] args) { System.out.println(&quot;Testing Pattern: State - STARTING\n&quot;); Queue queue = new Queue(); testInsert(queue, &quot;This &quot;); testInsert(queue, &quot;is &quot;); testInsert(queue, &quot;a &quot;); testInsert(queue, &quot;test&quot;); System.out.println(); testRemove(queue); testRemove(queue); testRemove(queue); testRemove(queue); System.out.println(&quot;\nTesting Pattern: State - FINISHED&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 Queue.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.state.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 the &lt;i&gt;context&lt;/i&gt; of the queue example. This is effectively * a queue with limited capacity. Requests are forwared to the current state * object. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 06/13/02 * */ public class Queue { /** * the current state of this context */ protected QueueState state = new QueueEmpty(); /** * Tries to insert an object into the queue. Returns true if successful, * false otherwiese. * * @param arg the object to be inserted into the queue * @returns true if insertion was successful, false otherwise. */ public boolean insert(Object arg) { return state.insert(arg); } /** * Returns the first item in the queue * * @returns the first item in the queue */ public Object getFirst() { return state.getFirst(); } /** * Tries to remove an object from the queue. Returns true if successful, * false otherwiese. * * @returns true if deletion was successful, false otherwise. */ public boolean removeFirst() { return state.removeFirst(); } /** * Sets the state of the context to the arguments state. * * @param state the new state for the context object. */ public void setState(QueueState state) { this.state = state; } }</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 QueueEmpty.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.state.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 the &lt;i&gt;ConcreteState&lt;/i&gt; &quot;empty&quot; for the queue example. * Removing items is impossible if the queue is empty. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 06/13/02 * * @see QueueNormal * @see QueueFull */ public class QueueEmpty implements QueueState { /** * Tries to insert an object into the queue. Returns true since it is * always possible to insert items into an empty queue. The appropriate * state transitionsi implemented by the aspect. * * @param arg the object to be inserted into the queue * @returns true. */ public boolean insert(Object arg) { // Inserts a new Object into the queue return true; } /** * Returns the first item in the queue. Returns null since the queue is * empty. * * @returns null. */ public Object getFirst() { // Returns the first element in the queue return null; } /** * Tries to remove an object from the queue. Returns false (queue is * empty). * * @param context the &lt;i&gt;Context&lt;/i&gt; for this design pattern (for update * on demand) * @returns false. */ public boolean removeFirst(){ // Removes the first element from the queue return false; } }</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 QueueFull.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.state.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 the &lt;i&gt;ConcreteState&lt;/i&gt; &quot;full&quot; for the queue example. * Inserting items is impossible if the queue is full. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 06/13/02 * * @see QueueEmpty * @see QueueNormal */ public class QueueFull implements QueueState { /** * stores the items in the queue */ protected Object[] items; /** * stores the index of the first item in the queue. T */ protected int first; /** * Tries to insert an object into the queue. Returns false since the * queue is full. * * @param arg the object to be inserted into the queue * @returns false. */ public boolean insert(Object arg) { return false; } /** * Returns the first item in the queue. * * @returns the first item in the queue. */ public Object getFirst() { return items[first]; } /** * Tries to remove an object from the queue. Returns true if successful, * false otherwiese. The state transition to &quot;normal&quot; is implemented by * the aspect. * * @returns true since it is always possible to delete an item from a * full queue */ public boolean removeFirst(){ return true; } }</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 QueueNormal.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.state.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 the &lt;i&gt;ConcreteState&lt;/i&gt; &quot;normal&quot; for the queue example. * Inserting and deleting items is possible in this state. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 06/13/02 * * @see QueueEmpty * @see QueueFull */ public class QueueNormal implements QueueState { /** * stores the items in the queue */ protected Object[] items = new Object[3]; /** * stores the index of the first item in the queue */ protected int first = 0; /** * stores the index of the last item in the queue */ protected int last = 0; /** * Tries to insert an object into the queue. Returns true if successful, * false otherwiese. Potential state changes to &quot;full&quot; are facilitated * by the aspect. * * @param arg the object to be inserted into the queue * @returns true if insertion was successful, false otherwise. */ public boolean insert(Object arg) { // Inserts a new Object into the queue items[(last)%items.length] = arg; last = (last+1) % items.length; return true; } /** * Returns the first item in the queue. * * @returns null. */ public Object getFirst() { // Returns the first element in the queue return items[first]; } /** * Tries to remove an object from the queue. Returns true if successful, * false otherwiese. Potential state changes to &quot;empty&quot; are facilitated * by the aspect. * * @returns true if deletion was successful, false otherwise. */ public boolean removeFirst(){ // Removes the first element from the queue first = (first + 1) % items.length; return true; } }</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 QueueState.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.state.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): */ /** * Defines the interface for &lt;i&gt;State&lt;/i&gt;s within this queue example. The * operations provided are insert, getFirst, and removeFirst. This verison * does not require passing the context as an argument to these functions. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 06/13/02 */ public interface QueueState { /** * Tries to insert an object into the queue. Returns true if successful, * false otherwiese. * * @param arg the object to be inserted into the queue * @returns true if insertion was successful, false otherwise. */ public boolean insert(Object arg); /** * Returns the first item in the queue * * @returns the first item in the queue */ public Object getFirst(); /** * Tries to remove an object from the queue. Returns true if successful, * false otherwiese. * * @returns true if deletion was successful, false otherwise. */ public boolean removeFirst(); }</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 QueueStateAspect.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.state.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 the state transitions for this state design pattern example. * State transitions are realizied as &lt;code&gt;after&lt;/code&gt; advice. The * joinpoints are the calls from the context to its state object.&lt;p&gt; * * Exisiting states are reused without a employing a flyweight mechanism or * (inflexibly) modularizing the transitions in the context. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 06/13/02 * */ public aspect QueueStateAspect { /** * the queue's &quot;empty&quot; state */ protected QueueEmpty empty = new QueueEmpty(); /** * the queue's &quot;normal&quot; state */ protected QueueNormal normal = new QueueNormal(); /** * the queue's &quot;full&quot; state */ protected QueueFull full = new QueueFull(); /** * Sets the initial state of the queue to empty. * * @param queue the queue context that is initialized. */ after(Queue queue): initialization(new()) &amp;&amp; target(queue) { queue.setState(empty); } /** * Updates the queue context's state after each call from it to the * &lt;code&gt;insert(Object)&lt;/code&gt; method if its current state. * * @param queue the queue context that makes the call. * @param qs the current QueueState that receives the call * @param arg the object to be inserted. */ after(Queue queue, QueueState qs, Object arg): call(boolean QueueState+.insert(Object)) &amp;&amp; target(qs) &amp;&amp; args(arg) &amp;&amp; this(queue) { if (qs == empty) { normal.insert(arg); queue.setState(normal); } else if (qs == normal) { if (normal.first == normal.last) { full.items = normal.items; full.first = normal.first; queue.setState(full); } } } /** * Updates the queue context's state after each call from it to the * &lt;code&gt;removeFirst()&lt;/code&gt; method if its current state. * * @param queue the queue context that makes the call. * @param qs the current QueueState that receives the call */ after(Queue queue, QueueState qs): call(boolean QueueState+.removeFirst()) &amp;&amp; target(qs) &amp;&amp; this(queue) { if (qs == full) { normal.items = full.items; normal.last = full.first; normal.first = (full.first +1) % normal.items.length; queue.setState(normal); } else if (qs == normal) { if (normal.first == normal.last) { queue.setState(empty); } } } }</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