|
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.Objectdk.rode.thesis.abstractfactory.PrototypicalRegistry
@ParticipantUsage(value="ConcreteProduct", type=StrictCopyable.class) @Participant(value="ConcreteFactory") public class PrototypicalRegistry
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.
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 | ||
---|---|---|
|
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. |
|
|
isRegistered(Class<T> type)
Returns true if a prototypical object is registered for the type supplied as type , false if not. |
|
|
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 |
---|
private final Map<Class<? extends StrictCopyable<?>>,StrictCopyable<?>> prototypes
Never null.
Constructor Detail |
---|
public PrototypicalRegistry()
Prototypes must be registered with the
registerPrototype(Class, StrictCopyable)
method.
Method Detail |
---|
public <T extends StrictCopyable<?>> T create(Class<T> type)
type
.
T
- The type of prototype.type
- The prototype type; cannot be null.
NullPointerException
- If type
is null.public Set<Class<? extends StrictCopyable<?>>> getRegisteredTypes()
isRegistered(Class)
public <T extends StrictCopyable<?>> boolean isRegistered(Class<T> type)
type
, false if not.
T
- The type of prototype
.type
- A class literal representing the prototypical type; cannot be null.
NullPointerException
- If type
is null.registerPrototype(Class, StrictCopyable)
,
getRegisteredTypes()
public <T extends StrictCopyable<?>> void registerPrototype(Class<T> type, T prototype)
prototype
to
this factory associated with the type supplied as type
.
prototype
is copied so this factory does not keep a reference to it.
T
- The type of prototype
.type
- A class literal representing the prototypical type; cannot be null.prototype
- The prototypical object; cannot be null.
NullPointerException
- If either argument is null.isRegistered(Class)
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 |