Java - 替代许多其他带有 instanceof 条件的 if 语句 [英] Java - alternative to many else if statements with instanceof conditions

查看:132
本文介绍了Java - 替代许多其他带有 instanceof 条件的 if 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段我非常想清理的凌乱代码,因为它有大约 12 个 else if 并且这些 if 语句中的每一个都检查 instanceof 2 个对象,例如:If (parentObj instanceof superclass && parentObj2 instanceof superclass)

I have this messy code that I really want to clean up because it has about 12 else if's and each of these if statements check for the instanceof 2 objects, so something like: If (parentObj instanceof superclass && parentObj2 instanceof superclass)

每个 if 语句都执行不同的东西,所以我认为多态不会让它变得更好.还因为这个小功能的 12 个不同的超类(大多数 if 语句执行 1 个衬垫)会有点荒谬.多态不起作用的另一个原因是我无权访问父类或超类.我知道很多 instanceof 条件通常不好做,即使我从来没有真正理解为什么.在每个 if 语句中执行的方法不是父类的,而是超类的,所以这就是为什么我需要检查它们的类型,以便我可以转换它们并执行这些方法.关于如何清理它的任何想法?谢谢!

Each if statement executes something different, so polymorphism I think won't make it any better. Also because 12 different super classes for this little functionality (most if statements execute 1 liners) would be a bit ridiculous. And another reason why polymorphism wouldn't work is because I do not have access to the parent class or the super classes. I know that many instanceof conditions is generally not good to do, even though I never really understood why. The methods that are executed inside each if statement are not of the parent class, but are of the super classes, so that's why I need to check for their types so that I can cast them and execute those methods. Any ideas as to how I can clean this up? Thanks!

抱歉缺乏细节,我是在手机上写的.无论如何,下面是我正在处理的一个例子.我已经查看了策略模式,我唯一担心的是出于同样的原因,我将不得不创建许多不同的类,而且我觉得创建许多不同的对象只是为了一直执行 1 个衬垫会有点浪费.在我看来,在策略设计中,我仍然需要进行许多 instanceof 检查才能知道要执行哪个策略.无论如何,下面是一些代码:p

Sorry for the lack of detail, I wrote this on my phone. Anyway below is an example of what I am dealing with. I have looked at the strategy pattern, and my only concern with that was for the same reason that I would have to create many different classes and I felt like creating many different objects just to execute 1 liners all of the time would be a bit wasteful. Also it looks like to me that in a strategy design, I would still have to have the many instanceof checks to know which strategy to execute. Anyway, below is some code :p

if (plotBlockState instanceof Sign && UpgradeBlockState instanceof Sign) {
    //do Sign Stuff
}
else if (plotBlockState instanceof Chest && UpgradeBlockState instanceof Chest) {
    //do Chest Stuff
}
else if (plotBlockState instanceof Dispenser && UpgradeBlockState instanceof Dispenser) {
    //do Dispenser Stuff
}
else if (plotBlockState instanceof Furnace && UpgradeBlockState instanceof Furnace) {
    //do Furnace Stuff
}
else if (plotBlockState instanceof BrewingStand && UpgradeBlockState instanceof BrewingStand) {
    //do Brew Stand Stuff
}
else if (plotBlockState instanceof Hopper && UpgradeBlockState instanceof Hopper) {
    //do hopper Stuff
}
else if (plotBlockState instanceof Dropper && UpgradeBlockState instanceof Dropper) {
    //do dropper Stuff
}
else if (plotBlockState instanceof Beacon && UpgradeBlockState instanceof Beacon) {
    //do beacon Stuff
}
else if (plotBlockState instanceof CreatureSpawner && UpgradeBlockState instanceof CreatureSpawner) {
    //do spawner Stuff
}
else if (plotBlockState instanceof NoteBlock && UpgradeBlockState instanceof NoteBlock) {
    //do noteblock Stuff
}
else if (plotBlockState instanceof Jukebox && UpgradeBlockState instanceof Jukebox) {
    //do jukebox Stuff
}
else if (plotBlockState instanceof Skull && UpgradeBlockState instanceof Skull) {
    //do skull Stuff
}
else if (plotBlockState instanceof CommandBlock && UpgradeBlockState instanceof CommandBlock) {
    //do commandblock Stuff
}

推荐答案

Strategy 模式似乎非常适合这种情况.这里是维基百科文章的链接,应该足以让您入门,而且因为您提供了0 代码,我想我也不会.

The Strategy pattern seems to suit this down to the ground. Here is the link to the wikipedia article, which should be more than enough to get you started, and because you've provided 0 code, I think I won't either.

但是因为我感觉很好

首先,我会让它们实现一个通用接口或超类.这样您就不必测试它们是什么类型的类.只需让它们调用相同的方法即可.我知道你说过多态性是不可能的,但我不知道为什么.如果您无权访问超类.. 制作一个通用接口.

Firstly, I would have them implementing a common interface or superclass. This is so you don't need to test what type of class they are. Simply have them call the same method. I know you've said polymorphism is out of the question, but I'm not sure why. And if you don't have access to the super class.. make a common interface.

例如:

// Undesirable syntax.
if(obj instanceof Dog)
{
    ((Dog)obj).woof();
}
else if(obj instanceof Cat)
{
    ((Cat)obj).meow();
}
else if(obj instanceof Lion)
{
    ((Lion)obj).roar();
}

现在让我们定义一些通用接口,Animal:

Now let's define some common interface, Animal:

public interface Animal
{
    public void speak();
}

现在你的 instanceof 树看起来像这样:

And now your instanceof tree looks like this:

public void makeTalk(Animal obj)
{
    obj.speak();
}

这篇关于Java - 替代许多其他带有 instanceof 条件的 if 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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