|
Evaluating Software Design Patterns — the "Gang of Four" patterns implemented in Java 6 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object dk.rode.thesis.prototype.PrototypeFactory
public class PrototypeFactory
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.
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 | ||
---|---|---|
|
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 . |
|
|
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 |
---|
private final Method copy
Copyable.copy()
method defined in the
Copyable
interface. Never null.
private static final ProxyFactory factory
copyable
objects. Never null.
Constructor Detail |
---|
public PrototypeFactory()
Method Detail |
---|
public <T> T getPrototypicalObject(Class<T> type, T object)
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!
T
- The type of object
.type
- A class literal representing the type of object
;
cannot be null.object
- The object to make prototypical; cannot be null.
NullPointerException
- Either argument is null.getPrototypicalObject(TypeLiteral, Object)
public <T> Copyable<T> getPrototypicalObject(TypeLiteral<T> type, T object)
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!
T
- The type of object
.type
- A type literal representing the type of object
;
cannot be null.object
- The object to make prototypical; cannot be null.
NullPointerException
- Either argument is null.getPrototypicalObject(Class, Object)
public String toString()
toString
in class Object
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |