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

dk.rode.thesis.abstractfactory
Class StandardFactory<E,P>

java.lang.Object
  extended by dk.rode.thesis.abstractfactory.StandardFactory<E,P>
Type Parameters:
E - The type of values delivered by the created abstractions and generators.
P - The type of argument required in the creation process of generators. Use Void if no arguments are required.
All Implemented Interfaces:
AbstractionFactory<E>, GeneratorFactory<E,P>, SequenceFactory<E,P>

@Participant(value="ConcreteFactory")
public class StandardFactory<E,P>
extends Object
implements SequenceFactory<E,P>, AbstractionFactory<E>, GeneratorFactory<E,P>

A standard factory can create sequence abstractions as well as the sequence value generators used as the implementations for the abstractions, and further more provides a convenient creation method to create sequences in a single step.

Implementation notes:
This factory works by composition: attached AbstractionFactory and GeneratorFactory instances perform the actual creation, and they can be changed at runtime, making this factory very flexible.

Author:
Gunni Rode / rode.dk
See Also:
PrototypicalSequenceFactory, SequenceFactory, AbstractionFactory, GeneratorFactory

Field Summary
private  AbstractionFactory<E> abstractionFactory
          The currently used abstraction factory.
private  GeneratorFactory<E,P> generatorFactory
          The currently used generator factory.
 
Constructor Summary
StandardFactory()
          Constructor.
StandardFactory(AbstractionFactory<E> abstractionFactory, GeneratorFactory<E,P> generatorFactory)
          Constructor.
 
Method Summary
 SequenceAbstraction<E> createAbstraction()
          Creates a new sequence abstraction instance.
 SequenceValueGenerator<E> createGenerator()
          Creates a new sequence value generator using the default argument.
 SequenceValueGenerator<E> createGenerator(P argument)
          Creates a new sequence value generator, where argument is an argument used in the creation process of the generator.
 Sequence<E> createSequence(P argument)
          Creates a new initialised sequence instance.
 AbstractionFactory<E> getAbstractionFactory()
          Returns the abstraction factory currently used by this factory.
 P getDefaultArgument()
          Returns the default argument to use for generator creation in case no argument is supplied to the GeneratorFactory.createGenerator(Object) method.
 GeneratorFactory<E,P> getGeneratorFactory()
          Returns the generator factory currently used by this factory.
 void setAbstractionFactory(AbstractionFactory<E> abstractionFactory)
          Sets the abstraction factory to use by this factory to abstractionFactory.
 void setGeneratorFactory(GeneratorFactory<E,P> generatorFactory)
          Sets the generator factory to use by this factory to generatorFactory.
 String toString()
          Returns the string representation of this factory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

abstractionFactory

private AbstractionFactory<E> abstractionFactory
The currently used abstraction factory.

The factory may change at runtime.

Will be null until explicitly set.


generatorFactory

private GeneratorFactory<E,P> generatorFactory
The currently used generator factory.

The factory may change at runtime.

Will be null until explicitly set.

Constructor Detail

StandardFactory

public StandardFactory()
Constructor.

Abstraction and generator factories must be explicit set before this factory can be used to create abstractions and generators, respectively.

See Also:
setAbstractionFactory(AbstractionFactory), setGeneratorFactory(GeneratorFactory)

StandardFactory

public StandardFactory(AbstractionFactory<E> abstractionFactory,
                       GeneratorFactory<E,P> generatorFactory)
Constructor.

The abstraction and generator factories supplied as abstractionFactory and generatorFactory will be set as the factories used, respectively. They can be changed at runtime if so desired.

Parameters:
abstractionFactory - The abstraction factory to use; cannot be null.
generatorFactory - The generator factory to use; cannot be null.
Throws:
NullPointerException - If either argument is null.
See Also:
setAbstractionFactory(AbstractionFactory), setGeneratorFactory(GeneratorFactory)
Method Detail

createAbstraction

public SequenceAbstraction<E> createAbstraction()
Description copied from interface: AbstractionFactory
Creates a new sequence abstraction instance.

Depending on the actual factory implementation, the returned abstraction may be initialised. If so, no implementation need be set.

Specified by:
createAbstraction in interface AbstractionFactory<E>
Returns:
A new sequence abstraction instance; never null.

createGenerator

public SequenceValueGenerator<E> createGenerator()
Description copied from interface: GeneratorFactory
Creates a new sequence value generator using the default argument.

Specified by:
createGenerator in interface GeneratorFactory<E,P>
Returns:
A new created generator; never null.
See Also:
GeneratorFactory.createGenerator(Object), GeneratorFactory.getDefaultArgument()

createGenerator

public SequenceValueGenerator<E> createGenerator(P argument)
Description copied from interface: GeneratorFactory
Creates a new sequence value generator, where argument is an argument used in the creation process of the generator.

Specified by:
createGenerator in interface GeneratorFactory<E,P>
Parameters:
argument - A context supplied argument to use in the construction of the generator, if any. If null, the default argument is used (which may also be null, depending on the actual implementation).
Returns:
A new created generator; never null.
See Also:
GeneratorFactory.createGenerator()

createSequence

public Sequence<E> createSequence(P argument)
Creates a new initialised sequence instance.

First, a SequenceAbstraction is created by the abstraction factory currently used. If the abstraction is initialised, it will be returned. If not, a generator will be created using the argument supplied as argument, if any, by the generator factory currently used. If no argument is supplied, the default argument is used. Then the generator is attached as the implementation for the created abstraction, which is finally returned.

Specified by:
createSequence in interface SequenceFactory<E,P>
Parameters:
argument - A context supplied argument to use in creation process. If the created abstraction is initialised, the argument is unused. Can be null, in which case the default argument is used.
Returns:
A new sequence instance; never null.

getAbstractionFactory

public AbstractionFactory<E> getAbstractionFactory()
Returns the abstraction factory currently used by this factory.

Returns:
The factory; can be null if not set.

getDefaultArgument

public P getDefaultArgument()
Description copied from interface: GeneratorFactory
Returns the default argument to use for generator creation in case no argument is supplied to the GeneratorFactory.createGenerator(Object) method.

Specified by:
getDefaultArgument in interface GeneratorFactory<E,P>
Returns:
The default argument, if any; can be null, depending on the actual implementation.

getGeneratorFactory

public GeneratorFactory<E,P> getGeneratorFactory()
Returns the generator factory currently used by this factory.

Returns:
The factory; can be null if not set.

setAbstractionFactory

public void setAbstractionFactory(AbstractionFactory<E> abstractionFactory)
Sets the abstraction factory to use by this factory to abstractionFactory.

Any previously set abstraction factory will be discarded, if any.

Parameters:
abstractionFactory - The factory; cannot be null.
Throws:
NullPointerException - If abstractionFactory is null.

setGeneratorFactory

public void setGeneratorFactory(GeneratorFactory<E,P> generatorFactory)
Sets the generator factory to use by this factory to generatorFactory.

Any previously set generator factory will be discarded, if any.

Parameters:
generatorFactory - The factory; cannot be null.
Throws:
NullPointerException - If generatorFactory is null.

toString

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

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.