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

Class ReflectiveCommandCreator<E>

  extended by dk.rode.thesis.factorymethod.CommandCreator<E,TypeLiteral<? extends Command<E>>>
      extended by dk.rode.thesis.factorymethod.ReflectiveCommandCreator<E>
Type Parameters:
E - The type of values delivered by created commands (and thus sequences).

public class ReflectiveCommandCreator<E>
extends CommandCreator<E,TypeLiteral<? extends Command<E>>>

A reflective command creator creates new commands based on type literals as tokens.

A type literal represents a fully parameterised command type, for example {@link TypeLiteral}<{@link NextCommand}<Integer>>. The creator can create any command, parameterised or not, declaring a no-arg constructor (no receiver) and/or constructor with a single parameter of type Sequence (a receiver in form of a sequence).

Implementation notes:
The creation is completely type safe with no casting required even though this creator creates commands with type parameters. This is because the TypeLiteral class ensures that the type parameters are accessible at runtime. This would not have been possible by merely using Class literals as tokens, as the type parameter information cannot be expressed using class literals. However, this is only feasible because we operate on concrete type parameters, e.g. NextCommand<Integer>, and not on erased generic types like NextCommand<E>.

Furthermore, the factory method in this creator upgrades the type literal supplied as the token to an instantiable type literal, if not already, which may throw a runtime exception in case the generic type represented by the type literal is an interface or abstract class, which naturally cannot be instantiated. This is no different than if a class literal was used, though. As the bounds on the type literals used by this creator is <? extends Command<E>> this will only be the case here if type literals are created as {@link TypeLiteral}<{@link Command}<Integer>>, i.e. using the Command interface instead of a concrete command implementation. An instantiable type literal can create a generic type based on a given constructor, here the no-arg or the constructor accepting a single Sequence argument as described above. This is utilised in the factory method in this creator.

Using the Prototype pattern as a means to create different types of commands is an, perhaps safer, alternative to creating commands using reflection. The Abstract Factory illustrates the use of prototypes in the creation process.

Gunni Rode / rode.dk
See Also:
TypeLiteral, InstantiableTypeLiteral

Constructor Summary
          No-arg constructor.
Method Summary
protected  Command<E> create(Sequence<E> sequence, TypeLiteral<? extends Command<E>> type)
          Factory method to create a new command of the type specified by the token supplied as token.
Methods inherited from class dk.rode.thesis.factorymethod.CommandCreator
createDefault, equals, getCommand, hashCode, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait

Constructor Detail


public ReflectiveCommandCreator()
No-arg constructor.

Method Detail


protected Command<E> create(Sequence<E> sequence,
                            TypeLiteral<? extends Command<E>> type)
                     throws Exception
Description copied from class: CommandCreator
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.

Specified by:
create in class CommandCreator<E,TypeLiteral<? extends Command<E>>>
sequence - The sequence; never null.
type - The token identifying the type of command to create; can be null.
The corresponding command, or null if no such command could be created. If null is returned, a default (on null) command is returned by CommandCreator.getCommand(Sequence, Object).
IllegalArgumentException - If type does not represent a concrete, parameterised Command type.
Exception - If the creation fails; if so, a default (or null) command will be returned by getCommand.
See Also:

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.