# Wednesday, August 01, 2007

Strategy

Notes:
shove dynamic behavior into interfaces.  have the main base compose the behavior interfaces.  Then you can dynamically assign the behavior interface instances in the child classes of the main base class.

Decorator:

Basically think of an abstract class like a vehicle.  Then you have a couple of classes inherited directly off of it: truck, bike broken down Chevy, etc.  Then u have a base decoration class, which inherits off of vehicle and also contains a member instance of vehicle, so it both "has a" and "is a" vehicle, and has a constructor that takes a vehicle type.  Next, you've got a bunch of decorations, each of which inherits off of the root decoration class, such as pimped out, racing striped, washed, waxed, painted pink, etc. 

So in code, you can create any of these decorations and pass them in a vehicle or a decorated vehicle.  This way you can have pink truck just as easily as you can have a pimped out-washed-waxed-racing striped broken down Chevy, without having to create a complicated inheritance tree for each possible combination.

  • Some Notes:
    sort of define your own inheritance tree on the fly
  • defines an "is a"/"has a" relationship between the decorations and the object they're decorating
  • one possible use:  to dynamically aggregate functionality of semi-heterogenous items
  • composition + inheritance
Wednesday, August 01, 2007 7:08:40 PM (GMT Daylight Time, UTC+01:00)
Friday, May 13, 2011 8:47:21 AM (GMT Daylight Time, UTC+01:00)
How are you
Friday, May 13, 2011 9:29:53 AM (GMT Daylight Time, UTC+01:00)
GOOD
OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview