|
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.Objectdk.rode.thesis.factorymethod.CommandCreator<E,TypeLiteral<? extends Command<E>>>
dk.rode.thesis.factorymethod.ReflectiveCommandCreator<E>
E - The type of values delivered by created commands (and thus sequences).@Participant(value="ConcreteCreator") public class ReflectiveCommandCreator<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.
TypeLiteral,
InstantiableTypeLiteral| Constructor Summary | |
|---|---|
ReflectiveCommandCreator()
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()
| Method Detail |
|---|
protected Command<E> create(Sequence<E> sequence,
TypeLiteral<? extends Command<E>> type)
throws Exception
CommandCreatorcommand of the
type specified by the token supplied as token.
The sequence set as the receiver is supplied
as sequence.
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.
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.CommandCreator.createDefault(Sequence)
|
Gunni Rode / rode.dk | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||