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

dk.rode.thesis.prototype
Class PrototypeFactory

java.lang.Object
  extended by dk.rode.thesis.prototype.PrototypeFactory

public class PrototypeFactory
extends Object

A prototype factory creates various dynamic proxies that allows any object to become copyable, if not already, if it supplies a copy constructor.

Implementation notes:
This factory utilises the Proxy pattern to create prototypical objects. The factory defers the actual proxy creation to the ProxyFactory class, which allows creation of proxies based on any type. The ProxyFactory could have been inherited, but composition is chosen instead to keep the interface of this class clean and focused on copyable objects.

Notice that the created proxies cannot be StrictCopyable because the original type is not copyable.

The prototype factory does not correspond to a Prototype Manager as described by Gamma et al. [Gamma95, p.121] because prototypes are created on the fly. No fixed pool of predetermined prototype objects is used by this factory.

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

Field Summary
private  Method copy
          The Copyable.copy() method defined in the Copyable interface.
private static ProxyFactory factory
          The singleton proxy factory used to create the proxy objects representing copyable objects.
 
Constructor Summary
PrototypeFactory()
          No-arg constructor.
 
Method Summary
<T> T
getPrototypicalObject(Class<T> type, T object)
          Returns a prototypical object representing object that can be copied by invoking Copyable.copy() on it if and only if the class of object supplies a copy constructor and/or directly implements Copyable.
<T> Copyable<T>
getPrototypicalObject(TypeLiteral<T> type, T object)
          Returns a prototypical object representing object that can be copied by invoking Copyable.copy() on it if and only if the class of object supplies a copy constructor and/or directly implements Copyable.
 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

copy

private final Method copy
The Copyable.copy() method defined in the Copyable interface.

Never null.


factory

private static final ProxyFactory factory
The singleton proxy factory used to create the proxy objects representing copyable objects.

Never null.

Constructor Detail

PrototypeFactory

public PrototypeFactory()
No-arg constructor.

Method Detail

getPrototypicalObject

public <T> T getPrototypicalObject(Class<T> type,
                                   T object)
Returns a prototypical object representing object that can be copied by invoking Copyable.copy() on it if and only if the class of object supplies a copy constructor and/or directly implements Copyable. If a prototypical object cannot be created, null is returned.

To invoke the copy() method, object must first be cast into the Copyable type. It will not be possible to cast to a generic type without generating compiler warnings because of type erasure.

A copy constructor of type is defined as a constructor accepting a single parameter of the exact same type as type.

If object implements Copyable, object is simply returned.

The returned prototypical object can be cast into any interface implemented by object, though not the actual type (class) itself, represented by type. Trying to cast the returned object into the actual type (class) of object will thus cause a class cast exception to be thrown!

Type Parameters:
T - The type of object.
Parameters:
type - A class literal representing the type of object; cannot be null.
object - The object to make prototypical; cannot be null.
Returns:
The prototypical object, or null.
Throws:
NullPointerException - Either argument is null.
See Also:
getPrototypicalObject(TypeLiteral, Object)

getPrototypicalObject

public <T> Copyable<T> getPrototypicalObject(TypeLiteral<T> type,
                                             T object)
Returns a prototypical object representing object that can be copied by invoking Copyable.copy() on it if and only if the class of object supplies a copy constructor and/or directly implements Copyable. If a prototypical object cannot be created, null is returned.

A copy constructor of type is defined as a constructor accepting a single parameter of the exact same type as type.

If object implements Copyable, object is simply returned.

The returned instance is cast to a copyable instance matching the exact type parameter E, even if it contains nested type parameters.

The returned prototypical object can be cast into any interface implemented by object, though not the actual type (class) itself, represented by type. Trying to cast the returned object into the actual type (class) of object will thus cause a class cast exception to be thrown!

Type Parameters:
T - The type of object.
Parameters:
type - A type literal representing the type of object; cannot be null.
object - The object to make prototypical; cannot be null.
Returns:
The prototypical object, or null.
Throws:
NullPointerException - Either argument is null.
See Also:
getPrototypicalObject(Class, Object)

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.