Inheritance is generally tricky to get right, and we were looking at a fairly complex base class that needed some modifications. Try as we might, neither me nor my pairing partner could figure out how to attack it.
In the end it should have been obvious really. The abstract base class was doing way too much. The concrete classes seemed similar (they were all marketing "landing pages"), but the behaviors were different. The base class was sort of controlling them, with the intention that the concrete classes would just implement the various validation methods appropriate to themselves.
The problem was that based on the results of those validations, there were different actions to be taken. So we ended up with sort of an inversion of the inheritence, where the logic was all in the base class and it was just controlled by essentially flags in the concrete classes. Not fresh, and a pain to figure out when you're looking at some logic and you have to consider what the effects on all the various concrete classes when you need to change the flow.
So if you find yourself looking at a base class with lots of code and classes extending that that aren't adding a lot of their own behavior, that's probably a warning sign (like I said, should have been obvious but it's easy to get caught up in these things).
I remember a talk w/ saskew about how inheritance can suck but I didn't completely agree because I just came out of a project where it did help me out. Your issue kicked off some neurons and I realize that we were talking about two different perspectives. I think the difference was that he was talking from your point by from in my project, the base abstract class was very simple and dumb; the base class methods didn't do much but it gave me an opportunity to introduce control over the entire class objects with little change. I first wrote the base abstract class and pushed logic into it after I wrote dozens of classes with similar business logic. The more servlets/classes, I wrote, I ended up pushing less than more, then less again from the base abstract class until I reached a point of 'balance'. It eventually ended up with a very small base class.
Using the preview button and correcting grammar would help but that's what you get when commenting under the influence of "Wild Turkey Rye".
This is Rob Meyer's weblog, a weblog focused on software development and system administration based on 10 years of experience. Want to explore further? You can find out more me or see the rest of my website.
Wondering if I've written on something in particular? Try searching:
You might want to take a look at some of the more requested postings (as judged by incoming traffic):
Want more? Subscribe to this site
or contact me at rob at big dis dot com.
See my writings on: