A {@link Command} encapsulates a unit of processing work to beperformed, whose purpose is to examine and/or modify the state of a transaction that is represented by a {@link Context}. Individual {@link Command}s can be assembled into a {@link Chain}, which allows them to either complete the required processing or delegate further processing to the next {@link Command} in the {@link Chain}.
{@link Command} implementations should be designed in a thread-safemanner, suitable for inclusion in multiple {@link Chain}s that might be processed by different threads simultaneously. In general, this implies that {@link Command} classes should not maintain state information ininstance variables. Instead, state information should be maintained via suitable modifications to the attributes of the {@link Context} that ispassed to the execute()
command.
{@link Command} implementations typically retrieve and store stateinformation in the {@link Context} instance that is passed as a parameterto the execute()
method, using particular keys into the Map
that can be acquired via Context.getAttributes()
. To improve interoperability of {@link Command} implementations, a useful design pattern is to expose thekey values used as JavaBeans properties of the {@link Command}implementation class itself. For example, a {@link Command} that requiresan input and an output key might implement the following properties:
private String inputKey = "input"; public String getInputKey() { return (this.inputKey); } public void setInputKey(String inputKey) { this.inputKey = inputKey; } private String outputKey = "output"; public String getOutputKey() { return (this.outputKey); } public void setOutputKey(String outputKey) { this.outputKey = outputKey; }
And the operation of accessing the "input" information in the context would be executed by calling:
String input = (String) context.get(getInputKey());
instead of hard coding the attribute name. The use of the "Key" suffix on such property names is a useful convention to identify properties being used in this fashion, as opposed to JavaBeans properties that simply configure the internal operation of this {@link Command}.
@author Craig R. McClanahan
@version $Revision: 1.5 $ $Date: 2004/02/25 00:01:07 $