To conclude the previous two posts on using composition for refactoring complex classes
Refactoring complex classes using Composition Part 1
and
Refactoring complex classes using Composition Part 2
Why should complex classes be refactored? To make code smaller and more manageable. To separate code that changes at different rates, and which are not concerned with the same things.
Composition vs Inheritance? Composition = "has a" relationship, Inheritance "is a" relationship.
Inheritance quickly makes you feel like you create Object-Oriented code, but its one of those devices that should be used for a real reason, not just off the cuff.
Reading
- Some background from
C2 Wiki - Composition Instead Of Inheritance
- A good principle from
Evolve aggregations from inheritance heirarchies - Brian Foote and William F. Opdyke
- Look at Adaptor, Facade, Proxy from Design Patterns (GoF) and also the Stategy, which is mentioned here by Erich Gamma
A Conversation with Erich Gamma, Part III
-
Refactoring Catalog - Martin Fowler
-
Refactoring posts - Martin Fowler
Tags: code, refactoring, oo, composition, inheritance