在java中instanceof_避免在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);}

我确实可以控制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”似乎是如此可读和可维护。有没有简单但没有臭味的东西?

事实证明,将来可能会添加新的子类,而我现有的代码将不得不优雅地处理它们。在这种情况下,基于HashMap的类将无法正常工作,因为找不到该类。从最具体的开始到最一般的结束的一串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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值