|
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.factorymethod.CommandCreator<E,T>
E
- The type of values delivered by created commands (and thus sequences).T
- The type of token used to identify the type of Command
to create.@Participant(value="Creator") public abstract class CommandCreator<E,T>
A command creator creates commands
to
manipulate a given receiver sequence
using the
getCommand(Sequence, Object)
method.
The type of command to create is determined by a token supplied
as the last argument to the getCommand(..)
method. The type of
token is determined by the type parameter T
.
Sub-classes must implement the create(Sequence, Object)
factory method, and can override the createDefault(Sequence)
factory method to change the default behaviour. The NullCommand
is returned as the default command by this implementation, which is
thus a command without a sequence as a receiver.
A command creator is stateless and thread-safe.
Implementation notes:
This class is declared abstract to allow for the factory methods to be
protected. If it had been defined as an interface, the factory methods
would have had to been declared public.
The design requires that type parameter, E
, of the created
commands must be supplied as a type parameter to the creator class and
not via a generic method, which is a viable alternative. This is to allow
sub-classes to utilise the specific type of E
in conjunction
with the type of tokens used, i.e. the tokens may also depend on
E
. Such a dependency is not possible using generic methods
alone. The ReflectiveCommandCreator
class express such a
dependency between E
and T
because T
uses
E
as a generic type parameter.
Constructor Summary | |
---|---|
protected |
CommandCreator()
No-arg constructor. |
Method Summary | |
---|---|
protected abstract Command<E> |
create(Sequence<E> sequence,
T token)
Factory method to create a new command of the
type specified by the token supplied as token . |
protected Command<E> |
createDefault(Sequence<E> sequence)
Factory method to create a new default command in case the normal command creation
failed. |
boolean |
equals(Object object)
Returns true if class of object is equal to
this class, false if not. |
Command<E> |
getCommand(Sequence<E> sequence,
T token)
Returns a new command instance of the type identified by token
associated with the sequence receiver supplied
at sequence . |
int |
hashCode()
Returns the hash code of this command creator. |
String |
toString()
Returns the string representation of this command creator. |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
protected CommandCreator()
Method Detail |
---|
protected abstract Command<E> create(Sequence<E> sequence, T token) throws Exception
command
of the
type specified by the token supplied as token
.
The sequence
set as the receiver is supplied
as sequence
.
sequence
- The sequence; never null.token
- The token identifying the type of command to create;
can be null.
getCommand(Sequence, Object)
.
Exception
- If the creation fails; if so, a default (or null)
command will be returned by getCommand
.createDefault(Sequence)
protected Command<E> createDefault(Sequence<E> sequence) throws Exception
normal
command creation
failed.
sequence
- The sequence; never null.
getCommand(Sequence, Object)
.
This implementation returns a new NullCommand
.
Exception
- If the creation fails; if so, a null command
will be returned by getCommand
.public boolean equals(Object object)
object
is equal to
this class, false if not.
equals
in class Object
object
- The object to test; can be null.
public final Command<E> getCommand(Sequence<E> sequence, T token)
token
associated with the sequence
receiver supplied
at sequence
.
The creation is deferred to the create(Sequence, Object)
factory method, but if the creation fails, new default command type
is tried created with the createDefault(Sequence)
method. If
that fails as well, a NullCommand
is returned.
If token
is null, a null command is returned without
deferring a call to the create methods.
If a token cannot be handled, a default command is returned.
sequence
- The sequence receiver; can be null, in which
case a default (or null) command is returned.token
- The token specifying the type of command; can
be null, in which case a null command is returned.
create(Sequence, Object)
,
createDefault(Sequence)
public int hashCode()
hashCode
in class Object
public String toString()
toString
in class Object
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |