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

dk.rode.thesis.factorymethod
Class Factory<T>

java.lang.Object
  extended by dk.rode.thesis.meta.reflect.TypeLiteral<T>
      extended by dk.rode.thesis.meta.reflect.InstantiableTypeLiteral<T>
          extended by dk.rode.thesis.factorymethod.Factory<T>
Type Parameters:
T - The product type created by this factory.
Direct Known Subclasses:
TypedFactory

@Participant(value="Creator")
public abstract class Factory<T>
extends InstantiableTypeLiteral<T>

A generic factory that will create products of type T.

Usage: to create instances of Foo<String> using the constructor accepting a String and int argument, in that order:

   // Create factory...
   Factory<Foo<String>> factory = 
     new Factory(String.class, Integer.class){}; // anonymous class
     
   // Create products...  
   Foo<String> foo    = factory.newInstance("Bar", 42);
   Foo<String> foobar = factory.newInstance("FooBar", 117);
 
The constructor to create new instances of T is fetched ignoring primitive types, e.g. a formal parameter type of Integer will match both Integer and int.

Implementation notes:
This factory is classified as a Factory Method, not an Abstract Factory, because its traits most closely resemble those described for Factory Method by Gamma et al. [Gamma95, p.107-116], but it is a judgement call. We see it as a variant of the Creator class [Gamma95, p.113], which uses templates to determine the actual type of product to create. We still use it in the Abstract Factory implementation as well, though. These are the reasons why it is named Factory, and not Creator.

The PrototypicalFactory shows how the Prototype pattern can often be used as an alternative to reflection.

Author:
Gunni Rode / rode.dk
See Also:
InstantiableTypeLiteral, TypedFactory

Field Summary
 
Fields inherited from class dk.rode.thesis.meta.reflect.TypeLiteral
type
 
Constructor Summary
protected Factory(Class<?>... parameterTypes)
          Constructor, which creates this factory to create instances of the type represented by the type parameter T using the declared constructor having the formal parameter types supplied as parameterTypes, if any.
 
Method Summary
 String toString()
          Returns the string representation of this factory.
 
Methods inherited from class dk.rode.thesis.meta.reflect.InstantiableTypeLiteral
create, create, create, equals, getConstructor, isInstatiable, isInstatiable, newInstance
 
Methods inherited from class dk.rode.thesis.meta.reflect.TypeLiteral
asClass, asClass, asType, create, create, create, getComponentType, getRawType, getRawType, getType, hashCode
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Factory

protected Factory(Class<?>... parameterTypes)
           throws NoSuchMethodException
Constructor, which creates this factory to create instances of the type represented by the type parameter T using the declared constructor having the formal parameter types supplied as parameterTypes, if any.

The constructor is made accessible if not already. New instances of T must be created with the InstantiableTypeLiteral.newInstance(Object...) factory method.

Parameters:
parameterTypes - The formal parameter types for the declared constructor to use.
Throws:
IllegalArgumentException - If this class is not generic, if T has no raw type, or if the raw raw represents a non instantiable class.
NoSuchMethodException - If no such constructor is declared in the raw type of T.
Method Detail

toString

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

Overrides:
toString in class TypeLiteral<T>
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.