相关疑难解决方法(0)

在Java中避免使用instanceof

具有一系列"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);}
Run Code Online (Sandbox Code Playgroud)

我确实可以控制NumberStuff等等.

我不想在几行代码中使用多行代码.(有时我将一个HashMap映射到一个IntegerStuff的实例,将BigDecimal.class映射到一个BigDecimalStuff的实例等等.但是今天我想要一些更简单的东西.)

我想要像这样简单的东西:

public static handle(Integer num) { ... }
public static handle(BigDecimal num) { ... }
Run Code Online (Sandbox Code Playgroud)

但是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);
  }
}
Run Code Online (Sandbox Code Playgroud)

是的,我知道HashMap和更多代码也可以修复它.但相比之下,"instanceof"似乎更具可读性和可维护性.有什么简单但不臭吗?

注释已添加5/10/2010:

事实证明,将来可能会添加新的子类,而我现有的代码必须优雅地处理它们.在这种情况下,类上的HashMap不起作用,因为找不到类.一系列if语句,从最具体的开始到以最一般的结尾,可能是最好的:

if (obj instanceof SubClass1) …
Run Code Online (Sandbox Code Playgroud)

java reflection polymorphism instanceof chain-of-responsibility

96
推荐指数
7
解决办法
7万
查看次数

在Java中避免'instanceof'

我有以下(可能是常见的)问题,现在绝对让我困惑:

有几个生成的事件对象扩展了抽象类Event,我想将它们分成Session Bean,比如

public void divideEvent(Event event) {
    if (event instanceof DocumentEvent) {
        documentGenerator.gerenateDocument(event);
    } else if (event instanceof MailEvent) {
        deliveryManager.deliverMail(event);
        ...
    }
    ...

}
Run Code Online (Sandbox Code Playgroud)

但是将来可能会有两种以上的事件类型,所以if-else将会很长并且可能无法读取.此外,我认为instanceof在这种情况下并不是真正的"最佳实践".

我可以在Event类型中添加一个抽象方法并让它们自行划分但是我必须在每个实体中注入特定的会话Bean.

是否有任何暗示可以为这个问题实现"漂亮"的解决方案?

谢谢你的帮助!

java oop instanceof

63
推荐指数
3
解决办法
9577
查看次数

什么时候应该使用instanceof,何时不使用instanceof

我已经阅读了各种文章,但我仍然不知道为什么不应该使用instanceof.kindlylet我知道你的想法.

java

14
推荐指数
3
解决办法
8446
查看次数

如何使用多态而不是instanceof?(为什么?)

如果我们采用以下代码:

Shape p1 = new Square();
Square c1;
if(p1 instanceof Square) {
  c1 = (Square) p1;
}
Run Code Online (Sandbox Code Playgroud)

偏好多态性是什么意思instanceof,顺便说一下,为什么它更好?

编辑: 我理解多态是什么; 我所缺少的是如何使用它而不是instanceof.

java polymorphism instanceof

14
推荐指数
1
解决办法
7484
查看次数

检查消息类型时避免使用instanceof

我有以下情况,其中客户端类根据它接收的消息类型执行不同的行为.我想知道是否有更好的方法,因为我不喜欢instanceof和if语句.

我想做的一件事是将方法拉出客户端类并将它们放入消息中.我会在IMessage接口中放入一个类似process()的方法,然后将消息特定的行为放在每个具体的消息类型中.这会使客户端变得简单,因为它只调用message.process()而不是检查类型.但是,唯一的问题是条件中包含的行为与对Client类中包含的数据的操作有关.因此,如果我在具体的消息类中实现了一个进程方法,我将不得不将它传递给客户端,我不知道这是否真的有意义.

public class Client {
    messageReceived(IMessage message) {
        if(message instanceof concreteMessageA) {
            concreteMessageA msg = (concreteMessageA)message;
            //do concreteMessageA operations
        }
    }
        if (message instanceof concreteMessageB) {
            concreteMessageb msg = (concreteMessageB)message;
            //do concreteMessageB operations
        }
}
Run Code Online (Sandbox Code Playgroud)

java polymorphism instanceof messages

9
推荐指数
2
解决办法
4795
查看次数