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

dk.rode.thesis.abstractfactory
Class PrototypicalRegistry

java.lang.Object
  extended by dk.rode.thesis.abstractfactory.PrototypicalRegistry

@ParticipantUsage(value="ConcreteProduct",
                  type=StrictCopyable.class)
@Participant(value="ConcreteFactory")
public class PrototypicalRegistry
extends Object

A prototypical registry creates instances of copyable objects based on registered prototypes.

Implementation notes:
Using the Prototype pattern as a means to create different types of objects is an, perhaps safer, alternative to creating objects using reflection. This class to what Gamma et al. call a Prototype Manager [Gamma95, p.121]. However, since this registry is based on class literals as keys to identify the prototype objects, generic types cannot be created without requiring an unchecked cast. The Factory type illustrates how reflection can be used in the creation process (of a single type) in a type safe manner, even for generic types. Type literals could also be used as the keys to identify the stored prototype objects, which will solve the casting problem, but that would require creation of type literals for all lookups and creation requests.

The registry enforces a correlation between the class and prototypical object type, but this could be relaxed to allow for adaptors, decorators, and proxies to be registered for a given class.

This registry does not enforce an upper bound on the prototypical objects to create in a manner similar as the SingletonRegistry, but could easily have done so.

Author:
Gunni Rode / rode.dk
See Also:
PrototypicalFactory

Field Summary
private  Map<Class<? extends StrictCopyable<?>>,StrictCopyable<?>> prototypes
          The prototypical objects to use registered by their class.
 
Constructor Summary
PrototypicalRegistry()
          No-arg constructor.
 
Method Summary
<T extends StrictCopyable<?>>
T
create(Class<T> type)
          Creates a new instance of the prototypical type supplied as type.
 Set<Class<? extends StrictCopyable<?>>> getRegisteredTypes()
          Returns a read-only collection of all prototypical types registered to this registry.
<T extends StrictCopyable<?>>
boolean
isRegistered(Class<T> type)
          Returns true if a prototypical object is registered for the type supplied as type, false if not.
<T extends StrictCopyable<?>>
void
registerPrototype(Class<T> type, T prototype)
          Registers the prototypical object supplied as prototype to this factory associated with the type supplied as type.
 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

prototypes

private final Map<Class<? extends StrictCopyable<?>>,StrictCopyable<?>> prototypes
The prototypical objects to use registered by their class.

Never null.

Constructor Detail

PrototypicalRegistry

public PrototypicalRegistry()
No-arg constructor.

Prototypes must be registered with the registerPrototype(Class, StrictCopyable) method.

Method Detail

create

public <T extends StrictCopyable<?>> T create(Class<T> type)
Creates a new instance of the prototypical type supplied as type.

Type Parameters:
T - The type of prototype.
Parameters:
type - The prototype type; cannot be null.
Returns:
The new instance; never null.
Throws:
NullPointerException - If type is null.

getRegisteredTypes

public Set<Class<? extends StrictCopyable<?>>> getRegisteredTypes()
Returns a read-only collection of all prototypical types registered to this registry.

Returns:
The registered types; never null, but can be empty.
See Also:
isRegistered(Class)

isRegistered

public <T extends StrictCopyable<?>> boolean isRegistered(Class<T> type)
Returns true if a prototypical object is registered for the type supplied as type, false if not.

Type Parameters:
T - The type of prototype.
Parameters:
type - A class literal representing the prototypical type; cannot be null.
Returns:
True if registered, false if not.
Throws:
NullPointerException - If type is null.
See Also:
registerPrototype(Class, StrictCopyable), getRegisteredTypes()

registerPrototype

public <T extends StrictCopyable<?>> void registerPrototype(Class<T> type,
                                                            T prototype)
Registers the prototypical object supplied as prototype to this factory associated with the type supplied as type.

prototype is copied so this factory does not keep a reference to it.

Type Parameters:
T - The type of prototype.
Parameters:
type - A class literal representing the prototypical type; cannot be null.
prototype - The prototypical object; cannot be null.
Throws:
NullPointerException - If either argument is null.
See Also:
isRegistered(Class)

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.