Bohn
Bohn

Reputation: 26919

Suggestions for Interface and Class design

My first interface-based time of programming and some confusions in how to design the interfaces.

I have created an interface for Relationships between two nodes, like this:

public interface IOWLRelation
{
    IOWLRelation AddSubClass(IOWLClass parent, IOWLClass child);
}

Then I created the class to implement this interface, like this:

class OWLRelation: IOWLRelation
{
    public OWLClass Parent { get; set; }
    public OWLClass Child { get; set; }

    public IOWLRelation AddSubClass(IOWLClass parent, IOWLClass child)
    {
        this.Parent = (OWLClass) parent;
        this.Child = (OWLClass) child;
        return this;
    }

}

well first of all: do you see a bad-practice in the way I declared these class and interface? Now my confusion is about those two public properties I have defined in my class: Do I need to also define them in the Interface? Also what if I want to add a enumeration member to this class? Whatever I add to my class I should also add it to its interface?

More importantly in the method implementation, I am passing Interface to the parameers of that method, is it bad or correct? should I pass interface or should I pass their real class?

Upvotes: 1

Views: 114

Answers (3)

Rob H
Rob H

Reputation: 1849

Think of the interface according to how it will be consumed. If the caller will want to know about Parent or Child, then you want to expose it in the interface. If they are just implementation details, then do not include them in the interface.

There are not really rigid rules about these things, because it all depends on how the interface is used.

I would say that in AddSubClass, if you are always casting the parameter to an OWLClass, you might want to think about whether that will make sense to the caller. Would they ever try to call it by passing two different implementations of IOWLClass? If thet do this, the cast will throw an exception. Is that acceptable?

Upvotes: 1

Tim Copenhaver
Tim Copenhaver

Reputation: 3302

In this case, I would do your implementation a little bit differently. If you think through the workflow of someone using your interface, it is a little confusing. You have to create a OWLRelation object first, then call the AddSubClass method, which just returns the object you already created. I would use a factory method instead:

public interface IOwlRelation
{
    OwlClass Parent { get; }
    OwlClass Child { get; }
}

public class OwlRelation : IOwlRelation
{
    public OwlClass Parent { get; private set; }
    public OwlClass Child { get; private set; }

    public static IOwlRelation Create(OwlClass parent, OwlClass child)
    {
        return new OwlRelation
        {
            Parent = parent,
            Child = child
        };
    }
}

Notice the Create method is static, so you don't have to create an instance of it just to get an instance of it. Also, the Parent and Child classes are read-only through the interface, but you can set them from inside the class. This is generally a good practice to keep the properties from being overwritten later, but you don't necessarily have to do it that way. I also renamed the classes to use normal camel casing, even for acronyms, since that's part of the C# naming standard.

Upvotes: 1

Michel Keijzers
Michel Keijzers

Reputation: 15347

If other classes inheriting from IOWLRelation also need a parent/child then also define these properties in the interface (maybe rename the interface to a more generic name in that case).

In the interface you should define all properties/methods you want the inherited classes to have (at least).

Passing interfaces is no problem at all. Actually it might be better than the actual class because the interface is more generic.

Upvotes: 1

Related Questions