避免Java中的instanceof [英] avoid instanceof in Java

查看:105
本文介绍了避免Java中的instanceof的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在大学的某个阶段被告知(并且随后在十几个地方读过),使用 instanceof 只能用作最后的手段。考虑到这一点,是否有人能够告诉我以下代码是否是最后的手段。我已经看过堆栈溢出但是找不到类似的情况 - 也许我错过了它?

I have been told at some stage at university (and have subsequently read in upteen places) that using instanceof should only be used as a 'last resort'. With this in mind, is anyone able to tell be if the following code I have is a last resort. I have had a look around on stack overflow but cannot quite find a similar scenario - perhaps I have missed it?

private void allocateUITweenManager() {
   for(GameObject go:mGameObjects){
      if (go instanceof GameGroup) ((GameGroup) go).setUITweenManager(mUITweenManager);
   }
}

其中


  • mGameObjects 是一个数组,其中只有一些是 GameGroup 类型

  • GameGroup 是抽象类 GameObject 的子类。

  • GameGroup 使用接口 UITweenable 其中包含方法 setUITweenManager()

  • GameObject 不使用接口 UITweenable

  • mGameObjects is an array, only some of which are GameGroup type
  • GameGroup is a subclass of abstract class GameObject.
  • GameGroup uses interface UITweenable which has method setUITweenManager()
  • GameObject does not use interface UITweenable

我想我可以同样(也可能应该)在我的代码中替换 GameGroup 以上 UITweenable - 我会问同样的问题。

I suppose I could equally (and probably should) replace GameGroup in my code above with UITweenable - I would be asking the same question.

还有另一种方法可以避免的instanceof ?这段代码不会失败,因此(我认为,对吧?),但是如果按下 instanceof 似乎得到了,我是否已经在某个地方犯了一些OOP罪我在这里使用 instanceof

Is there another way of doing this that avoids the instanceof? This code cannot fail, as such (I think, right?), but given the bad press instanceof seems to get, have I committed some cardinal sin of OOP somewhere along the line that has me using instanceof here?

提前致谢!

推荐答案

我了解了访问者模式在大学的编译器课程中,我认为它可能适用于您的场景。考虑下面的代码:

I learned about Visitor pattern in Compiler class at university, I think it might apply in your scenario. Consider code below:

public class GameObjectVisitor {

    public boolean visit(GameObject1 obj1) { return true; }
    .
    .
    // one method for each game object
    public boolean visit(GameGroup obj1) { return true; }
}

然后你可以在 GameObject中放一个方法这样的界面:

And then you can put a method in GameObject interface like this:

public interface GameObject {

    .
    .
    public boolean visit(GameObjectVisitor visitor);
}

然后每个 GameObject 实现此方法:

public class GameGroup implements GameObject {

    .
    .
    .
    public boolean visit(GameObjectVisitor visitor) {
        visitor.visit(this);
    }
}

当你有复杂的继承层次结构时,这个特别有用 GameObject 。对于您的情况,您的方法将如下所示:

This is specially useful when you've complex inheritance hierarchy of GameObject. For your case your method will look like this:

private void allocateUITweenManager() {

    GameObjectVisitor gameGroupVisitor = new GameObjectVisitor() {
        public boolean visit(GameGroup obj1) {
            obj1.setUITweenManager(mUITweenManager);
        }
    };

    for(GameObject go:mGameObjects){
      go.visit(gameGroupVisitor);
   }
}

这篇关于避免Java中的instanceof的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆