Question is related to classes below. Zookeeper1 and Zookeeper2 are 2 alternatives that I could use. I can potentially store in future different types of animals inside Zookeeper. I should be able to get those classes. In 1st case I store all animals in list (meaning in future I can add other new Animals easily), but I need to cast dog with (Dog) when I need to get it. Read somewhere that casts are code-smell, so I wanted to see if there are any alternatives? Other solution prevents casting, but has problem of adding new lists each time I add new animal.
class AnimalId{}
interface Animal{
AnimalId getAnimalId();
void breathe();
}
class Cat implements Animal{
public AnimalId getAnimalId() { return null; }
public void breathe() {}
}
class Dog implements Animal{
public AnimalId getAnimalId() { return null; }
public void breathe() {}
public void bark(){}
}
class ZooKeeper1{
Map<AnimalId, Animal> animals = new HashMap<>(); //future-proof
void addAnimal(Animal a){
animals.put(a.getAnimalId(), a);
}
void printAnimals(){
animals.forEach((key, value) -> System.out.println(key));
}
Dog getDog(AnimalId animalId){
return (Dog)animals.get(animalId); //NOK - must type-cast!
}
public static void main(String[] args) {
ZooKeeper1 zk1 = new ZooKeeper1();
zk1.addAnimal(new Cat());
zk1.addAnimal(new Dog());
zk1.printAnimals();
Dog d = zk1.getDog(new AnimalId());
d.bark();
}
}
class ZooKeeper2{
Map<AnimalId, Cat> cats = new HashMap<>();
Map<AnimalId, Dog> dogs = new HashMap<>(); //will need to add more lines in future
void addCat(Cat c){
cats.put(c.getAnimalId(), c);
}
void addDog(Dog d){
dogs.put(d.getAnimalId(), d); //will need to add more lines in future
}
void printAnimals(){
cats.forEach((key, value) -> System.out.println(key));
dogs.forEach((key, value) -> System.out.println(key)); //will need to add more lines in future
}
Dog getDog(AnimalId animalId){
return dogs.get(animalId); //OK no type-cast
}
public static void main(String[] args) {
ZooKeeper2 zk2 = new ZooKeeper2();
zk2.addCat(new Cat());
zk2.addDog(new Dog());
zk2.printAnimals();
Dog d = zk2.getDog(new AnimalId());
d.bark();
}
}
OK, so after looking into heterogeneous containers in Java, I guess this would be so far the best option I have? Any comments on this type of solution?
interface Animal { AnimalId getId(); }
class AnimalId { int id; AnimalId(int id){this.id = id;} public boolean equals(Object o){ return id==((AnimalId)o).id; } public int hashCode(){ return 1; } }
class Cat implements Animal { AnimalId id; Cat(AnimalId id){this.id=id;} public AnimalId getId(){ return id; } public String catSpecific(){ return "CS"; } }
class Dog implements Animal { AnimalId id; Dog(AnimalId id){this.id=id;} public AnimalId getId(){ return id; } public String dogSpecific(){ return "DS"; } }
class Zoo {
private Map<Class<? extends Animal>, Map<AnimalId, Animal>> animals = new HashMap<>();
public <T extends Animal> void assignAnimal(T animal){
animals.computeIfAbsent(animal.getClass(), k -> new HashMap<>()).put(animal.getId(), animal);
}
public <T extends Animal> T getAnimal(Class<T> type, AnimalId animalId){
return type.cast(animals.get(type).get(animalId));
}
public static void main(String[] args) {
Zoo zoo = new Zoo();
AnimalId animalId = new AnimalId(1);
Animal animal1 = new Cat(animalId);
Animal animal2 = new Dog(animalId);
zoo.assignAnimal(animal1);
zoo.assignAnimal(animal2);
Cat cat = zoo.getAnimal(Cat.class, animalId);
Dog dog = zoo.getAnimal(Dog.class, animalId);
System.out.println(cat.catSpecific());
System.out.println(dog.dogSpecific());
}
}