简体   繁体   English

避免Java实例

[英]Avoiding instanceof Java

I am trying to find a way to bypass the use of instanceof. 我试图找到一种方法来绕开instanceof的使用。 I've created a class Item which has multiple subclasses like WeaponItem and BodyItem. 我创建了一个类Item,其中包含多个子类,例如WeaponItem和BodyItem。 Now I would like to make to do a call such as equip(Item) and it should determine by itself which overloaded function it should call such as equip(BodyItem). 现在,我想进行一个诸如equip(Item)的调用,它应该自行确定应该调用哪个重载函数,例如equip(BodyItem)。

Is there a way to bypass the use of instanceof for this case and what would you recommend? 在这种情况下,有没有一种方法可以绕开instanceof的使用,您会建议什么? I've heard that in most cases using instanceof is bad practice and therefor I want to know what the alternatives are. 我听说在大多数情况下,使用instanceof是不好的做法,因此,我想知道替代方法是什么。

Code: 码:

inv.equip(it); // inv = inventory object, it = Item

An example of equip function within inventory class how I preferably want it 库存类中的Equip函数示例,我希望如何使用它

public void equip(HelmItem it) 
{
    if (it != this.getHelm())
    {
        this.setHelm(it);
    }
}

How I had it before: 我以前怎么过的:

public void equip(Item it)
{
    if (it instanceof WeaponItem)
    {
        if (it != this.getWeapon())
        {
            this.setWeapon((WeaponItem) it);
        } 
    } etc for all subclasses of item
}

Indeed, this could be solved with a visitor pattern. 确实,这可以通过访客模式解决。

However, it does not have to be a full-blown visitor, but a simplified variation of it. 但是,它不一定是成熟的访客,而是它的简化版本。 You could pass the inventory to the item and let the item do whatever it wants with it: 您可以将库存传递给物料,然后让物料用它做任何想做的事情:

abstract class Item {
    public abstract void equip(Inventory inv);
}

class HelmItem extends Item {
    @Override
    public void equip(Inventory inv) {
        inv.setHelm(this);
    }
}

class WeaponItem extends Item {
    @Override
    public void equip(Inventory inv) {
        inv.setWeapon(this);
    }
}

Then you can just call: 然后,您可以致电:

it.equip(inv)

without the instanceof operator. 没有instanceof运算子。

Why not put the method in the Item concrete class, and it can equip itself? 为什么不将方法放在Item具体类中,并且它可以自我装备? It's a little counter intuitive but it would solve your problem. 这有点反常,但可以解决您的问题。

public class SomeConcreteItem extends Item {
    public void equip(Body body) {
        // Just an example.
        body.getSections().get(0).equip(this);
    }
}

That way, the concrete implementation knows how to equip itself and the classes that use it don't care. 这样,具体的实现就知道如何装备自己,而使用它的类则不在乎。 You can reference it by the Item superclass and provided that the Item superclass has an abstract method public void equip(Body body); 您可以通过Item超类引用它,并提供Item超类具有抽象方法的方法public void equip(Body body); , then you don't ever need to know about the concrete implementation and, therefore, no need for the instanceof operator. ,那么您永远不需要了解具体的实现,因此也不需要instanceof运算符。

A Note on Introducing a Design Pattern 设计模式介绍

You should be careful about introducing Design Patterns. 您在引入设计模式时应该小心。 People have a bad habit of leaping straight to a complicated pattern to solve a problem, when really something simpler and (in my opinion) more elegant is available. 当确实可以使用更简单且(在我看来)更优雅的东西时,人们习惯直接跳到复杂的模式来解决问题。

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

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