How do we know to favour composition over generalisation is always the right choice?
Posted
by
Carnotaurus
on Programmers
See other posts from Programmers
or by Carnotaurus
Published on 2011-04-10T11:20:42Z
Indexed on
2012/09/05
9:49 UTC
Read the original article
Hit count: 293
Whether an object physically exists or not, we can choose to model it in different ways. We could arbitarily use generalisation or composition in many cases. However, the GoF principle of "favour composition over generalisation [sic]" guides us to use composition. So, when we model, for example, a line then we create a class that contains two members PointA and PointB of the type Point (composition) instead of extending Point (generalisation). This is just a simplified example of how we can arbitarily choose composition or inheritance to model, despite that objects are usually much more complex.
How do we know that this is the right choice? It matters at least because there could be a ton of refactoring to do if it is wrong?
© Programmers or respective owner