SE450: Choosing Delegation [34/35] |
GoF: prefer delegation. Subclassing is too static -- forces awkward class hierachies -- inflexible.
/------------\ | Deal | \------------/ /_\ /_\ | | | | +------------+ +-------------+ | ActiveDeal | | PassiveDeal | +------------+ +-------------+ /_\ /_\ | | | | +------------+ +-------------+ | Tabular | | Tabular | | ActiveDeal | | PassiveDeal | +------------+ +-------------+
versus
/------------\ 1 /-------------------\ | Deal |<>----------------> | PresentationStyle | \------------/ \-------------------/ /_\ /_\ /_\ /_\ | | | | | | | | +------------+ +-------------+ +------------+ +-------------+ | ActiveDeal | | PassiveDeal | | TabularPS | | SinglePS | +------------+ +-------------+ +------------+ +-------------+
In english, we often say "is" when we mean "has the attribute of".
"The shirt is purple" versus "The shirt has a color attribute, which is purple".
"The room is a cuboid" versus "The room has a shape attribute, which is cuboid".