Skip to content
Snippets Groups Projects
Commit 144cbe91 authored by Peter Hertkorn's avatar Peter Hertkorn
Browse files

Change configurable factory to registry

parent 5d9882c6
No related branches found
No related tags found
No related merge requests found
......@@ -16,6 +16,13 @@ import aliensimulator.verhalten.ErdeNeutralAdapter;
public class AlienSimulator {
public static void main(String[] args) {
try {
Class.forName("aliensimulator.predators.HishQuTen");
Class.forName("aliensimulator.predators.Yautja");
} catch (Exception e) {
e.printStackTrace();
}
AlienFactory alienFactory = new AlienFactory();
Alien alienMars = alienFactory.createAlien("mars");
......
......@@ -5,6 +5,10 @@ import aliensimulator.verhalten.ErdeNeutralAdapter;
public class HishQuTen extends Predator {
static {
PredatorRegistry.register(new HishQuTen());
}
public HishQuTen() {
this.setErdeBesuchverhalten(new ErdeNeutralAdapter(new NeutralBesuchen()));
}
......@@ -14,4 +18,8 @@ public class HishQuTen extends Predator {
System.out.println("Ich bin ein Hish-Qu-Ten.");
}
@Override
public Boolean canHandle(String type) {
return type.equalsIgnoreCase("hishquten");
}
}
......@@ -7,6 +7,8 @@ public abstract class Predator {
public abstract void darstellen();
public abstract Boolean canHandle(String type);
public void fliegen() {
System.out.println("Ich fliege durch den Weltraum.");
}
......
package aliensimulator.predators;
import java.io.IOException;
import java.util.Properties;
public class PredatorFactory {
public Predator createPredator(String type) {
Predator predator = null;
Properties properties = new Properties();
try {
properties.load(this.getClass().getResourceAsStream("predator.properties"));
String className = properties.getProperty(type);
Class<?> classOfPredator = Class.forName(className);
predator = (Predator) classOfPredator.newInstance();
} catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
predator = PredatorRegistry.getPredatorHandler(type);
if (predator == null) {
System.out.println("Kein Handler gefunden");
}
return predator;
}
}
package aliensimulator.predators;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class PredatorRegistry {
static List<Predator> predatorHandlers = new ArrayList<Predator>();
static void register(Predator predator) {
predatorHandlers.add(predator);
}
static Predator getPredatorHandler(String type) {
ListIterator<Predator> iter = predatorHandlers.listIterator();
while (iter.hasNext()) {
Predator predator = iter.next();
if (predator.canHandle(type)) {
return predator;
}
}
return null;
}
}
......@@ -5,6 +5,10 @@ import aliensimulator.verhalten.ErdeZerstoererischAdapter;
public class Yautja extends Predator {
static {
PredatorRegistry.register(new Yautja());
}
public Yautja() {
this.setErdeBesuchverhalten(new ErdeZerstoererischAdapter());
}
......@@ -14,4 +18,8 @@ public class Yautja extends Predator {
System.out.println("Ich bin ein Yautja.");
}
@Override
public Boolean canHandle(String type) {
return type.equalsIgnoreCase("yautja");
}
}
yautja=aliensimulator.predators.Yautja
hishquten=aliensimulator.predators.HishQuTen
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment