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

@Pattern(name="Factory Method", scope=Class, purpose=Creational, participants={"Product","ConcreteProduct","Creator","ConcreteCreator"})

Package dk.rode.thesis.factorymethod

Implementations and examples of the Factory Method design pattern [Gamma95, p.107].


Class Summary
CommandCreator<E,T> A command creator creates commands to manipulate a given receiver sequence using the CommandCreator.getCommand(Sequence, Object) method.
EvilSequenceCommandCreator<E> An evil sequence command creator is a test creator that flips the functionality offered by the sequence command creator, and may decide to return a new evil command as a default command.
Factory<T> A generic factory that will create products of type T.
Main Factory Method tests.
ReflectiveCommandCreator<E> A reflective command creator creates new commands based on type literals as tokens.
ReversibleSequenceCommandCreator<E> A reversible sequence command creator extends the sequence command creator to allow for the creation of reverse commands for reversible sequences.
SequenceCommandCreator<E> A standard command creator creates new commands based on sequence states as tokens.
TypedFactory<T,P> A generic factory that will create products of type T while only accepting constructor arguments of type P (for all arguments).

Package dk.rode.thesis.factorymethod Description

Implementations and examples of the Factory Method design pattern [Gamma95, p.107].


Here, the Command interface represents the Product participant, and a concrete Command implementation represent the ConcreteParticipant participant, for example NextCommand. Hence, factory methods are defined to create commands. As explained in the Command pattern, the commands primarily use Sequence instances as receivers, but some are receiver-less.

The CommandCreator type represents the Creator participant, and a sub-class of it represent a ConcreteCreator, for example ReversibleSequenceCommandCreator. The CommandCreater<E,T> class is parameterised with the type of the Command (and Sequence) values as well the type of tokens used identify the type of commands to create. The ReversibleSequenceCommandCreator, and others, use internal sequence states as tokens, while the ReflectiveCommandCreator class uses type literals as tokens.

Furthermore, the generic Factory class illustrates how reflection can be used to make a completely generic factory type to create any type of instantiable product, providing the product type has a usable constructor. UML Class Diagram:

Implementation notes:
The log implementation used in this thesis also illustrates the Factory Method pattern.

Gunni Rode / rode.dk

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.