Toby Inkster

Horizontal Reuse: An Alternative to Inheritance

Comparing Perl's Moose with Java, Ruby, and PHP

photo: Jorge Royan

In class-based object-oriented programming, when there are classes that appear to share some functionality, this is often a time when people will refactor them into two subclasses of a common base class, avoiding repetition.

For example, in my farm model the Horse class and the Tractor class each implement a pull_plough method. (American readers might prefer pull_plow.) So this method is a candidate to split out into a Hitchable class for Horse and Tractor to each inherit from.

This would have nice benefits for polymorphism. I’ll be able to pass an object to my Farmer which will only need to check that the object inherits from Hitchable rather than having a hard-coded list of classes like Horse and Tractor that it knows are acceptable.

Read more…

Comments: 9