这是instanceof的可接受用途吗? [英] Is this an acceptable use of instanceof?

查看:117
本文介绍了这是instanceof的可接受用途吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发卡片游戏UNO的实现(主要用于运行模拟测试一些房屋规则,但这是一个不同的故事)。



对于那些从未玩过它的人来说,它与Crazy Eights类似。玩家轮流在弃牌堆上打牌。卡必须匹配数量或颜色。还有抽奖卡,迫使下一位玩家抽取两张或四张牌。这也非常友好,这使得它非常有趣。



我有类与<卡的code>价值和颜色。我想做的是创建 DrawCard ,它扩展。然后,在确定游戏机制时,我会使用 instanceof 来测试我是否有 DrawCard



我知道 instanceof 通常有问题,但这对我来说似乎没问题。通常这些卡片的处理方式相同,只有少数特殊卡片的处理方式不同,而且只能在特定于卡片类型的特殊情况下使用(虽然这看起来像滑坡一样......)



我可以在类中使用标记(实际上,每种类型的卡都有自己的'值'),但我是无论如何都要扩展以获得其他类型的卡可能具有的某些方法(它不仅仅是一种识别方法)。使用 instanceof 对我来说似乎更为通用,因为我不需要知道的哪些值需要这个特殊值行为,例如,将Draw 8卡添加到Draw 2和Draw 4的当前内容中会很容易。(说这让我想知道我是否可以使用某种嵌套枚举或其他东西,但我不知道知道)



我知道两种解决方案都能正常运行(让程序运行)。对我来说,使用 instanceof 感觉更好,但我没有经验去了解它是否可以。这是一个糟糕的设计吗?

解决方案

简短回答:不是真的可以接受,所以是的 - 这是设计缺陷的标志。 instanceof 几乎总是有异味,除非在极少数情况下,图书馆试图提供某种形式的辅助或其他丑陋的帮助,但有助于克服语言/环境的缺点。 / p>

根据该意见,这个问题有点重复。请参阅答案:



什么时候可以使用instanceof?



它建议使用访问者模式。如果你真的在系统中有两种不同的类型,这可能是真的。



你也不必明确地使用访问者模式,而是将其合并到你的程序流程。所以考虑接口卡:

 接口卡{
void pick(播放器播放器);
}

类DrawCard实现卡片{
void pick(播放器播放器){
player.draw(value); //假设传递的值如, ctor
}
}


I am working on an implementation of the card game UNO (mostly to run simulations to test some house rules, but that's a different story).

For those who've never played it, it's similar to Crazy Eights. Players take turns playing a card on the discard pile. The card has to match in number or color. There are also draw cards, which force the next player to draw either two or four cards. It's also quite friendly to house rules, which makes it pretty interesting.

I have a Card class with the value and color of the card. What I would like to do is to create DrawCard which extends Card. Then, in determining the game mechanics, I would use instanceof to test whether I have a DrawCard.

I know that instanceof is often problematic, but this seems to be okay to me. Usually the cards are processed the same, and only a few special cards are treated differently, and only in specialized circumstances specific to the card type (That seems like a slippery-slope thing to say though...)

I could just use markers in the Card class (actually, each type of card already has its own 'value'), but I am extending Card anyway in order to have some methods that other types of cards may have (it's not just a means of identification). Using instanceof seems more general to me, as I wouldn't need to know about which values of value require this special behaviour, and it would be easy, for example, to add a Draw 8 card to the current contents of Draw 2 and Draw 4. (Saying this makes me wonder if I could use some sort of nested enum or something, but I don't know)

I know that both solutions would work (make the program run). To me using instanceof feels nicer, but I don't have the experience to understand whether it is okay. Is this bad design?

解决方案

The short answer: Not really acceptable, so yes - it's a sign of a design deficiency. instanceof is almost always a smell except in rare cases where a library is trying to do some kind of assistance that is meta or otherwise ugly but helps overcome a language/environment shortcoming.

With that opinion, the question is sort of a duplicate. See the answer to:

When is it acceptable to use instanceof?

It suggests to use the visitor pattern. This may be true if you genuinely have two different types in the system.

You also don't have to use the visitor pattern 'explicitly' but rather incorporate it in to your program flow. So consider an interface card:

interface Card {
  void pick(Player player);
}

class DrawCard implements Card {
  void pick(Player player) {
    player.draw(value); // assume value passed in e.g. ctor
  }
}

这篇关于这是instanceof的可接受用途吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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