M
Mike
Hi,
I generally prefer to use interfaces rather than abstract classes, for
several reasons including the ability to implement multiple interfaces and,
to me, "cleaner" polymorphism.
But, as I write this, I am working on something that has troubled me before,
and I was hoping to get some suggestions.
Basically, my problem with interfaces is that it is harder (for me, anyway)
to enforce contracts between related interfaces. Much of the contract has
to be written down as part of the documentation for the interfaces. For
example, in some cases I "wish" that I could place constructors in
interfaces, such that I could make the no-args constructor private, forcing
the use of a more specific constructor - implementing classes would then
have to provide a constructor with this signature. I also sometimes wish
that I could declare static methods in interfaces.
In general, with abstract classes, I have more control over implementation
and contractual relationships. But I don't like resorting to abstract
classes unless I have to. Although, if there is a common piece of code that
any implementor would have to provide, I may use an abstract class for that
purpose, rather than an interface (or, more likely, an abstract base
implementation of an interface.)
I just don't like leaving everything to documentation. Anyone know what I'm
talking about, and could either point out what I'm doing wrong, or suggest
alternatives? Perhaps a reference to some code that makes good use of
interfaces in this manner?
Thanks.
Mike
I generally prefer to use interfaces rather than abstract classes, for
several reasons including the ability to implement multiple interfaces and,
to me, "cleaner" polymorphism.
But, as I write this, I am working on something that has troubled me before,
and I was hoping to get some suggestions.
Basically, my problem with interfaces is that it is harder (for me, anyway)
to enforce contracts between related interfaces. Much of the contract has
to be written down as part of the documentation for the interfaces. For
example, in some cases I "wish" that I could place constructors in
interfaces, such that I could make the no-args constructor private, forcing
the use of a more specific constructor - implementing classes would then
have to provide a constructor with this signature. I also sometimes wish
that I could declare static methods in interfaces.
In general, with abstract classes, I have more control over implementation
and contractual relationships. But I don't like resorting to abstract
classes unless I have to. Although, if there is a common piece of code that
any implementor would have to provide, I may use an abstract class for that
purpose, rather than an interface (or, more likely, an abstract base
implementation of an interface.)
I just don't like leaving everything to documentation. Anyone know what I'm
talking about, and could either point out what I'm doing wrong, or suggest
alternatives? Perhaps a reference to some code that makes good use of
interfaces in this manner?
Thanks.
Mike