Replacing Inheritance With Composition

Ivan Moore briefly discusses replacing inheritance with composition regarding the amout of testing code required. He concludes that with composition,

instead of having the tests duplicated (or using inheritance in the tests to remove the duplication) the tests can now be split into two parts. One test for the class with the code that is common, and separate tests for the code that is different.

This is quite similiar to what I wrote about applying filters to a collection. And as Ivan notes

There are other benefits in addition to the simplification of the tests, and the removal of duplication. Using composition also allows more flexibility in combining these classes together.

Published: January 31 2005