FAQ 8.13 Is container-of-thing a kind-of container-of-anything?

No, even if thing is a kind-of anything.

This design error is, unfortunately, both common and disastrous.

Despite its intuitive appeal, container-of-thing is not a kind-of container-of- anything (more precisely, a container-of-thing is not substitutable for a container-of-anything). A container-of-anything allows anything to be inserted into it, but container-of-thing only allows things to be inserted into it. Therefore, container-of-thing is strictly less powerful than container-of-anything, and the software that is based on the promises of container-of-anything breaks when it receives a container-of-thing instead.

This is often surprising to people new to OO or the study of proper inheritance. The reason for the surprise is that people often rely on the intuitive notion of kind of, but the intuitive notion is inappropriate as a design principle. The reasons are that kind of is somewhat muddy (what exactly does it mean?), and also it is wrong often enough to be unreliable and therefore dangerous. Remember: the most important issue is substitutability. If intuition and substitutability are in conflict, reject intuition.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net