问题与以下课程有关。 Zookeeper1和Zookeeper2是我可以使用的两种选择。我可以在未来的Zookeeper中存储不同类型的动物。我应该能够得到那些课程。在第一种情况下,我将所有动物存储在列表中(意思是将来我可以轻松地添加其他新动物),但是当我需要它时,我需要与狗(狗)一起施展。阅读某些演员阵容闻名的地方,所以我想知道是否有其他选择?其他解决方案可以防止投射,但每次添加新动物时都会出现添加新列表的问题。
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();
}
}
答案 0 :(得分:0)
想象一下,我在没有任何课程ZooKeeper1
的情况下编写了课程Dog
,并将其传递给了您。然后,您决定扩展该类并添加方法Dog getDog(AnimalId id)
。
您希望这只能起作用吗?如果你看到你的推理存在差距,那么你就会明白为什么施法是一个坏主意。
施法不是一个奇迹般的解决方案。使用它的唯一安全方法是仅投射已知类型的对象;例如,如果您将Dog
实例存储在Animal
类型的变量中,那么您肯定知道可以将getAnimal(..)
的结果转换为Dog
类型。
答案 1 :(得分:0)
好的,所以在查看Java中的异构容器后,我想这到目前为止是我的最佳选择吗?对此类解决方案有何评论?
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());
}
}