繁体   English   中英

在 Java 中避免 instanceof

[英]Avoiding instanceof in Java

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

一个基类class有多个子类; 他们都不在我的控制之下。 类似的情况是 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 并不是那样工作的。

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

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 日添加的注释:

事实证明,将来可能会添加新的子类,而我现有的代码将不得不优雅地处理它们。 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 的 Amazon 博客中的这篇文章感兴趣: “when polymorphism failed” 从本质上讲,他是在处理这样的情况,当多态带来的麻烦比它解决的麻烦时。

问题是要使用多态性,您必须制作每个“切换”类的“处理”部分的逻辑 - 即在这种情况下为 Integer 等。 显然这是不切实际的。 有时从逻辑上讲,它甚至不是放置代码的正确位置。 他建议将“instanceof”方法作为多种弊端中的较小者。

与您被迫编写臭味代码的所有情况一样,将其固定在一种方法(或至多一个类)中,以免异味泄漏。

正如评论中强调的那样,访问者模式将是一个不错的选择。 但是如果没有对目标/接受者/访问者的直接控制,您就无法实现该模式。 即使您无法通过使用包装器直接控制子类(以 Integer 为例),这里仍然可以使用访问者模式的一种方式:

public class IntegerWrapper {
    private Integer integer;
    public IntegerWrapper(Integer anInteger){
        integer = anInteger;
    }
    //Access the integer directly such as
    public Integer getInteger() { return integer; }
    //or method passthrough...
    public int intValue() { return integer.intValue(); }
    //then implement your visitor:
    public void accept(NumericVisitor visitor) {
        visitor.visit(this);
    }
}

当然,包装一个 final 类可能被认为是它自己的味道,但也许它非常适合您的子类。 就我个人而言,我不认为instanceof在这里有那么糟糕的气味,特别是如果它仅限于一种方法并且我很乐意使用它(可能是根据我上面自己的建议)。 正如您所说,它的可读性、类型安全性和可维护性很强。 一如既往,保持简单。

您可以将处理的实例放入映射(键:类,值:处理程序)中,而不是巨大的if

如果按键查找返回null ,则调用一个特殊的处理程序方法,该方法尝试查找匹配的处理程序(例如,通过对映射中的每个键调用isInstance() )。

找到处理程序后,将其注册到新密钥下。

这使得一般情况快速而简单,并允许您处理继承。

您可以使用反射:

public final class Handler {
  public static void handle(Object o) {
    try {
      Method handler = Handler.class.getMethod("handle", o.getClass());
      handler.invoke(null, o);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  public static void handle(Integer num) { /* ... */ }
  public static void handle(BigDecimal num) { /* ... */ }
  // to handle new types, just add more handle methods...
}

您可以扩展这个想法来一般处理实现某些接口的子类和类。

我认为最好的解决方案是 HashMap,以 Class 为键,以 Handler 为值。 请注意,基于 HashMap 的解决方案以恒定算法复杂度 θ(1) 运行,而 if-instanceof-else 的嗅觉链以线性算法复杂度 O(N) 运行,其中 N 是 if-instanceof-else 链中的链接数(即要处理的不同类的数量)。 所以基于 HashMap 的解决方案的性能比 if-instanceof-else 链解决方案的性能渐进高 N 倍。 考虑到您需要以不同的方式处理 Message 类的不同后代:Message1、Message2 等。 下面是基于 HashMap 处理的代码片段。

public class YourClass {
    private class Handler {
        public void go(Message message) {
            // the default implementation just notifies that it doesn't handle the message
            System.out.println(
                "Possibly due to a typo, empty handler is set to handle message of type %s : %s",
                message.getClass().toString(), message.toString());
        }
    }
    private Map<Class<? extends Message>, Handler> messageHandling = 
        new HashMap<Class<? extends Message>, Handler>();

    // Constructor of your class is a place to initialize the message handling mechanism    
    public YourClass() {
        messageHandling.put(Message1.class, new Handler() { public void go(Message message) {
            //TODO: IMPLEMENT HERE SOMETHING APPROPRIATE FOR Message1
        } });
        messageHandling.put(Message2.class, new Handler() { public void go(Message message) {
            //TODO: IMPLEMENT HERE SOMETHING APPROPRIATE FOR Message2
        } });
        // etc. for Message3, etc.
    }

    // The method in which you receive a variable of base class Message, but you need to
    //   handle it in accordance to of what derived type that instance is
    public handleMessage(Message message) {
        Handler handler = messageHandling.get(message.getClass());
        if (handler == null) {
            System.out.println(
                "Don't know how to handle message of type %s : %s",
                message.getClass().toString(), message.toString());
        } else {
            handler.go(message);
        }
    }
}

有关 Java 中 Class 类型变量用法的更多信息: http : //docs.oracle.com/javase/tutorial/reflect/class/classNew.html

您可以考虑责任链模式 对于您的第一个示例,例如:

public abstract class StuffHandler {
   private StuffHandler next;

   public final boolean handle(Object o) {
      boolean handled = doHandle(o);
      if (handled) { return true; }
      else if (next == null) { return false; }
      else { return next.handle(o); }
   }

   public void setNext(StuffHandler next) { this.next = next; }

   protected abstract boolean doHandle(Object o);
}

public class IntegerHandler extends StuffHandler {
   @Override
   protected boolean doHandle(Object o) {
      if (!o instanceof Integer) {
         return false;
      }
      NumberHandler.handle((Integer) o);
      return true;
   }
}

然后类似地用于您的其他处理程序。 然后是按顺序将 StuffHandler 串在一起的情况(最具体到最不具体,带有最终的“后备”处理程序),并且您的firstHandler.handle(o);程序代码只是firstHandler.handle(o); .

(另一种方法是,而不是使用链,只在您的调度程序类中有一个List<StuffHandler> ,并让它循环遍历列表直到handle()返回 true)。

只需使用instanceof。 所有的解决方法似乎都更复杂。 这是一篇谈论它的博客文章: http : //www.velocityreviews.com/forums/t302491-instanceof-not-always-bad-the-instanceof-myth.html

我已经使用reflection解决了这个问题(大约 15 年前的泛型时代)。

GenericClass object = (GenericClass) Class.forName(specificClassName).newInstance();

我定义了一个通用类(抽象基类)。 我已经定义了许多基类的具体实现。 每个具体类都将加载 className 作为参数。 这个类名被定义为配置的一部分。

基类定义所有具体类的公共状态,具体类将通过​​覆盖基类中定义的抽象规则来修改状态。

当时还不知道这个机制叫什么名字,一直被称为reflection

这篇文章中列出了更多的替代方案: Mapenum除了反射。

在 BaseClass 中添加一个返回类名的方法。 并使用特定的类名覆盖方法

public class BaseClass{
  // properties and methods
  public String classType(){
      return BaseClass.class.getSimpleName();
  }
}

public class SubClass1 extends BaseClass{
 // properties and methods
  @Override
  public String classType(){
      return SubClass1.class.getSimpleName();
  }
}

public class SubClass2 extends BaseClass{
 // properties and methods
  @Override
  public String classType(){
      return SubClass1.class.getSimpleName();
  }
}

现在按以下方式使用开关盒-

switch(obj.classType()){
    case SubClass1:
        // do subclass1 task
        break;
    case SubClass2:
        // do subclass2 task
        break;
}

我用于 Java 8 的内容:

void checkClass(Object object) {
    if (object.getClass().toString().equals("class MyClass")) {
    //your logic
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM