Quite a straightforward one, this item, and applicable to all object-oriented programming languages. This explains the very basic use case for polymorphism: not using a type field or tag in a class to mark it for special behaviour, but using a class hierarchy instead. The disadvantages of tagged classes are well known: brittleness in case of change, inability to change behaviour without changing the tagged class itself and cluttered code. On the other hand, class hierarchies can often reflect a natural hierarchy of types in the application domain, leading to a closer match between code and the problem domain it's describing.
I don't know if there's much more to say about this one. But it does bring me straight back to when I did my very first C++, reading the Deitel & Deitel book while at university. I'm pretty sure they used the exact same example, with a hierarchy of shapes, methods to calculate area and all. A very neat idea that's useful in many circumstances, but shouldn't be overused (as discussed in item 16).
Jan Stette
Sunday, 22 February 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment