From f31d34d197264e46db0280ff70128e516e7f72f8 Mon Sep 17 00:00:00 2001 From: Peter Hertkorn <peter.hertkorn@reutlingen-university.de> Date: Wed, 15 Jun 2022 09:07:15 +0200 Subject: [PATCH] Initial commit --- .../predators/PredatorFactory.java | 32 ++++++++++++++++--- .../predators/factory.properties | 2 ++ 2 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 src/aliensimulator/predators/factory.properties diff --git a/src/aliensimulator/predators/PredatorFactory.java b/src/aliensimulator/predators/PredatorFactory.java index 14ad9e3..c17973e 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 0000000..7144c0f --- /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 -- GitLab