java - 在 Java 中避免使用 instanceof

标签 java reflection polymorphism instanceof chain-of-responsibility

拥有“instanceof”操作链被认为是“代码异味”。标准答案是“使用多态性”。在这种情况下我该怎么做?

基类有许多子类;他们都不在我的控制之下。 Java 类 Integer、Double、BigDecimal 等情况类似。

if (obj instanceof Integer) {NumberStuff.handle((Integer)obj);}
else if (obj instanceof BigDecimal) {BigDecimalStuff.handle((BigDecimal)obj);}
else if (obj instanceof Double) {DoubleStuff.handle((Double)obj);}

我确实可以控制 NumberStuff 等等。

我不想在几行就可以的情况下使用多行代码。 (有时我会创建一个 HashMap 映射 Integer.class 到 IntegerStuff 的实例,BigDecimal.class 到 BigDecimalStuff 的实例等。但今天我想要一些更简单的东西。)

我想要这样简单的东西:

public static handle(Integer num) { ... }
public static handle(BigDecimal num) { ... }

但 Java 不能那样工作。

我想在格式化时使用静态方法。我正在格式化的东西是复合的,其中一个 Thing1 可以包含一个数组 Thing2s,而一个 Thing2 可以包含一个 Thing1s 数组。当我像这样实现我的格式化程序时遇到了一个问题:

class Thing1Formatter {
  private static Thing2Formatter thing2Formatter = new Thing2Formatter();
  public format(Thing thing) {
      thing2Formatter.format(thing.innerThing2);
  }
}
class Thing2Formatter {
  private static Thing1Formatter thing1Formatter = new Thing1Formatter();
  public format(Thing2 thing) {
      thing1Formatter.format(thing.innerThing1);
  }
}

是的,我知道 HashMap 并且更多的代码也可以解决这个问题。但相比之下,“instanceof”似乎如此可读和可维护。有没有简单但不臭的东西?

2010 年 5 月 10 日添加的注释:

事实证明, future 可能会添加新的子类,而我现有的代码将不得不优雅地处理它们。在这种情况下,Class 上的 HashMap 将不起作用,因为找不到 Class。一连串的 if 语句,从最具体的开始,到最一般的结束,可能是最好的:

if (obj instanceof SubClass1) {
    // Handle all the methods and properties of SubClass1
} else if (obj instanceof SubClass2) {
    // Handle all the methods and properties of SubClass2
} else if (obj instanceof Interface3) {
    // Unknown class but it implements Interface3
    // so handle those methods and properties
} else if (obj instanceof Interface4) {
    // likewise.  May want to also handle case of
    // object that implements both interfaces.
} else {
    // New (unknown) subclass; do what I can with the base class
}

最佳答案

您可能对 Steve Yegge 的亚马逊博客中的这篇文章感兴趣:"when polymorphism fails" .从本质上讲,他正在解决这样的情况,即多态性带来的麻烦多于解决的问题。

问题在于,要使用多态性,您必须使每个“切换”类的“句柄”部分的逻辑——即在这种情况下为整数等。显然这是不切实际的。有时它甚至在逻辑上都不是放置代码的正确位置。他推荐“instanceof”方法,因为它是多种弊端中较小的一个。

与所有被迫编写有味道的代码的情况一样,将其保留在一种方法(或最多一个类)中,这样气味就不会泄露出去。

关于java - 在 Java 中避免使用 instanceof,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2790144/

相关文章:

java - 通过java打开需要认证的网页

Java:对象列表到字符串列表的可能转换

c++ - 虚函数输出奇怪的值

java - 如何将数据插入带有外键的表中?

java - 同步方法和静态变量访问 JAVA/ANDROID

scala - 比较 Scala 反射符号

c# - 如何迭代静态类中的所有静态公共(public)Guid

c# - 如何序列化和处理通用类型中所有事件的订阅?

python - Flask 基于类的 View ——多态性

c++ - 在 C++ 中的多态方法上使用 std::for_each