简体   繁体   English

我使用枚举的方式出了点问题

[英]Something is wrong with the way I am using Enumerations

public enum RelationType { Subclass, HasArm, HasLeg };
public RelationType RelationShipType { get; set; }

public static IOwlRelation AddSubClassRelation(IOwlClass parent, IOwlClass child)
{
    return new OwlRelation
    {
        Parent = parent,
        Child = child,
        RelationShipType = RelationType.Subclass
    };
}

So let's say this was in a OwlRelation class and now in the consumer class first I create a OwlRelation object by saying like 假设这是在OwlRelation类中,现在在消费者类中,我首先创建一个OwlRelation对象,例如

OwlRelation r1 = OwlRelation.AddSubClassRelation( someParent, someChild);

and also have a method like AddRelation(OwlRelation) that I can pass that r1 object to it, now in the body of this method I can check and see what was the value of the enumeration on this object and some stuff based on that value. 并且还有一个类似AddRelation(OwlRelation)的方法可以将r1对象传递给它,现在在此方法的主体中,我可以检查并查看此对象的枚举值是什么,以及基于该值的一些东西。

So that's the reason I have defined that Enumeration type in the OwlRelation class. 因此 ,这就是我在OwlRelation类中定义了Enumeration类型的原因。 BUT I think this is not the correct way of axchieving this and prob I just don't have enough expertise to figure it out. 但是我认为这不是解决此问题的正确方法,而且可能我只是没有足够的专业知识来解决。 So what do you think is the correct way of doing this? 那么您认为这样做的正确方法是什么?

You could implement different relation types as different classes. 您可以将不同的关系类型实现为不同的类。

public abstract class OwlRelation : IOwlRelation
{
    // Implement infrastructure common to all relation types
}

public SubclassOwlRelation : OwlRelation
{
    // Implement things specific to Subclass-relations
}

public HasArmOwlRelation : OwlRelation
{
    // Implement things specific to HasArm-relations
}

...

With your implementation you are likely to run into code like this one 在您的实现中,您可能会遇到这样的代码

switch (relation.RelationshipType) {
    case RelationshipType.Subclass:
        DoSomeSubclassStuff(relation);
        break;
    case RelationshipType.HasArm:
        DoSomeArmStuff(relation);
        break;
    case RelationshipType.HasLeg:
        DoSomeLegStuff(relation);
        break;
}

With an object-oriented approach the corresponding code looks like this 使用面向对象的方法,相应的代码如下所示

relation.DoSomeStuff();

I'm not sure what you are trying to do. 我不确定您要做什么。 From the other direction, the two correct ways to use an enum are: 从另一个方向看,使用enum的两种正确方法是:

  1. You have a mutually-exclusive set of values, the total number of which are less than the integer-type you are basing the enum on ( int is the default, long is the largest possible), and you can reasonably list every one of them (or expect users to know the number that corresponds with missing items, due to some external standard). 您有一组互斥的值,其总数小于您作为enum所基于的整数类型( int是默认值, long是最大可能值),并且可以合理地列出每个值(或由于某些外部标准,希望用户知道与缺失物品相对应的编号)。

  2. You have a non-mutually-exclusive set of values, which you can combine with binary operators so that if eg Read is 1, Write is 2 and Execute is 4, then the ability to read and excecute, but not write is 5 ( 1 | 4 ). 您有一组非互斥的值,可以将它们与二进制运算符结合使用,因此,例如,如果Read为1, Write为2, Execute为4,则读取和执行但不写入的能力为5( 1 | 4 )。

Now, I haven't looked at the Web Ontology Language (I'm guessing that's the sort of OWL you mean) in a long time, but I don't see how Arm and Subclass fits into this at all (nor remember anything about arms in OWL). 现在,我已经很长时间没有看过Web本体语言了(我想这就是您所指的OWL),但是我完全看不到Arm和Subclass如何适应这种语言(也不记得任何有关OWL中的武器)。 But I also don't see how it fits into anything else - I don't see either the question "Is it an arm, or a subclass?" 但是我也看不到它如何适用于其他任何东西-我也看不到“是手臂还是子类?”这个问题。 makes sense (which would fit enum-use 1) nor the question "Is it an arm, a subclass, or both, or neither?" 有意义(适合枚举1),也没有问题“是手臂,子类,还是两者兼而有之?” makes sense (which would fit enum-use 2). 有道理(适合枚举2)。

So, not much of an answer, but hopefully enough on using enum to help a bit. 因此,答案不多,但希望可以使用enum有所帮助。

I think it's a typical problem and it's solved by applying strategy pattern. 我认为这是一个典型的问题,可以通过应用策略模式来解决。 So it would be different RelationStrategy subclasses as @Olivier has suggested. 因此,就像@Olivier建议的那样,将是不同的RelationStrategy子类。 You can read about it here 你可以在这里读到它

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

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