interface vs class

J

Jimski

Hi,

I am creating a model to navigate a tree of symbols and links. My main
purpose of this post is to ascertain the quickest way of navigating the
tree. There are two ways that I am considering, see below. I am unsure
why you would implement interfaces in this design? Is it a performance
issue?

Simple method
-------------

The properties simply reference the actual link objects and symbol
objects.

public class Symbol
{
ArrayList listLinks
ArrayList listLinksEntering
ArrayList listLinksExiting
}

public class Link
{
Symbol fromSymbol
Symbol toSymbol
}

Interface method
----------------

The interfaces ISymbol and ILink implement the properties within each
class. The ICollection stores a collection of ILink references.

public class Symbol : ISymbol
{
ICollection listLinks
ICollection listLinksEntering
ICollection listLinksExiting
}

public class Link : ILink
{
ISymbol fromSymbol
ISymbol toSymbol
}


Thanks in advance.

Jimski
 
M

Miha Markic [MVP C#]

Hi Jimski,

As I see it, I would use interfaces if I don't want to inherit from Symbol
or List when creating a "derived" class.
Otherwise I would preffer inheritance.
 
R

Richard Blewett [DevelopMentor]

The difference won't be speed (although there is a tiny theoretical difference it won't be measurable) it will be flexibility. In the second you can use any type that implements ICollection, ILink and ISymbol rather than just the concrete classes you show in the first example. If you don't need that flexibility then stick with the first method as it is simpler to code in general and is slightly better for versioning if the various pieces are deployed in separate assemblies.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

Hi,

I am creating a model to navigate a tree of symbols and links. My main
purpose of this post is to ascertain the quickest way of navigating the
tree. There are two ways that I am considering, see below. I am unsure
why you would implement interfaces in this design? Is it a performance
issue?
 
J

Jimski

Thanks Richard.

The interface example comes from a component package that we have
bought. I guess when distributing components interfaces are useful.

Why would they affect the versioning when deployed in separate
assemblies?

Thanks
Jimski
 
R

Richard Blewett [DevelopMentor]

If you add a method to an interface you break any implementors (this isn't an issue if you control all of the implementors, e.g they are in the same assembly as the interface). Adding methods to classes doesn't break anything unless you add an abstract member to a class.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

Thanks Richard.

The interface example comes from a component package that we have
bought. I guess when distributing components interfaces are useful.

Why would they affect the versioning when deployed in separate
assemblies?

Thanks
Jimski
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top