Sunday, September 14, 2008

Coupling: Interfaces and Abstract Classes

When "loosely coupled" is la mode du jour, nobody really likes an abstract class. Well nobody on my team. I suspect there's more going on here than the simple increase in coupling (e.g. the number of assumptions a caller must make) over calling an interface method.

Deriving from an abstract class:
1) forces you to inherit from an object hierarchy, and
2) forces you to provide an implementation for a set of abstract methods, and
3) usually means that some behaviour is "thrown in for free" in the base class(es).

Implementing an interface
1) forces you to provide an implementation for a set of method signatures.

So... use more interfaces; achieve a lower level of coupling, and you'll also implicitly be choosing composition over inheritance.

No comments: