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

dk.rode.thesis.factorymethod
Class CommandCreator<E,T>

java.lang.Object
  extended by dk.rode.thesis.factorymethod.CommandCreator<E,T>
Type Parameters:
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.
Direct Known Subclasses:
ReflectiveCommandCreator, SequenceCommandCreator

@Participant(value="Creator")
public abstract class CommandCreator<E,T>
extends Object

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.

Author:
Gunni Rode / rode.dk

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

CommandCreator

protected CommandCreator()
No-arg constructor.

Method Detail

create

protected abstract Command<E> create(Sequence<E> sequence,
                                     T token)
                              throws Exception
Factory method to create a new command of the type specified by the token supplied as token.

The sequence set as the receiver is supplied as sequence.

Parameters:
sequence - The sequence; never null.
token - The token identifying the type of command to create; can be null.
Returns:
The corresponding command, or null if no such command could be created. If null is returned, a default (on null) command is returned by getCommand(Sequence, Object).
Throws:
Exception - If the creation fails; if so, a default (or null) command will be returned by getCommand.
See Also:
createDefault(Sequence)

createDefault

protected Command<E> createDefault(Sequence<E> sequence)
                            throws Exception
Factory method to create a new default command in case the normal command creation failed.

Parameters:
sequence - The sequence; never null.
Returns:
A new default command; if null is returned, a null command is returned by getCommand(Sequence, Object). This implementation returns a new NullCommand.
Throws:
Exception - If the creation fails; if so, a null command will be returned by getCommand.

equals

public boolean equals(Object object)
Returns true if class of object is equal to this class, false if not.

Overrides:
equals in class Object
Parameters:
object - The object to test; can be null.
Returns:
True if equal, false if not.

getCommand

public final 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.

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.

Parameters:
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.
Returns:
A new command; never null.
See Also:
create(Sequence, Object), createDefault(Sequence)

hashCode

public int hashCode()
Returns the hash code of this command creator.

Overrides:
hashCode in class Object
Returns:
The hash code.

toString

public String toString()
Returns the string representation of this command creator.

Overrides:
toString in class Object
Returns:
The string representation; never null.

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.