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.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 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;. This version implements an update-on-demand * logic (see GoF, implementation 1, page 308). The logic requires passing * the context on calls to methods defined in the state interface, and an * interface for context objects. The interface is defined in * &lt;code&gt;QueueContext&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 passes the context as an argument to it's method * calls to sate objects, thus allowing for update on demand and removing * the constraint that Queue has to ask the states for the successor state * after each call. * * According to GoF, this implementation is generally more flexible than * having the state stransitions fixed in the context object. Note however * that this approach results in a tight coupling of &lt;i&gt;ConcreteState&lt;/i&gt;s * and &lt;i&gt;Context&lt;/i&gt; objects. * * &lt;p&gt;&lt;i&gt;This is the Java version.&lt;/i&gt;&lt;p&gt; * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 06/13/02 * * @see QueueContext * @see Queue * @see QueueState * @see QueueEmpty * @see QueueNormal * @see QueueFull */ 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 */ private 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 */ private 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 passes the context as an argument to it's method * calls to sate objects, thus allowing for update on demand and removing * the constraint that Queue has to ask the states for the successor state * after each call. * * @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.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 &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 implements QueueContext { /** * 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) { // Inserts a new Object into the queue return state.insert(this, arg); } /** * Returns the first item in the queue * * @returns the first item in the queue */ public Object getFirst() { // Returns the first element in the queue return state.getFirst(this); } /** * 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() { // Removes the first element from the queue return state.removeFirst(this); } /** * 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 QueueContext.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.state.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 &lt;i&gt;Context&lt;/i&gt;s within this queue example. The * operation provided is setState. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 06/13/02 * */ public interface QueueContext { /** * Sets the state of the context to the arguments state. * * @param state the new statefor the context object. */ public void setState(QueueState 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.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 &lt;i&gt;ConcreteState&lt;/i&gt; &quot;empty&quot; for the queue example. * Removing items is impossible if the queue is empty. Inserting items will * trigger a state change to &quot;normal&quot;. * * @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 if successful, * false otherwiese. If insertion is successful, the state of the context * is changed to &quot;normal&quot; (queue is no longer empty). * * @param context the &lt;i&gt;Context&lt;/i&gt; for this design pattern (for update * on demand) * @param arg the object to be inserted into the queue * @returns true if insertion was successful, false otherwise. */ public boolean insert(QueueContext context, Object arg) { // Inserts a new Object into the queue QueueNormal nextState = new QueueNormal(); context.setState(nextState); return nextState.insert(context, arg); } /** * Returns the first item in the queue. Returns null since the queue is * empty. * * @param context the &lt;i&gt;Context&lt;/i&gt; for this design pattern (for update * on demand) * @returns null. */ public Object getFirst(QueueContext context) { // 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(QueueContext context){ // 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.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 &lt;i&gt;ConcreteState&lt;/i&gt; &quot;full&quot; for the queue example. * Inserting items is impossible if the queue is full. Removing items will * trigger a state change to &quot;normal&quot;. * * @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; /** * Creates a new QueueFull state object with the given set of elements * and the given index. * * @param items the content of the full queue * @param first the index of the first item in the queue */ public QueueFull(Object[] items, int first) { this.items = items; this.first = first; } /** * Tries to insert an object into the queue. Returns false since the * queue is full. * * @param context the &lt;i&gt;Context&lt;/i&gt; for this design pattern (for update * on demand) * @param arg the object to be inserted into the queue * @returns false. */ public boolean insert(QueueContext context, Object arg) { return false; } /** * Returns the first item in the queue. * * @param context the &lt;i&gt;Context&lt;/i&gt; for this design pattern (for update * on demand) * @returns the first item in the queue. */ public Object getFirst(QueueContext context) { return items[first]; } /** * Tries to remove an object from the queue. Returns true if successful, * false otherwiese. If successful the state of the context is changed to * &quot;normal&quot;. * * @param context the &lt;i&gt;Context&lt;/i&gt; for this design pattern (for update * on demand) * @returns true if deletion was successful, false otherwise. */ public boolean removeFirst(QueueContext context){ QueueState nextState = new QueueNormal(items, first, first); context.setState(nextState); return nextState.removeFirst(context); } }</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.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 &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; /** * Creates a new QueueNormal state object with the given set of elements * and the given indices for first and last object. * * @param items the content of the full queue * @param first the index of the first item in the queue * @param last the index of the last item in the queue */ public QueueNormal(Object[] items, int first, int last) { this.items = items; this.first = first; this.last = last; } /** * Alternate constructor that uses preset values for object variables. */ public QueueNormal() { } /** * Tries to insert an object into the queue. Returns true if successful, * false otherwiese. If insertion is successful and the queue is full * after the insertion, the state of the context is changed to &quot;full&quot;. * * @param context the &lt;i&gt;Context&lt;/i&gt; for this design pattern (for update * on demand) * @param arg the object to be inserted into the queue * @returns true if insertion was successful, false otherwise. */ public boolean insert(QueueContext context, Object arg) { items[(last)%items.length] = arg; last = (last+1) % items.length; if (first == last) { context.setState(new QueueFull(items, first)); } return true; } /** * Returns the first item in the queue. * * @param context the &lt;i&gt;Context&lt;/i&gt; for this design pattern (for update * on demand) * @returns null. */ public Object getFirst(QueueContext context) { return items[first]; } /** * Tries to remove an object from the queue. Returns true if successful, * false otherwiese. If removal was successful and the removed item was * the last one in the queue, the state of the context is changed to * &quot;empty&quot;. * * @param context the &lt;i&gt;Context&lt;/i&gt; for this design pattern (for update * on demand) * @returns true if deletion was successful, false otherwise. */ public boolean removeFirst(QueueContext context){ first = (first + 1) % items.length; if (first == last) { context.setState(new QueueEmpty()); } 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.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 &lt;i&gt;State&lt;/i&gt;s within this queue example. The * operations provided are insert, getFirst, and removeFirst. * * @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 context the &lt;i&gt;Context&lt;/i&gt; for this design pattern (for update * on demand) * @param arg the object to be inserted into the queue * @returns true if insertion was successful, false otherwise. */ public boolean insert(QueueContext context, Object arg); /** * Returns the first item in the queue * * @param context the &lt;i&gt;Context&lt;/i&gt; for this design pattern (for update * on demand) * @returns the first item in the queue */ public Object getFirst(QueueContext context); /** * Tries to remove an object from the queue. Returns true if successful, * false otherwiese. * * @param context the &lt;i&gt;Context&lt;/i&gt; for this design pattern (for update * on demand) * @returns true if deletion was successful, false otherwise. */ public boolean removeFirst(QueueContext context); }</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