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.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 "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>. 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
* <code>QueueContext</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 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 <i>ConcreteState</i>s
* and <i>Context</i> objects.
*
* <p><i>This is the Java version.</i><p>
*
* @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(" 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
*/
private 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 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("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.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 "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 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:00ZQueueContext.java2004-03-24T16:00:00Z2004-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 "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>Context</i>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:00ZQueueEmpty.java2004-03-24T16:00:00Z2004-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 "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. Inserting items will
* trigger a state change to "normal".
*
* @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 "normal" (queue is no longer empty).
*
* @param context the <i>Context</i> 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 <i>Context</i> 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 <i>Context</i> 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:00ZQueueFull.java2004-03-24T16:00:00Z2004-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 "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. Removing items will
* trigger a state change to "normal".
*
* @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 <i>Context</i> 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 <i>Context</i> 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
* "normal".
*
* @param context the <i>Context</i> 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:00ZQueueNormal.java2004-03-24T16:00:00Z2004-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 "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;
/**
* 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 "full".
*
* @param context the <i>Context</i> 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 <i>Context</i> 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
* "empty".
*
* @param context the <i>Context</i> 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:00ZQueueState.java2004-03-24T16:00:00Z2004-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 "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.
*
* @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 <i>Context</i> 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 <i>Context</i> 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 <i>Context</i> 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: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