Inline...
Bob Powell [MVP]
Visual C#, System.Drawing
A minor off-topic point, but putting text after your sig really screws
up newsreaders that are sig-aware.
It's true to say that the details are subtle and one can be substituted for
the other in many sutuations however, there are very clear-cut reasons why
one would chose an interface and reject an abstract class and vice-versa.
Before we get into this, I guess it might be nice to actually mention to
the OP reasons for choosing one or the other...
Advantages of abstract base class...
Can force base concrete implementation
Can allow default implemention
Easily extensible
Advantages of Interfaces
Doesn't force inheritance tree, can act as mixin
Fits well with "prefer composition to inheritance"
Not easily extensible.
Yes, I'm being a bit flippant with the "extensible" thing. But one
advantage of interfaces is that it defines a stronger contract. In
practice, this usually forces lazy programmers (like me) to think harder
during both design and maintenance.
We both agree on something later that I'll state upfront: when in doubt,
use an interface.
Even a pure abstract class forces a specific type heirarchy.
True, my point though is that's not what is usually meant by "concrete
implementation", which has a very specific meaning. Base classes *can*
enforce a "concrete implementation", but they don't have to.
This is the point. You absolutely do not need to provide a DLL or any
concrete imlementation in order to hand an interface around to anyone who
might wish to use it. Take for example a .NET implemented plug-in
architecture. It is quite possible to define component interactions in terms
of a set of interfaces and allow anyone to implement them using whatever
concrete class heirarchy they think fit. As long as the contract of the
interface is adhered to you never need to give a DLL, which may contain your
valuable trade secrets, to a third-party.
I'm not sure here if we're being ambiguous about the meaning of
"interface", or if you or I are simply wrong about something. Usually
if you implement a .Net plugin architecture you deliver a DLL with your
interface in it and have others reference the dll and implement the
interface. This works identically with abstract base classes as well.
Usually this DLL doesn't have anything in it *except* the interface or
base class, so there's no issue with trade secrets.
There's a couple of alternatives, but I generally don't like either.
You could ask the client to create their own interface dll from a
definition you give them (although this requires strong naming be turned
off). Or you could publish a written spec of your "interface", then
load their plugin class and call all methods through reflection or late
binding.
Alternatively I suppose the interface could be a standard .Net interface
from the framework, but that's irrelevant here and you could of course
do the same thing with base classes anyway.
Did you have something else in mind? Am I missing something?
Architecture choice is almost impossible to get right first time and often
refactoring rears its ugly head.
I actually think refactoring has a rather attractive visage
But then, I'm kind of TDD-oriented.
From experience I can say that I have had
to refactor far less when I employed an interface based architecture than
when I used a class-heirarchy. I don't say that Abstract classes are not to
be used, just that the implications of using one enforces a pattern that
becomes inflexible as soon as you chose that architecture.
Agreed.
PS. This is a very interesting and timely topic to me because this
weekend I need to decide between an interface and a base class for a
plugin system I'm writing. On the one hand I have a natural tendency
toward interfaces, on the other hand there's some default functionality
I'd like to enforce for performance reasons.