|
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.SequenceCommand<E>
E
- The type of values returned by this command, i.e.
the type of values delivered by the receiving sequence.@Participant(value="Command") public abstract class SequenceCommand<E>
A sequence command is a command that manipulates
the state of a sequence
when it
is executed
. The sequence in
question is the receiver.
Sequence commands are not thread-safe and
cannot be shared, as they carry information required
for a specific execution. However, sequence commands supply
a copy
method to return a new command of
the same type using the same sequence as is. Any state
related to the execution is not copied.
By default, two sequence commands having the exact same class and using the same sequence are considered equal.
Implementation notes:
A simple version of the Template Method pattern is applied here [Gamma95, p.325]: the
(final) execute()
method is the template method that
utilises the primitive operation doExecute(boolean)
,
which sub-classes must implement. If the sequence in question is a
memorizable
sequence, this command is
able to be undone using the default isUndoable()
and
undo()
implementations as execute()
saves the
before state. Hence, isUndoable()
and undo()
are hook operations that can be overridden by sub-commands
if specific control of the undo mechanism is required, as for example
done in the ResetCommand
class.
Field Summary | |
---|---|
private SequenceMemento<E> |
memento
A sequence memento to restore the previous state if sequence is a memorizable sequence. |
protected E |
result
The result of the execution. |
protected Sequence<E> |
sequence
The sequence receiver used by this command. |
Constructor Summary | |
---|---|
protected |
SequenceCommand(Sequence<E> sequence)
Constructor, which creates this sequence command to use the Sequence supplied as
sequence as the receiver. |
Method Summary | |
---|---|
protected abstract List<Command<E>> |
doExecute(boolean saved)
Performs the actual execution of this command. |
boolean |
equals(Object object)
Returns true if object has the same class
as this sequence command and uses the same
sequence receiver, false if not. |
List<Command<E>> |
execute()
Saves the before state of this sequence command if the receiver sequence is a memorizable
sequence before execution . |
E |
getResult()
Returns the result of the execution of this
command. |
int |
hashCode()
Returns the hash code of this sequence command. |
boolean |
isUndoable()
Returns true if this sequence command has been executed and can be undone by this sequence command, false if not. |
String |
toString()
Returns the string representation of this sequence command. |
E |
undo()
Performs undo of this sequence command if possible. |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Methods inherited from interface dk.rode.thesis.command.Command |
---|
copy |
Field Detail |
---|
private SequenceMemento<E> memento
sequence
is a memorizable
sequence.
Will only be set if execute()
is invoked by
sub-classes before their actual execution.
Can be null.
protected E result
Can be null.
@Participant(value="Receiver") protected final Sequence<E> sequence
Never null.
Constructor Detail |
---|
protected SequenceCommand(@Participant(value="Receiver") Sequence<E> sequence)
Sequence
supplied as
sequence
as the receiver.
sequence
- The sequence; cannot be null.
NullPointerException
- If sequence
is null.Method Detail |
---|
protected abstract List<Command<E>> doExecute(boolean saved) throws CommandException
After successful execution, the result of the
execution can be obtained via the getResult()
method.
saved
- True if the before state was successfully saved before
this method is invoked, false if not.
processor
currently
executing this command. Can be null or empty.
CommandException
- If the execution fails.execute()
public boolean equals(Object object)
object
has the same class
as this sequence command and uses the same
sequence receiver, false if not.
equals
in class Object
object
- The object to test; can be null.
public final List<Command<E>> execute() throws CommandException
memorizable
sequence before execution
.
execute
in interface Command<E>
processor
currently
executing this command. Can be null or empty.
CommandException
- If the execution fails.doExecute(boolean)
public E getResult()
Command
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.
getResult
in interface Command<E>
public int hashCode()
hashCode
in class Object
public boolean isUndoable()
isUndoable
in interface Command<E>
memorizable
sequence that has been executed, false if not.public String toString()
toString
in class Object
public E undo() throws CommandException
If this command is not undoable
an
exception will be thrown. If this command cannot be undone
otherwise, null will be returned.
The command result value is cleared.
undo
in interface Command<E>
CommandException
- In case this command is not undoable.
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |