What’re some helpful metaphors for the Composite Pattern?

The authors of Actionscript 3.0 Design Patterns offer this one: a branch with leaves, where a Branch is a Composite and a Leaf is a Component. Makes sense — a branch can contain multiple leaves, while a leaf is a self-contained object with no nested elements or no additional items inside it/contained in it. For simplicity’s sake & since no analogy is perfect, let’s leave biology & the fact that a leaf is made up of layers of cells aside.

As good as the branch/leaves metaphor is, different people learn differently, so having additional examples, with alternate ways to describe the same abstract concept can’t hurt.

Perhaps, some of these can be useful too:

  • Ice tray & ice cubes
  • Ammo belt & bullets
  • Moving truck (Composite), boxes (Composite), chair (Component), & box contents (Component or Composite)
  • Organizational chart: Department (Composite), Employee (Component)
  • Suitcase & clothes
  • Directory & files
  • Composition (the “Has-A” relationship) in Object Oriented Programming
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s