rendered paste body//----------------------- Factory class ----------------------------
//
// Employee is abstract class for all employees
//
// kah 09/05
package mazeApplications.mazeCreation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
//import mazeCreation.magical.*;
public class Factory implements FactoryInterface
{
private static ArrayList<Factory> factories;
private String name;
private Factory( String type ) {
name = type;
}
public String toString() { return name; }
public static Factory getInstance( String type ) {
Factory temp = null;
// Search the arraylist to see if c_type is in there already
for(Factory f: factories) {
if(f.toString().equals(type)) {
return f;
}
}
// Instantiate a new Factory for the proposed MazeObject type.
temp = new Factory(type);
factories.add(temp);
return temp;
}
// The problem with this is the fact that the "Basic" type can't instantiate itself immediately.
// So what needs to happen is
public Door createDoor() throws FactoryException {
String classname;
Door d = null;
if(this.toString().equals("Basic")) {
classname = "mazeApplications.MazeCreation.Door";
} else classname = "mazeApplications.MazeCreation." + this.toString().toLowerCase() + this.toString() + "Door";
// Let's instantiate it!
d = (Door) this.instantiate(classname);
if(d == null) {
throw new FactoryException("Unable to instantiate class " + classname + ".");
}
return d;
}
public Room createRoom() throws FactoryException {
String classname;
Room r = null;
if(this.toString().equals("Basic")) {
classname = "mazeApplications.MazeCreation.Room";
} else classname = "mazeApplications.MazeCreation." + this.toString().toLowerCase() + this.toString() + "Room";
r = (Room) this.instantiate(classname);
if(r == null) {
throw new FactoryException("Unable to instantiate class " + classname + ".");
}
return r;
}
public Wall createWall() throws FactoryException {
String classname;
Wall w = null;
if(this.toString().equals("Basic")) {
classname = "mazeApplications.MazeCreation.Wall";
} else classname = "mazeApplications.MazeCreation." + this.toString().toLowerCase() + this.toString() + "Wall";
w = (Wall) this.instantiate(classname);
if (w == null) {
throw new FactoryException("Unable to instantiate class " + classname + ".");
}
return w;
}
private Object instantiate(String what) throws FactoryException {
Class c = null;
Constructor cons = null;
try {
c = Class.forName(what);
} catch (ClassNotFoundException e) {
throw new FactoryException("ClassNotFoundException while trying to find the class " + what);
}
if(!Door.class.isAssignableFrom(c)) {
throw new FactoryException("Class " + c.toString() + " is not assignable to Door");
}
try {
cons = c.getConstructor();
cons.setAccessible(true);
} catch (SecurityException e) {
throw new FactoryException(e.getMessage());
} catch (NoSuchMethodException e) {
throw new FactoryException(e.getMessage());
}
try {
return (Object) cons.newInstance();
} catch (IllegalArgumentException e) {
throw new FactoryException(e.getMessage());
} catch (InstantiationException e) {
throw new FactoryException(e.getMessage());
} catch (IllegalAccessException e) {
throw new FactoryException(e.getMessage());
} catch (InvocationTargetException e) {
throw new FactoryException(e.getMessage());
}
}
}