|
Evaluating Software Design Patterns — the "Gang of Four" patterns implemented in Java 6 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object dk.rode.thesis.command.CommandProcessor
@Participant(value="Invoker") public class CommandProcessor
A command processor can execute
and possibly undo
a given group of
commands.
Implementation notes:
This class is a variant of the POSA Command Processor pattern suggested
by Schmidt et al. [Schmidt00, p.397]
that also handles commands spawned by other commands during
execution
. This enables commands to express
business-like semantics that can alter the behaviour at runtime depending
on data. For example, a given command could based on data decide to spawn a
new type of command to handle the data invariants, which in turn could spawn
other commands, etc. The execution is depth-first, which ensures that changes
performed by previously spawned commands will be available for later commands
in the (original) list of commands to execute. An example of a command that
possibly will spawn other commands is the ReverseCommand
.
The support for spawned commands lessen the need for
composite
commands considerably, if not
all together. In the general case, we feel it is better to let the
command processor handle execution and undo of any command,
including those otherwise stored in composites. This ensures uniform
handling of all commands, consistent log format, more control, etc.
The supplied group of commands acts as part of a local history list, though in conjunction with possibly spawned commands, as described by Gamma et al. [Gamma95, p.238], but processors are stateless otherwise.
Command processors are not generic, but use generic methods to handle any type of command result value.
execute(List)
,
CommandProcessingResult
Constructor Summary | |
---|---|
CommandProcessor()
No-arg constructor. |
Method Summary | ||
---|---|---|
|
execute(Command<E>... commands)
Executes the commands supplied as
commands , in order, or tries to undo executed commands in case of execution failure. |
|
|
execute(List<Command<E>> commands)
Executes the commands supplied as
commands , in order, or tries to undo executed commands in case of execution failure. |
|
private
|
execute(List<Command<E>> commands,
List<Command<E>> executedCommands)
Executes the commands supplied as
commands , in order, or tries to undo executed commands in case of execution failure. |
|
String |
toString()
Returns the string representation of this processor. |
|
private
|
undo(List<Command<E>> executedCommands)
Undoes the executed commands supplied as
executedCommands , if possible. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public CommandProcessor()
Method Detail |
---|
public <E> CommandProcessingResult<E> execute(Command<E>... commands) throws CommandException
Executes
the commands supplied as
commands
, in order, or tries to undo
executed commands in case of execution failure.
If the execution and undo fails, an exception is thrown. Otherwise
a CommandProcessingResult
is returned with the status and
possible result of the execution. The result is that of the last
executed command, which may be a spawned command.
E
- The type of result obtained by executing the commands
in commands
.commands
- The group of commands to execute; cannot be null.
A null command will cause an execution failure with a
following attempt at undoing of previous executed commands.
NullPointerException
- If commands
is null.
CommandException
- If the execution (and undo) fails.execute(List)
public <E> CommandProcessingResult<E> execute(List<Command<E>> commands) throws CommandException
Executes
the commands supplied as
commands
, in order, or tries to undo
executed commands in case of execution failure.
If the execution and undo fails, an exception is thrown. Otherwise
a CommandProcessingResult
is returned with the status and
possible result of the execution. The result is that of the last
executed command, which may be a spawned command.
E
- The type of result obtained by executing the commands
in commands
.commands
- The list (group) of commands to execute. A null
entry will cause an execution failure with a
following attempt at undoing of previous executed
commands.
NullPointerException
- If commands
is null.
CommandException
- If the execution (and undo) fails.execute(Command...)
private <E> boolean execute(List<Command<E>> commands, List<Command<E>> executedCommands) throws CommandException
Executes
the commands supplied as
commands
, in order, or tries to undo
executed commands in case of execution failure.
Each executed command is added to the history list of executed
commands supplied as executedCommands
, which is the
list that will be tried undone
in case
of execution failure.
If the execution and undo fails, an exception is thrown. If the execution fails, but all commands could be undone, this method returns false. Otherwise true to indicate execution success.
E
- The type of result obtained by executing the commands
in commands
.commands
- The list (group) of commands to execute. A null
entry will cause an execution failure with a
following attempt at undoing of previous executed
commands.executedCommands
- The history list of executed commands; never null.
NullPointerException
- If either argument is null.
CommandException
- If the execution and undo fails.public String toString()
toString
in class Object
private <E> boolean undo(List<Command<E>> executedCommands) throws Exception
commands
supplied as
executedCommands
, if possible.
If the undo of a given undoable
command
fails, this method will throw an exception. If a given command is
not undoable, this method returns false. True is returned if all
commands could be undone.
E
- The type of result obtained from the executed
commands.executedCommands
- The executed commands to undo; cannot be null.
NullPointerException
- If executedCommands
is null.
Exception
- If the undo of a command fails.
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |