Design patterns

28
Design Pa*erns addi-onal material

description

 

Transcript of Design patterns

Page 1: Design patterns

Design  Pa*erns  

addi-onal  material  

Page 2: Design patterns

A  design  pa*ern  is  …  

•  …  a  ba*le-­‐tested  solu-on  to  a  known  design  problem.  

•  Allows  for  fast,  reliable  solu-ons.  •  Allows  quicker,  more  precise  __________.  •  Allows  a  higher  level  of  __________.  

Page 3: Design patterns

An  example  

•  Carpenters  making  a  drawer.  •  How  should  they  build  it?  •  Should  we  invent  a  way  to  put  the  drawer  together?  

•  Perhaps  others  have  solved  the  problem  before?  

•  How?  

Page 4: Design patterns

Two  solu-ons  

•  “We  could  cut  each  at  45  degrees  and  nail  or  glue  the  facing  pieces  together.”  

•  “Maybe  we  could  make  the  joint  by  cuNng  straight  down  into  the  wood,  and  then  cut  backup  45  degrees,  and  then  going  straight  back  down,  and  then  back  up  the  other  way  45  degrees,  and  then  going  straight  back  down,  and  then  …”  

Page 5: Design patterns

Miter  Joint  

•  Easy  •  Quick  •  Cheap  •  Invisible  

Page 6: Design patterns

Dovetail  joint  

•  Very  complicated!  •  Time  consuming  •  Extremely  strong  •  Requires  no  glue  or  nails  

•  Looks  beau-ful  

Page 7: Design patterns

Gang  of  Four  

•  Coined  “Design  Pa*erns”  

•  Erich  Gamma  •  Richard  Helm  •  Ralph  Johnson  •  John  Vlissides  

Page 8: Design patterns

GOF  Principles  

•  Design  to  interfaces,  not  implementa-on.  •  Favor  composi-on  over  inheritance.  •  Find  what  varies  and  encapsulate  it.  •  Strive  for  loosely  coupled  designs  between  objects  that  interact.  

•  Classes  should  be  open  for  extension  but  closed  for  modifica-on.  

•  Depend  on  abstrac-on.    Do  not  depend  on  concrete  classes.  

Page 9: Design patterns

Crea-onal  Pa*erns  

•  Create  objects  at  run-me  so  that  their  characteris-cs  can  be  customized  to  the  environment.  

•  Class-­‐crea-on  pa*erns    – Use  inheritance  effec-vely  in  the  instan-a-on  process.  

•  Object-­‐crea-on  pa*erns    – Use  delega-on  effec-vely  to  get  the  job  done  

Page 10: Design patterns

Factory  Method  Pa*ern  

•  Define  an  interface  for  crea-ng  an  object,  but  let  subclasses  decide  which  class  to  instan-ate.    

•  Factory  Method  lets  a  class  defer  instan-a-on  to  subclasses.  

Page 11: Design patterns

Singleton  Pa*ern  

•  Ensure  a  class  has  only  one  instance  and  provide  a  global  point  of  access  to  it.  

Page 12: Design patterns

Structural  Pa*erns  

•  Cover  class  and  object  composi-on.  •  Structural  class-­‐crea-on  pa*erns  use  inheritance  to  compose  interfaces.  

•  Structural  object-­‐pa*erns  define  ways  to  compose  objects  to  obtain  new  func-onality.  

Page 13: Design patterns

Adapter  Pa*ern  •  Convert  the  interface  of  a  class  into  another  interface  clients  expect.  

•  Lets  classes  work  together  that  couldn't  otherwise  because  of  incompa-ble  interfaces.  

Page 14: Design patterns

Composite  Pa*ern  

•  Compose  objects  into  tree  structures  to  represent  part-­‐whole  hierarchies.    

•  Composite  lets  clients  treat  individual  objects  and  composi-ons  of  objects  uniformly.  

Page 15: Design patterns

Decorator  Pa*ern  •  A*ach  addi-onal  responsibili-es  to  an  object  dynamically.    

•  Decorators  provide  a  flexible  alterna-ve  to  subclassing  for  extending  func-onality.    

Page 16: Design patterns

Flyweight  Pa*ern  

•  Use  sharing  to  support  large  numbers  of  fine-­‐grained  objects  efficiently.    

Page 17: Design patterns

Behavioral  Pa*erns  

•  Cover  a  class's  objects  communica-on.  •  Concerned  with  communica-on  between  objects.  

Page 18: Design patterns

Mediator  Pa*ern  •  Encapsulates  how  a  set  of  objects  interact.    •  Loose  coupling  by  keeping  objects  from  referring  to  each  other  explicitly,  and  it  lets  you  vary  their  interac-on  independently.  

Page 19: Design patterns

Observer  Pa*ern  

•  Define  a  one-­‐to-­‐many  dependency  between  objects  so  that  when  one  object  changes  state,  all  its  dependents  are  no-fied  and  updated  automa-cally.  

Page 20: Design patterns

State  Pa*ern  

•  Allow  an  object  to  alter  its  behavior  when  its  internal  state  changes.    

•  The  object  will  appear  to  change  its  class.  

Page 21: Design patterns

Example:  Inspector  App  

•  Say  you’ve  created  a  program  that  keeps  track  of  inspec-on  results.      

•  There  may  be  several  inspectors  on  the  inspec-on.  •  You  choose  a  lead  inspector  and  zero  or  more  helper  inspectors.  

Page 22: Design patterns

The  Class  class LeadInspector {

public LeadInspector(int personId) { // Constructor: create the lead inspector here

// by reading the person info from a DB.

}

// More methods here

}

Page 23: Design patterns

How  it  is  created  

public static void main() {

//Make inspector 75 the lead inspector. LeadInsp insLead = new LeadInsp(75);

HelpInsp helper1 = new HelpInsp(79);

HelpInsp helper2 = new HelpInsp(82);

//Do other inspector things below.

}

Page 24: Design patterns

Whoops!  

•  But  what  if  we  accidentally  create  another  lead  inspector  elsewhere?  

public static void main() { //Make inspector 75 the lead inspector.

LeadInsp insLead = new LeadInsp(75);

HelpInsp helper1 = new HelpInsp(79);

HelpInsp helper2 = new HelpInsp(82);

//Do other inspector things below.

}

Public void anotherMethod() {

LeadInsp insLeader = new LeadInsp(77); }

Page 25: Design patterns

Got  to  prevent  that!  

•  We’ll  change  the  way  our  LeadInspector  class  provides  access  to  itself.  

•  Make  the  constructor  private  so  it  can  only  be  called  internally.  

•  Provide  another  method  to  provide  an  instance.  

•  If  we’ve  already  created  an  instance,  provide  a  pointer  to  it  instead  of  making  a  new  one.  

Page 26: Design patterns

New  instance-­‐ge*er  

class LeadInspector { private static LeadInspector oneLead; private LeadInspector(int personId){

// Constructor: create the lead inspector here.

}

public LeadInspector getInstance(int personId){

if (oneLead == null) {

oneLead = new LeadInspector(int personId);

}

return oneLead;

}

// More code here

}

Page 27: Design patterns

All  is  well!  

•  Now,  even  if  we  (or  someone  else)  tries  to  instan-ate  another  copy  of  our  object,  we  can  provide  them  with  the  one  and  only  one  instance  of  it.  

•  This  is  called  the  __________  pa*ern!  

Page 28: Design patterns

Recommended  Reading  

•  Head  First  Design  Pa*erns  

•  By  Freeman  &  Freeman  

•  O’Reilly  &  Associates  

•  ISBN  0-­‐596-­‐00712-­‐4