Simberon Design Minute
 

Deep Class Hierarchies

When developers first learn about object oriented programming, the concept they tend to latch onto the most is inheritance. They start modeling everything as class hierarchies and often these class hierarchies go quite deep. I saw one system with 13 levels of subclasses. What you discover when you do this is that it's very hard writing code at the bottom of a deep class hierarchy. To effectively write code for a subclass, you really need to understand how the superclass works. When there are many superclasses above you, understanding them all can get very difficult.

You should aim for a class hierarchy of two or maybe three levels below the base level. Sometimes, an infrastructure requires you to subclass from a particular class. In that case treat that class as your base.

To keep your hierarchies small, you should try to get behavior by delegating it to other objects rather than by inheriting it.

Download