Class hierarchies should be deep and narrow

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
m
Line 1: Line 1:
A good class hierarchy design should be several layers deep. In a good design, a base class should not have large number of direct children classes (i.e. a superclass has 28 subclasses). If a class hierarchy is not deep but very wide, it is a sign that a change is needed.
+
A good class hierarchy design should be several layers deep. In a good design, a base class should not have a large number of direct children classes (eg a superclass having 28 subclasses). If a class hierarchy is not deep but is very wide, it is a sign that a change is needed.
  
Solution for solving this problem is to find some common behaviors from the children sibling classes, and then extract these common behaviors into a parent class under the original base parent class.
+
A solution for solving this problem is to find some behaviour common to multiple child classes, and then extract these common behaviors into a parent class under the original base parent class. An example related to this in [[Johnson and Foote 1988]] is the View class in Smalltalk, which has 27 subclasses, some of which can contain subviews, but not all. The authors propose a concrete subclass of ''View'' called ''ViewWithSubviews'' which implements the subview functionality currently contained in ''View'', and to make ''View'' an abstract superclass.  
  
 
[[Riel's heuristics|Riel's heuristic]] [[Favour deep hierarchies]] is derived from this idea.
 
[[Riel's heuristics|Riel's heuristic]] [[Favour deep hierarchies]] is derived from this idea.
Line 8: Line 8:
 
* [[Favour deep hierarchies]]
 
* [[Favour deep hierarchies]]
 
* [[Design maxims]]
 
* [[Design maxims]]
 +
 +
[[Category:Johnson and Foote's heuristics]]

Revision as of 01:18, 18 August 2009

A good class hierarchy design should be several layers deep. In a good design, a base class should not have a large number of direct children classes (eg a superclass having 28 subclasses). If a class hierarchy is not deep but is very wide, it is a sign that a change is needed.

A solution for solving this problem is to find some behaviour common to multiple child classes, and then extract these common behaviors into a parent class under the original base parent class. An example related to this in Johnson and Foote 1988 is the View class in Smalltalk, which has 27 subclasses, some of which can contain subviews, but not all. The authors propose a concrete subclass of View called ViewWithSubviews which implements the subview functionality currently contained in View, and to make View an abstract superclass.

Riel's heuristic Favour deep hierarchies is derived from this idea.

See also

Personal tools