27.03.2007, 22:19
Hier die Lösung eines Java Entwicklers (Event und Factory basiert):
Code:
public class FindAndKillElefant implements FoundAnimalListener {
WildLifeFactory factory;
public static void main(String[] args) {
factory = WildLifeFactory.getInstance(WildLifeFactory.STARTPoints.NORTH_AFRICA, WildLifeFactory.ENDPoints.SOUTH_AFRICA);
factory.addAnimalFoundListener(this);
factory.search();
}
public void animalFound(AnimalFoundEvent afe) {
Animal animal = afe.getFoundAnimal();
if(animal instanceof Elefant) {
Elefant elefant = (Elefant) animal;
ElefantKiller killer = new ElefantKiller(factory);
boolean isdeath = killer.kill(elefant);
if(!isdeath) factory.release(elefant);
else {
System.out.println("Du hast gerade einen "+elefant.getWeight()+" KG schweren Elefant getötet!");
System.exit(0);
}
}
}
class ElefantKiller extends WildlifeKiller {
public ElefantKiller(WildLifeFactory factory) {
super(factory);
}
public boolean kill(Animal animal) {
animal.stopHearth(true);
return removeAnimalFromWildlifeByAnts(animal);
}
}
}