Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Composite Pattern

The Composite Pattern

My slides from my recent talk at Burrows on the composite pattern.

Ricky Clegg

April 26, 2012
Tweet

More Decks by Ricky Clegg

Other Decks in Technology

Transcript

  1. The  Composite  Pa.ern   “What  we  need  to  do  is

     learn  to  work  in  the  system,  by  which  I  mean   that  everybody,  every  team,  every  pla=orm,  every  division,  every   component  is  there  not  for  individual  compe@@ve  profit  or  recogni@on,   but  for  contribu@on  to  the  system  as  a  whole  on  a  win-­‐win  basis.”     W.  Edwards  Deming  -­‐  Na2onal  Medal  of  Technology  1987                 Blog:  betweenthebraces.com        Twi*er:  @ricky_clegg          Email:  [email protected]  
  2. Give  me  more  informa@on   •   What  kind  of  pa.ern

     is  it?   •   When  should  I  use  it?   •   Why  should  I  use  it?    
  3. Structural  Pa.erns   •   It  is  part  of  the  Structural

     Pa.erns  group.  This  includes  pa.erns  such   as  Decorator  and  Adapter   •   Structural  pa.erns  are  strategies  that  use  classes  and  objects  to  build   up  larger  structures.  This  mainly  entails  the  use  of  abstract  classes  and   interfaces.  
  4. So  What  Is  The  Composite  Pa.ern?   •   The  composite

     pa.ern  provides  a  robust  solu@on  to  building  complex   systems  that  are  made  up  of  several  smaller  components.  The   components  that  make  up  the  system  may  be  individual  objects  or   containers  that  represent  collec@ons  of  objects.   •   Think  of  a  car  as  a  complex  system  that  is  made  p  of  several  smaller   components.  The  car  contains  an  engine,  body,  chassis,  seats,  @res,  etc.  
  5. Lets  Look  Underneath  The  Bonnet     •   A  wheel

     is  a  composite  object.  It  contains  several  smaller  parts  @re,   rim,  hubcaps.   •   The  thing  that  makes  it  special  is  that  the  client  has  no  need  to  know   about  all  the  parts  of  a  composite  object.  It  can  simply  call  one   opera@on  and  the  composite  object  will  call  the  same  method  on  all  it’s   children  to  make  them  operate.  
  6. The  Composite  Pa.ern  Is  Just  Like  A  Tree   • 

     The  pa.ern  is  made  up  of  branches  and  leaves.   •   A  object  that  has  children  is  referred  to  as  a  composite  node.  An  object   with  no  children  is  called  a  leaf  node.   •   The  trunk  of  the  tree  (client)  does  not  need  to  know  about  the  leaves   or  if  the  branches  have  more  branches.  When  .sway()  is  called  each   composite  object  will  call  .sway()  on  every  branch  and  leaf.  
  7. Sounds  Like  Music  To  My  Ears   •   A  MusicPlaylist

     class  is  a  composite  node.  A  Song  is  a  leaf  node.   •   A  MusicPlaylist  can  contain  Songs  and  other  MusicPlaylists.   •   The  opera@on  they  all  share  is  ‘play()’  and  can  be  built  of  an  IMusic  to   enforce  the  contract.  And  a  Music  abstract  class  to  build  default   func@onality.   •   The  clients  has  no  idea  about  how  many  songs  there  are,  or  how  to   play  them.  Calling  myRnBPlaylist.play()  will  handle  all  subsequent   opera@ons.  
  8. S@ll  Not  Clear  Enough?   If  we  touch  a  part

     of  a  children's  mobile  which  is  a  composite  node,  it   will  call  the  touch()  opera@on.  This  will  cascade  the  touch()  opera@on  on   only  the  children  of  that  object.