carfield.com.hkMain.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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.<p>
*
* Intent: <i>Allow an object to alter its behavior when its internal state
* changes. The object will appear to change its class</i><p>
*
* Participatng objects are <code>Queue</code> as <i>Context</i>, and
* <code>QueueNormal</code>, <code>QueueEmpty</code>, and <code>QueueFull
* </code> as <i>ConcreteState</i>s. The <i>State</i> interface is defined in
* <code>QueueState</code>.
* <p>
*
* This example of the State design pattern models a Queue ADT with
* a limited capacity that has three different states:
* <UL>
* <LI>Empty: The queue is empty
* <LI>Normal: The queue is neither empty nor full
* <LI>Full: The queue is full (# of elements = capacity)
* </UL>
* The queue will store Objects to preserve generality. The following
* operations are defined on Queue:
* <UL>
* <LI><code>insert(Object)</code> Inserts a new Object into the queue
* <LI><code>getFirst():Object</code> Returns the first element in the queue
* <LI><code>removeFirst()</code> Removes the first elelemts from the queue
* </UL>
* These are the pattern roles:
* <UL>
* <LI>Queue: Context
* <LI>QueueState: State interface
* <LI>QueueEmpty: ConcreteStateA
* <LI>QueueNormal: ConcreteStateB
* <LI>QueueFull: ConcreteStateC
* </UL>
* 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.
*
* <p><i>This is the AspectJ version.</i><p>
*
* @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(" Trying to insert ["+s+"] into the queue ... ");
boolean status = queue.insert(s);
if (status == true) {
System.out.println("successful");
} else {
System.out.println("NOT successful, queue full?");
}
}
/**
* 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(" Trying to remove 1st element of the queue ... ");
String item = (String) queue.getFirst();
boolean status = queue.removeFirst();
if (status == true) {
System.out.println("successful: "+item);
} else {
System.out.println("NOT successful: "+item);
}
}
/**
* Implements the driver for the State design pattern example.<p>
*
* This example of the State design pattern models a Queue ADT with
* a limited capacity that has three different states:
* <UL>
* <LI>Empty: The queue is empty
* <LI>Normal: The queue is neither empty nor full
* <LI>Full: The queue is full (# of elements = capacity)
* </UL>
* The queue will store Objects to preserve generality. The following
* operations are defined on Queue:
* <UL>
* <LI><code>insert(Object)</code> Inserts a new Object into the queue
* <LI><code>getFirst():Object</code> Returns the first element in the queue
* <LI><code>removeFirst()</code> Removes the first elelemts from the queue
* </UL>
* These are the pattern roles:
* <UL>
* <LI>Queue: Context
* <LI>QueueState: State interface
* <LI>QueueEmpty: ConcreteStateA
* <LI>QueueNormal: ConcreteStateB
* <LI>QueueFull: ConcreteStateC
* </UL>
* 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("Testing Pattern: State - STARTING\n");
Queue queue = new Queue();
testInsert(queue, "This ");
testInsert(queue, "is ");
testInsert(queue, "a ");
testInsert(queue, "test");
System.out.println();
testRemove(queue);
testRemove(queue);
testRemove(queue);
testRemove(queue);
System.out.println("\nTesting Pattern: State - FINISHED");
}
}
</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:00ZQueue.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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 <i>context</i> 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:00ZQueueEmpty.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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 <i>ConcreteState</i> "empty" 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 <i>Context</i> 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:00ZQueueFull.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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 <i>ConcreteState</i> "full" 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 "normal" 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:00ZQueueNormal.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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 <i>ConcreteState</i> "normal" 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 "full" 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 "empty" 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:00ZQueueState.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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 <i>State</i>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:00ZQueueStateAspect.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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 <code>after</code> advice. The
* joinpoints are the calls from the context to its state object.<p>
*
* 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 "empty" state
*/
protected QueueEmpty empty = new QueueEmpty();
/**
* the queue's "normal" state
*/
protected QueueNormal normal = new QueueNormal();
/**
* the queue's "full" 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()) && target(queue) {
queue.setState(empty);
}
/**
* Updates the queue context's state after each call from it to the
* <code>insert(Object)</code> 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)) && target(qs) && args(arg) && 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
* <code>removeFirst()</code> 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()) && target(qs) && 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:00Zfiles.lst2004-03-24T16:00:00Z2004-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