标记接口与枚举

时间:2021-04-17 09:53:53

标签: java oop enums interface

我正在尝试模拟动物园。

假设我在动物园的区域有以下结构(省略了一些细节)

public abstract class AnimalHabitat{

   private ArrayList<Animal> animals = new ArrayList<>();

   public void setAnimal(Animal animal) {
      animals.add(animal)
   }
}
public class Enclosure extends AnimalHabitat{}
public class Aquarium extends AnimalHabitat{}
public class Cage extends AnimalHabitat{}

然后我有以下动物结构

public abstract class Animal{}
public class Lion extends Animal{}
public class Zebra extends Animal{}
public class Shark extends Animal{}
public class Starfish extends Animal{}
public class Parrot extends Animal{}
public class Eagle extends Animal{}

我想将一种动物添加到其相应的适当栖息地。为了简化代码,我想使用标记界面,例如

public interface TerrestrialAnimal{}
public class Lion extends Animal implements TerrestrialAnimal{}
public class Zebra extends Animal implements TerrestrialAnimal{}

然后我就可以了

public class Zoo{

   public boolean addAnimal(AnimalHabitat habitat, Animal animal) {
      if (animal instanceOf TerrestrialAnimal && habitat instanceOf Enclosure) {
         habitat.set(animal);
         return true;
      }
      if (animal instanceOf AquaticAnimal && habitat instance of Aquarium) {
          habitat.set(animal);
          return true;
      }
      // So for aerial
   }
}

然而另一种方法是使用枚举。例如假设我有

public enum AnimalType{
   Terrestrial, Aquatic, Aerial;
   
   //getter
}

然后在Animal抽象类中我可以定义

public abstract class Animal{
   private AnimalType type;
   // Initialise in the constructor depending on the animal instance
}

我会在 Zoo 的 addAnimal() 方法中做同样的事情。

每种方法的优缺点是什么?谢谢!

2 个答案:

答案 0 :(得分:1)

我会使用枚举。您不需要所有这些 if 语句。

只需在 typeAnimal 中都有属性 AnimalHabitat,然后比较它们。

if (animal.getType() == habital.getType()) { // can add to habitat

如果您想在特定于动物类型的接口中添加一些方法,请切换到接口。

答案 1 :(得分:1)

枚举

优点

  • 易于扩展:您可以轻松增加价值
  • 更巧合的是:你有一个文件来定义所有的 AnimalType
  • 更具可读性:绝对可读
  • 更灵活:您可以在 Enum 上定义方法,您可以使用枚举值打印 AnimalType
  • Comparable:你可以做简单的比较而不是使用 instanceof

使用枚举方法我没有发现任何缺点。

界面

优点

  • 方法:您可以定义常用方法签名
  • 您可以在同一个 Animal 中使用 2 个界面(动物可能有更多栖息地?或更多类型?)
  • 您可以在集合/类变量中使用接口作为超类型

缺点

  • 昂贵:绝对昂贵,每种类型一个接口

在您的示例中,我更喜欢 Enums,因为您使用接口来定义动物类型,并且可以使用 Enums 轻松完成。如果您需要定义通用方法签名或希望使用接口作为超类型,请使用接口,如下所示:

List<TerrestrialAnimal> terrestrialAnimal = new ArrayList<>(); 它可以包含所有陆生动物。

相关问题