<rdf:RDF
    xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
    xmlns:s='http://snipsnap.org/rdf/snip-schema#'
    xml:base='http://www.peerbox.com:8668/rdf'>
    <s:Snip rdf:about='http://www.peerbox.com:8668/rdf#start/2006-10-17/1'
         s:name='start/2006-10-17/1'
         s:cUser='kgr'
         s:oUser='kgr'
         s:mUser='kgr'>
        <s:content>1 Favour What over What? {anchor:Favour What over What?}&#xA;Favour composition over inheritance.&#xD;&#xA;&#xD;&#xA;All learned OO practitioners will tell you the same thing.  But why?  The reason is that dynamic composition offers better code reuse (and therefore, better productivity) than static inheritance. &#xD;&#xA;&#xD;&#xA;Consider that I have an Interface ‘Abc’ and that I have a few implementations: ‘SimpleAbc’, ‘DefaultAbc’, ‘CompoundAbc’, ‘MockAbc’, ‘SuperSpecialAbc’, etc.&#xD;&#xA;&#xD;&#xA;Now consider the following ways that I might want to add additional behaviour to implements of Abc:&#xD;&#xA;   * synchronization&#xD;&#xA;   * logging&#xD;&#xA;   * caching&#xD;&#xA;   * authentication&#xD;&#xA;   * filtering (of some collection based return values)&#xD;&#xA;   * read-only support&#xD;&#xA;   * sorting (of some collection based return values)&#xD;&#xA;   * etc.&#xD;&#xA;&#xD;&#xA;With inheritance you would need 1024 classes to implement all combinations: 2^7 features * 5 implementations.&#xD;&#xA;&#xD;&#xA;With composition you would only need 12 classes: 5 implementations + 7 decorators.&#xD;&#xA;&#xD;&#xA;It is very unlikely that you would ever need all 1024 combinations, but it is very likely that you would need more than 12.&#xD;&#xA;&#xD;&#xA;Dynamic composition literally gives exponentially better code reuse than static inheritance.  They say that some &quot;super-programmers&quot; are ten times faster than the average but mastering composition lets you become O(x^n) faster; which is usually much(^n) better than being a mere &quot;ten times&quot; faster.  &#xD;&#xA;&#xD;&#xA;Of course this isn’t really true in a general sense.  It is only true in languages like Java where inheritance is static.  In languages with dynamic inheritance, inheritance is just as good.&#xD;&#xA;&#xD;&#xA;This leads me to conclude that the maxim:&#xD;&#xA;&#xD;&#xA;{quote}Favour Composition over Inheritance{quote}&#xD;&#xA;&#xD;&#xA;could be written more accurately as:&#xD;&#xA;&#xD;&#xA;{quote}Favour __dynamic__ composition over __static__ inheritance.{quote}&#xD;&#xA;&#xD;&#xA;or, more generally, as just:&#xD;&#xA;&#xD;&#xA;{quote}Favour dynamic over static.{quote}&#xD;&#xA;&#xD;&#xA;  &#xD;&#xA;&#xD;&#xA;  &#xD;&#xA;</s:content>
        <s:mTime>2006-10-17 11:54:00.865</s:mTime>
        <s:cTime>2006-10-17 11:54:00.865</s:cTime>
        <s:comments
             rdf:type='http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag'/>
        <s:snipLinks>
            <rdf:Bag>
                <rdf:li rdf:resource='http://www.peerbox.com:8668/rdf#2006'/>
                <rdf:li rdf:resource='#kgr'/>
                <rdf:li rdf:resource='http://www.peerbox.com:8668/rdf#start/2006-03-31/1'/>
            </rdf:Bag>
        </s:snipLinks>
        <s:attachments
             rdf:type='http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag'/>
    </s:Snip>
</rdf:RDF>
