Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

dk.rode.thesis.command
Interface Command<E>

Type Parameters:
E - The type of result obtained by executing this command.
All Superinterfaces:
Copyable<Command<E>>, StrictCopyable<Command<E>>
All Known Implementing Classes:
CompositeCommand, EvilCommand, LogCommand, NextCommand, NullCommand, ResetCommand, ReverseCommand, SequenceCommand

@Participant(value="Command")
public interface Command<E>
extends StrictCopyable<Command<E>>

A command represents a possibly undoable operation on a given receiver.

The result of the execution is called the command value and can be acquired using the getResult() method after execution. Commands should always be executed by a CommandProcessor to ensure proper undo functionality.

When executed, commands may spawn new commands to be executed immediately, i.e. before other commands in the current list of commands being executed by the command processor used. The execution of spawned commands is thus depth-first.

Commands are not thread-safe, and generally cannot be shared. Instead, they can be copied for "reuse".

Author:
Gunni Rode / rode.dk
See Also:
CommandProcessor, CommandProcessingResult

Method Summary
 Command<E> copy()
          Creates a copy of this command, excluding internal state information required for execution and undo.
 List<Command<E>> execute()
          Performs the execution of this command.
 E getResult()
          Returns the result of the execution of this command.
 boolean isUndoable()
          Returns true if this command has been executed and is undoable, false if not.
 E undo()
          Perform undo of this command, if possible.
 

Method Detail

copy

Command<E> copy()
Creates a copy of this command, excluding internal state information required for execution and undo.

Specified by:
copy in interface Copyable<Command<E>>
Returns:
A new instance of this command; never null.

execute

List<Command<E>> execute()
                         throws CommandException
Performs the execution of this command.

After successful execution, the result of the execution can be obtained via the getResult() method.

Returns:
A list of spawned commands, if any, to be executed by the command processor currently executing this command. Can be null or empty.
Throws:
CommandException - If the execution fails.
See Also:
undo()

getResult

E getResult()
Returns the result of the execution of this command.

Invoking this method before execution will cause an illegal state exception to be thrown. If undone, this command must be re-executed before the result can be fetched again.

Returns:
The result of the execution; can be null.

isUndoable

boolean isUndoable()
Returns true if this command has been executed and is undoable, false if not.

If undoable, the effect of the execution can be undone by invoking undo().

Returns:
True if undoable, false if not.

undo

E undo()
       throws CommandException
Perform undo of this command, if possible.

If this method is invoked and this command is not undoable, an exception will be thrown.

The result of invoking undo on a command that has not been executed is undefined.

Returns:
The result of the undo operation; may be null.
Throws:
CommandException - If the undo fails.

Gunni Rode / rode.dk

Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!

The original thesis and source code are available at rode.dk/thesis.