Class cryptiX.utils.Factory
All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class cryptiX.utils.Factory

java.lang.Object
   |
   +----cryptiX.utils.Factory

public class Factory
extends Object
The Factory provides a means to register and instantiate concrete classes that implement a certain abstract class or interface. The purpose of the factory is to facilitate runtime binding of implementation classes. The runtime system can dynamically, or example from applet parameters, what classes to load and instantiate.

In order to use the Factory, all instances of the implemented classes shall be made indirectly, via the factory. The concreate instantiable classes must register themselves with the factory. The runtime environment must load one or more instantiable classes, since otherwise they may not be available...

The Factory class is an abstract class, to be subclassed as the actual "Thing" factory. There may be one factory per package, or one factory per each dynamic interface. In theory, it would be possible to construct a single system wide factory class, but the Java runtime name space checking prevents this. More specificly, Class.newInstance() does not create instances for classes that are protected and in another package. Therefore there must be at least one factory per package.

For an example of a concrete factory, see FactorySimple.java. For more discussion about Design Patterns, see [Gamma et al, Design Patterns]


Constructor Index

 o Factory()
Restrict instantiation to the class itself and subclasses.

Method Index

 o create(Class)
Creates a new instance of the given class.
 o create(Class, Object, boolean)
Create an instance using the given interface, type and default.
 o register(Class, Object, Class)
Register a class as one providing implementations for a given interface and type.

Constructors

 o Factory
  protected Factory()
Restrict instantiation to the class itself and subclasses.

Methods

 o create
  protected abstract Object create(Class implementation) throws InstantiationException, IllegalAccessException
Creates a new instance of the given class. This method must be implemented by a subclass in order to behave with Java package protection.

This is the only non-static function to be implemented by the subclasses. Typically, the factory classes will implement their own, class specific static routines for instance creation. In fact, by doing this clever protection we have enforced this....

Parameters:
implementation - the implementation class to be instantiated
Throws: InstantiationException
if the implementation class was not available, or if it happens to be an abstract class or an interface.
Throws: IllegalAccessException
if an attempt was made to instantiate a of a foreign package not supporting foreign instantiation.
 o create
  protected synchronized Object create(Class iface,
                                       Object type,
                                       boolean singleton) throws InstantiationException
Create an instance using the given interface, type and default.
Parameters:
interface - The abstract superclass or interface
type - An object denoting indirectly which concrete class should be used when instantiating
default - Default type to be used if type == null
singleton - Boolean denoting weather a shared instance should be used.
Returns:
s the newly created, uninitialized instance.
Throws: InstantiationException
if new instance couldn't be created.
 o register
  protected synchronized void register(Class iface,
                                       Object type,
                                       Class implementation)
Register a class as one providing implementations for a given interface and type.
Parameters:
interface - The desired interface class
type - An Object used to denote implementation, typically a String
implementation - The implementation class being registered

All Packages  Class Hierarchy  This Package  Previous  Next  Index