枚举中的特殊成员:全部和无

发布于 2024-09-13 17:53:28 字数 1015 浏览 4 评论 0原文

可能的重复:
如何将 Enum 与其他选项一起使用(全部、无)

有点主观。

我有一个枚举

public enum Faction {
    Aliance,
    Horde
}

,因为我们都知道它所应用的紧密建模业务领域的代码“总是更好”。 在给定的域中有两个派系,并且这两个派系在上面的集合中枚举。

问题:支持和反对包含特殊成员的论点是什么,例如上面枚举中的 None 和 All 。

我个人认为这两个不属于那里,因为不存在“全部”和“无”这样的派别。使用 flags-enum 也是不合适的。

解决方法是使用另一个枚举,它将模拟与派系的从属关系,在这种情况下,适合其中包含 All 和 None 等元素。

问题#2:为了模型的正义性,我应该创建 FactionAffiliation 枚举吗?或者我应该节省自己额外的打字时间并查看 Faction 枚举,就好像它 FactionAffiliation 一样?

编辑:重复如何使用具有附加选项的枚举(全部、无)

Possible Duplicate:
How to use Enum with aditional options (All, None)

Somewhat subjective.

I have an enum

public enum Faction {
    Aliance,
    Horde
}

as we all know code closely modeling business domain it is applied is "always better".
In a given domain there are two factions, and these two factions are enumerated in the set above.

Question: What are the arguments for and against including special members such as: None, and All in the enum above.

Personally I think those two do not belong there, since there is no such faction as All and None. Also using flags-enum is not appropriate.

The workaround is to have another enum, that will model an affiliation with a faction, in that case it is appropriate to have elements such as All and None in there.

Question #2: Should I create FactionAffiliation enum for the sake of righteousness of the model? Or should I spare myself extra typing and view Faction enum as if it is FactionAffiliation?

Edit: Duplicate of How to use Enum with additional options (All, None)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

囍孤女 2024-09-20 17:53:28

当您处理打算用作位域的枚举时,AllNone 最有意义(您应该标记[Flags])。在这种情况下,您应该将第一个标志的值指定为 1,将第二个标志的值指定为 2。然后,None 可以采用默认值 0 并表示不存在派系隶属关系。 All 成员是可选的,因为它可以通过在其他两个标志上应用按位或来形成。标志枚举通常具有复数名称,例如 FactionAffiliations

如果您不处理标志,通常最好不要包含 None 值。您的大多数程序不应该需要这个,因此这是您不必测试的特殊情况。如果您需要此 None 值,请考虑使用枚举的可为空版本:Faction?。可为 null 的枚举比 None 枚举更好地表示值的缺失。

我假设您的代码是 C#,但大多数建议应该适用于其他语言。

All and None mostly make sense when you're dealing with enumerations you intend to use as bitfields (which you should mark [Flags]). In that case, you should give your first flag a value of 1, and your second a value of 2. Then, None can take the default value of 0 and represent the absence of faction affiliation. The All member is optional as it can be formed by applying a bitwise or on your two other flags. Flags enumerations typically have a plural name, like FactionAffiliations.

If you're not dealing with flags, it's usually better not to include a None value. Most of your program shouldn't need this so it's a special case you won't have to test. If you ever need this None value, consider using a nullable version of your enum instead: Faction?. A nullable enum better represents the absence of a value than a None enumerant.

I assumed your code was C#, but most of the advice should hold in other languages.

温暖的光 2024-09-20 17:53:28

我会避免“全部”和“无”。给定一个人,说:该人可能属于一个派系。所以要回答“这个人属于哪个派系?”的问题。我们可以简单地(比如说)读取该人的派系字段。但对于 All 的特殊情况,属于(例如)部落的人也将属于 All - 即使该人的派系字段 != ALL。这改变了 IN 的含义,从而导致代码复杂和出现细微错误的风险。

I would avoid All and None. Given a Person, say: that Person may belong to one Faction. So to answer the question "Which Faction is this Person in?" we could simply (say) read the Person's faction field. But for the special case of All, the Person who belongs to (say) Horde would also belong to All - even though that Person's faction field != ALL. That changes the meaning of IN, in ways that lead to complicated code and risks of subtle bugs.

毁梦 2024-09-20 17:53:28

我从更实际的角度来看待使用,而不是从纯粹的面向对象设计。

是否有一些地方不允许您使用“全部”或“无”派系/派系关系?您绝对必须在其中声明一个派系?

如果是这样,那么您应该使用两个单独的枚举。

如果不是——如果你在任何地方都使用一个派系,“全部”和“无”都是可行的选项,那么只使用一个,并随心所欲地称呼它。

I look at the use from a more practical view, rather than from pure OO design.

Are there places where you will use a Faction/FactionAffliance where All or None would be not allowed; where you absolutely must have exactly one Faction declared?

If so, then you should use the two separate enums.

If not -- if everywhere you use a faction, "All" and "None" are viable options, then use just one, and call it whatever you like.

明天过后 2024-09-20 17:53:28

我想说你的实施应该得到改进。
您正在使用枚举来模拟简单的布尔状态。
完全摆脱枚举并将其替换为布尔属性:“IsAlliance”或“IsHorde”,以任何一个为准。
枚举是多种可能性的模型,但你没有多种可能性。

I would say that your implementation should be improved.
You're using an enum to model a simple boolean state.
Get rid of the enum altogether and replace it with a boolean property: 'IsAlliance' or 'IsHorde', whichever.
An enum is a model for multiple possibilities, and you don't have multiple possibilities.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文