0
Design	
  Pa*erns	
  
addi-onal	
  material	
  
A	
  design	
  pa*ern	
  is	
  …	
  
•  …	
  a	
  ba*le-­‐tested	
  solu-on	
  to	
  a	
  known	
  design	
  
problem.	
  ...
An	
  example	
  
•  Carpenters	
  making	
  a	
  drawer.	
  
•  How	
  should	
  they	
  build	
  it?	
  
•  Should	
  we...
Two	
  solu-ons	
  
•  “We	
  could	
  cut	
  each	
  at	
  45	
  degrees	
  and	
  nail	
  or	
  
glue	
  the	
  facing	
...
Miter	
  Joint	
  
• 
• 
• 
• 

Easy	
  
Quick	
  
Cheap	
  
Invisible	
  
Dovetail	
  joint	
  
Very	
  complicated!	
  
Time	
  consuming	
  
Extremely	
  strong	
  
Requires	
  no	
  glue	
  or	...
Gang	
  of	
  Four	
  

•  Coined	
  “Design	
  
Pa*erns”	
  
•  Erich	
  Gamma	
  
•  Richard	
  Helm	
  
•  Ralph	
  Joh...
GOF	
  Principles	
  
Design	
  to	
  interfaces,	
  not	
  implementa-on.	
  
Favor	
  composi-on	
  over	
  inheritance....
Crea-onal	
  Pa*erns	
  
•  Create	
  objects	
  at	
  run-me	
  so	
  that	
  their	
  
characteris-cs	
  can	
  be	
  cu...
Factory	
  Method	
  Pa*ern	
  
•  Define	
  an	
  interface	
  for	
  crea-ng	
  an	
  object,	
  but	
  
let	
  subclasse...
Singleton	
  Pa*ern	
  
•  Ensure	
  a	
  class	
  has	
  only	
  one	
  instance	
  and	
  
provide	
  a	
  global	
  poi...
Structural	
  Pa*erns	
  
•  Cover	
  class	
  and	
  object	
  composi-on.	
  
•  Structural	
  class-­‐crea-on	
  pa*ern...
Adapter	
  Pa*ern	
  
•  Convert	
  the	
  interface	
  of	
  a	
  class	
  into	
  
another	
  interface	
  clients	
  ex...
Composite	
  Pa*ern	
  
•  Compose	
  objects	
  into	
  
tree	
  structures	
  to	
  
represent	
  part-­‐whole	
  
hiera...
Decorator	
  Pa*ern	
  
•  A*ach	
  addi-onal	
  
responsibili-es	
  to	
  an	
  
object	
  dynamically.	
  	
  
•  Decora...
Flyweight	
  Pa*ern	
  
•  Use	
  sharing	
  to	
  support	
  large	
  numbers	
  of	
  fine-­‐
grained	
  objects	
  efficie...
Behavioral	
  Pa*erns	
  
•  Cover	
  a	
  class's	
  objects	
  communica-on.	
  
•  Concerned	
  with	
  communica-on	
 ...
Mediator	
  Pa*ern	
  
•  Encapsulates	
  how	
  a	
  set	
  of	
  objects	
  interact.	
  	
  
•  Loose	
  coupling	
  by...
Observer	
  Pa*ern	
  
•  Define	
  a	
  one-­‐to-­‐many	
  
dependency	
  between	
  
objects	
  so	
  that	
  when	
  
on...
State	
  Pa*ern	
  
•  Allow	
  an	
  object	
  to	
  alter	
  its	
  behavior	
  when	
  its	
  
internal	
  state	
  cha...
Example:	
  Inspector	
  App	
  
•  Say	
  you’ve	
  created	
  a	
  program	
  that	
  keeps	
  track	
  of	
  
inspec-on...
The	
  Class	
  
class LeadInspector {
public LeadInspector(int personId) {
// Constructor: create the lead inspector here...
How	
  it	
  is	
  created	
  
public static void main() {
//Make inspector 75 the lead inspector.
LeadInsp insLead = new ...
Whoops!	
  
•  But	
  what	
  if	
  we	
  accidentally	
  create	
  another	
  
lead	
  inspector	
  elsewhere?	
  
public...
Got	
  to	
  prevent	
  that!	
  
•  We’ll	
  change	
  the	
  way	
  our	
  LeadInspector	
  class	
  
provides	
  access...
New	
  instance-­‐ge*er	
  
class LeadInspector {
private static LeadInspector oneLead;
private LeadInspector(int personId...
All	
  is	
  well!	
  
•  Now,	
  even	
  if	
  we	
  (or	
  someone	
  else)	
  tries	
  to	
  
instan-ate	
  another	
  ...
Recommended	
  Reading	
  
•  Head	
  First	
  Design	
  
Pa*erns	
  
•  By	
  Freeman	
  &	
  
Freeman	
  
•  O’Reilly	
 ...
Upcoming SlideShare
Loading in...5
×

Design patterns

262

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
262
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
10
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Design patterns"

  1. 1. Design  Pa*erns   addi-onal  material  
  2. 2. 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  __________.  
  3. 3. 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?  
  4. 4. 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  …”  
  5. 5. Miter  Joint   •  •  •  •  Easy   Quick   Cheap   Invisible  
  6. 6. Dovetail  joint   Very  complicated!   Time  consuming   Extremely  strong   Requires  no  glue  or   nails   •  Looks  beau-ful   •  •  •  • 
  7. 7. Gang  of  Four   •  Coined  “Design   Pa*erns”   •  Erich  Gamma   •  Richard  Helm   •  Ralph  Johnson   •  John  Vlissides  
  8. 8. 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.   •  •  •  • 
  9. 9. 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  
  10. 10. 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.  
  11. 11. Singleton  Pa*ern   •  Ensure  a  class  has  only  one  instance  and   provide  a  global  point  of  access  to  it.  
  12. 12. 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.  
  13. 13. 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.  
  14. 14. 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.  
  15. 15. 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.    
  16. 16. Flyweight  Pa*ern   •  Use  sharing  to  support  large  numbers  of  fine-­‐ grained  objects  efficiently.    
  17. 17. Behavioral  Pa*erns   •  Cover  a  class's  objects  communica-on.   •  Concerned  with  communica-on  between   objects.  
  18. 18. 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.  
  19. 19. 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.  
  20. 20. State  Pa*ern   •  Allow  an  object  to  alter  its  behavior  when  its   internal  state  changes.     •  The  object  will  appear  to  change  its  class.  
  21. 21. 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.  
  22. 22. 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 }
  23. 23. 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. }
  24. 24. 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); }
  25. 25. 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.  
  26. 26. 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 }
  27. 27. 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!  
  28. 28. Recommended  Reading   •  Head  First  Design   Pa*erns   •  By  Freeman  &   Freeman   •  O’Reilly  &   Associates   •  ISBN  0-­‐596-­‐00712-­‐4  
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×