Bohn
Bohn

Reputation: 26929

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 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.

So that's the reason I have defined that Enumeration type in the OwlRelation class. 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?

Upvotes: 0

Views: 112

Answers (3)

Konstantin Chernov
Konstantin Chernov

Reputation: 1936

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. You can read about it here

Upvotes: 1

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112762

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();

Upvotes: 4

Jon Hanna
Jon Hanna

Reputation: 113382

I'm not sure what you are trying to do. From the other direction, the two correct ways to use an enum are:

  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).

  2. You have a non-mutually-exclusive set of values, which you can combine with binary operators so that if e.g. Read is 1, Write is 2 and Execute is 4, then the ability to read and excecute, but not write is 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). 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?" makes sense (which would fit enum-use 2).

So, not much of an answer, but hopefully enough on using enum to help a bit.

Upvotes: 1

Related Questions