net.kolls.railworld
Class ExtensibleFactory<T extends SaveLoad>

java.lang.Object
  extended by net.kolls.railworld.ExtensibleFactory<T>
Type Parameters:
T - The type of object to create in the factory. Must implement SaveLoad

public class ExtensibleFactory<T extends SaveLoad>
extends java.lang.Object

A factory that creates instances by name and is extensible. New types can be added (e.g. by plugins) and then instantiated by other classes. Works with the save/load system to allow saving and restoring of various classes. Type parameter is supertype of all the items for that factory.

Author:
Steve Kollmansberger
See Also:
SaveLoad

Constructor Summary
ExtensibleFactory()
          Create a new extensible factory.
 
Method Summary
 void addType(T item)
          Adds a new subclass of the master item type to the factory.
 java.util.ArrayList<T> allTypes()
          Returns an instance of each item class known.
 void clear()
          Removes all items.
 T createInstance(java.lang.String type)
          Given the Object.toString() value, creates a new instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExtensibleFactory

public ExtensibleFactory()
Create a new extensible factory.

Method Detail

clear

public void clear()
Removes all items.


addType

public void addType(T item)
Adds a new subclass of the master item type to the factory. The instance's toString method will be queried immediately and that will be the key by which the item is found. If an item already exists with that key, it will be replaced.

Parameters:
item - An instance of the class to add

allTypes

public java.util.ArrayList<T> allTypes()
Returns an instance of each item class known. Note that a new instance for each item class is expected each time this method is called.

Returns:
T[]

createInstance

public T createInstance(java.lang.String type)
                                  throws java.lang.ClassNotFoundException
Given the Object.toString() value, creates a new instance.

Parameters:
type - The name of the item class, as defined in the toString method.
Returns:
New item class instance
Throws:
java.lang.ClassNotFoundException - If no item class is found to match the requested type.