Combination or inheritance
-
In object-oriented programming, why is combination better than inheritance in code reuse, for example, given the coordinates of two points to calculate the distance between two points, should the Distance class be inherited from the Point class or an object of the Point class as a data member of the Distance class?
-
In object-oriented programming, why is combination better than inheritance in code reuse, for example, given the coordinates of two points to calculate the distance between two points, should the Distance class be inherited from the Point class or an object of the Point class as a data member of the Distance class?
The question comes down to is distance fundemental to point or is it a function executed and that is up to how you intend to use it. The general rule is function over inheritance and it's clearly covered by wikipedia Composition over inheritance - Wikipedia[^] If point has shapes that derive from it and distance is the closest point between the two shapes then we can provide a clear implementation of how it must work. The only way to reconcile the closest point between two complex shapes will be to run each geometry (line,arc,spline etc) of each shape against the other shape or scanline the two shapes in a minimal box that includes both shapes. Either of those two processes will require these functions 1.) reset or seek so a geometry index will be the next out (seek_geometry(i)) 2.) retrieve the next geometry from the shape (read_geometry(i)) 3.) check if this geometry was last, if not back to 2 we go (check_geometry_end). Essentially the geometry callback function(s) will be inherited from point but the distance will be another object because it will take two objects and run the process. Distance can then evolve to take a list of objects or different sorts of objects. It is easy to see in that situation distance is not really a function of a shape it is a function between two shapes. However it could also be between a shape and something else such as a bounding box or grid or something yet to be thought of. So from a flexibility and reuse point of view you code for functionality and make point and distance two different objects. In the simplest example it would make little difference but the functionality coding would make it easier to extend, reuse and more flexible. Ultimately you are more likely to reuse the more flexible code.
In vino veritas
-
In object-oriented programming, why is combination better than inheritance in code reuse, for example, given the coordinates of two points to calculate the distance between two points, should the Distance class be inherited from the Point class or an object of the Point class as a data member of the Distance class?
元昊 潘 wrote:
...should the Distance class be inherited from the Point class or an object of the Point class as a data member of the Distance class?
Is a vs. Has a
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles