如何从多个类扩展状态 [英] How to extend states from multiple classes

查看:139
本文介绍了如何从多个类扩展状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(请注意:交易卡游戏Magic:The Gathering的知识将在这里加分。抱歉,我不知道如何更轻松。)

(Please note: knowledge of the trading card game Magic: The Gathering will be a plus here. Sorry, I don't know how to put it any easier.)

我使用 Java 遇到了一个问题,我将描述如下......我有一个名为Card的基本类,具有以下所有属性:

I have hit upon a problem using Java, which I'll describe as follows... I have a fundamental class called Card with all the following attributes:

public class Card{
    String Name;
    String RulesText;
    String FlavorText;
    String Cost;
    int ConvertedCost;
    String Rarity;
    int Number;
}

Permanent类扩展了Card,并依次由Creature类扩展,飞行者,神器,土地和结界。截至目前,其中只有前两个有自己的字段:

The Permanent class extends Card, and is extended in turn by the classes Creature, Planeswalker, Artifact, Land and Enchantment. As of yet, among them, only the first two have fields of their own:

public class Creature extends Permanent{
    int Power;
    int Toughness;
}
public class Planeswalker extends Permanent{
    int Loyalty;
}

问题:


  • 某些永久对象可能受影响其子类对象的方法的影响,例如,作为工件和生物,或作为工件和土地。我知道我可以使用接口来扩展行为,但除非我使用抽象类,否则我不能这样做以扩展状态,在某些情况下我需要一个状态为 Artifacts和Creatures的对象。

  • 通过定位不同子类的对象的方法,不能影响给定子类的大多数永久对象。 (即,仅针对Enchantment对象的方法不会影响Creature对象。)

  • Some Permanent objects may be subject to methods affecting an object of its subclasses, for example, both as Artifacts AND Creatures, or as Artifacts AND Lands. I know I can use interfaces to extend behavior, but I cannot do that to extend state unless I use abstract classes, and in some instances I need an object with the state of both Artifacts and Creatures.
  • Most Permanent objects of a given subclass cannot be affected by a method targeting an object of a different subclass. (I.e., a method targeting Enchantment objects only cannot affect Creature objects.)

推荐答案

我认为你遇到的问题比你知道的多。卡片和永久物牌之间存在差异,卡牌会发挥作用,如召唤生物,而召唤的永久物牌就是游戏中的生物。该卡在游戏中用于表示方便,但在编程期间您需要区分它们。

I think you have more problems than you know. There's a difference between a card and a Permanent, a card performs an effect, like summoning a creature, while the Permanent resulting from the summoning is the creature in play. The card gets used in the game to represent both things for convenience but during programming you will want to distinguish between them.

MTG存在永久物与a不直接相关的情况卡(像Hive这样的卡片生成的生物只由代币代表,它们没有卡片表示),所以你的继承层次结构不起作用。虽然你需要保持卡与被召唤的生物之间的联系(所以你知道什么时候把卡送回丢弃堆),我不认为你所设定的具体继承关系将是有用。您可能需要一个卡层次结构和另一个不同的永久物层次结构。不要忽略层次结构的可能性(因为有许多可以有效利用的继承关系),只需要小心将单独的东西分开。

MTG has cases where Permanents are not related directly to a card (cards like The Hive generate creatures represented only by tokens, they do not have a card representation), so your inheritance hierarchy does not work for that. While you will need to keep a linkage between a card and the creature summoned by it (so you know when to send the card back to the discard pile), I don't think the specific inheritance relationship you have laid out is going to be useful. You may need one hierarchy for cards and another different one for Permanents. Don't ignore the possibility of hierarchies (because there are many inheritance relationships that can be leveraged usefully), just be careful to keep separate things separate.

我建议阅读Steve Yegge关于通用设计模式的博文,他在那里谈论在他的游戏中使用属性模式(看起来像MTG一样灵活)。很明显,游戏中很多东西的特征是可变的(包括什么是生物,什么不是,因为有卡片可以改变生物的土地),处理属性和改变它们的系统(包括临时变化到期)是将是至关重要的。一个类层次结构本身不足以适应MTG中的各种效果。

I would recommend reading Steve Yegge's blogpost on the Universal Design Pattern, where he talks about using a Properties pattern for his game (which seems about as flexible as MTG). Obviously a lot of characteristics of things in the game are mutable (including what's a creature and what isn't, since there are cards that change lands to creatures) and a system for handling properties and changes to them (including expiring temporary changes) is going to be essential. By itself a class hierarchy is not going to be flexible enough to accommodate the variety of effects in MTG.

MTG的有趣之处在于你拥有这个表面主题领域(由生物,法术,文物,土地等组成的东西)似乎可以理解和坚实,但规则允许这些事情以不可预测的方式改变(特别是因为总是可以引入新卡)。主题域通过游戏域(操纵主题域的规则集)来实现。如果您将主题域硬编码到您的实现中(通过扩展超类或实现接口来表示Lands或Artifacts),总会遇到您不能做的事情,并且解决这些问题可能很难或不可能。阅读规则,注意引入的技术术语(卡片,永久物,令牌,效果等),因为这是您需要实施的真实域名。

What makes MTG interesting is that you have this ostensible subject-matter domain (composed of things like Creatures, Spells, Artifacts, Lands, etc.) that seems understandable and solid, but the rules allow these things to change in unpredictable ways (especially since new cards can always be introduced). The subject-matter domain is implemented through a game domain (the set of rules that manipulate the subject-matter domain). If you hard-code the subject-matter domain into your implementation (by doing things like representing Lands or Artifacts by extending a super class or implementing an interface) there are always going to be things you run into that you can't do, and fixing these problems may be hard or impossible. Read the rules noting the technical terminology introduced (cards, permanents, tokens, effects, etc.), because that's the real domain that you need to be implementing.

这篇关于如何从多个类扩展状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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