Subj : Re: Polymorphism sucks [Was: Paradigms which way to go?] To : comp.programming,comp.object From : Robert C. Martin Date : Sat Jul 09 2005 06:58 pm On 9 Jul 2005 11:43:39 -0700, "topmind" wrote: >Here is an example borrowed from c2.com. We taxonomize drinks by making >each orthogonal factor a level: > >DRINKS > > > Coke > --Diet > ----Caffeinated > ----No Caffeine > --Regular > ----Caffeinated > ----No Caffeine > Lemon-Lime soda > --Diet > ----Caffeinated > ----No Caffeine > --Regular > ----Caffeinated > ----No Caffeine > Iced Tea > --Diet > ----Caffeinated > ----No Caffeine > --Regular > ----Caffeinated > ----No Caffeine > >Any one of the three factors (flavor, dietness, caffeiness) could be >put at the top level, but we cannot put them all there. Thus, we must >make one the "king" arbitrarily and demoting one arbitrarily to the >leaves. > >Also note what happens if you add a 4th factor to this tree. Using inheritance to represent degrees of freedom is not always a good idea. In this particular case we are probably better off using composition: |Stimulant|<---------|Coke|-------->|Sweetener| A A | | +-----+---------+ +--------+---------+ | | | | |Caffeine| |NullStimulant| |Sugar| |Nutrisweet| This is the GOF "Bridge" pattern (or strongly related to it). Trees aren't always the right design solution. OO is not primarily about making trees. You *can* make trees with OO, but you don't have to; and you often shouldn't. ----- Robert C. Martin (Uncle Bob) | email: unclebob@objectmentor.com Object Mentor Inc. | blog: www.butunclebob.com The Agile Transition Experts | web: www.objectmentor.com 800-338-6716 "The aim of science is not to open the door to infinite wisdom, but to set a limit to infinite error." -- Bertolt Brecht, Life of Galileo .