diff --git a/src/aliensimulator/predators/PredatorFactory.java b/src/aliensimulator/predators/PredatorFactory.java index 14ad9e3fe5367105689b17c33998c9d984611dd0..c17973e75e63342e04421724c65693c738832c8c 100644 --- a/src/aliensimulator/predators/PredatorFactory.java +++ b/src/aliensimulator/predators/PredatorFactory.java @@ -1,13 +1,35 @@ package aliensimulator.predators; +import java.io.IOException; +import java.util.Properties; + public class PredatorFactory { + private Properties properties = new Properties(); + + public PredatorFactory() { + try { + properties.load(this.getClass().getResourceAsStream("factory.properties")); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + public Predator createPredator(String type) { - if (type.equals("yautja")) { - return new Yautja(); - } else if (type.equals("hishquten")) { - return new HishQuTen(); - } else return null; + String handlerName = properties.getProperty(type); + Class<?> classOfProtocolHandler = null; + Predator predator = null; + try { + classOfProtocolHandler = Class.forName(handlerName); + predator = (Predator) classOfProtocolHandler.newInstance(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (InstantiationException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + return predator; } } diff --git a/src/aliensimulator/predators/factory.properties b/src/aliensimulator/predators/factory.properties new file mode 100644 index 0000000000000000000000000000000000000000..7144c0f08320f48848c23210b2f62d0b9f35c3e8 --- /dev/null +++ b/src/aliensimulator/predators/factory.properties @@ -0,0 +1,2 @@ +hishquten=aliensimulator.predators.HishQuTen +yautja=aliensimulator.predators.Yautja \ No newline at end of file