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