Refactoring to Design Patterns (phpDay 2009)

54
Refactoring to Design Pa0erns Simone Carle5 phpDay 2009 – May 15‐16, 2009 – Verona, Italy h0p://grusp.it ‐ h0p://phpday.it ‐ h0p://joind.in/event/view/34

description

 

Transcript of Refactoring to Design Patterns (phpDay 2009)

Page 1: Refactoring to Design Patterns (phpDay 2009)

Refactoring to Design Pa0erns Simone Carle5 

phpDay 2009 – May 15‐16, 2009 – Verona, Italy h0p://grusp.it ‐ h0p://phpday.it ‐ h0p://joind.in/event/view/34 

Page 2: Refactoring to Design Patterns (phpDay 2009)

About Me 

•  Technical Manager in Altura Labs •  Addicted to Agile Development 

•  Fallen in love with Development Best Prac=ces 

•  Passionate about Object Oriented Programming 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 3: Refactoring to Design Patterns (phpDay 2009)

Designing object oriented soKware is hard, and designing reusable object‐oriented soKware is 

even harder. 

Design Pa*erns (GoF) 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 4: Refactoring to Design Patterns (phpDay 2009)

EvoluRon 

Page 5: Refactoring to Design Patterns (phpDay 2009)

Maintainability 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 6: Refactoring to Design Patterns (phpDay 2009)

Maintainability 

Long project lifecycle 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 7: Refactoring to Design Patterns (phpDay 2009)

Maintainability 

Long project lifecycle 

Scalability 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 8: Refactoring to Design Patterns (phpDay 2009)

Maintainability 

Long project lifecycle 

Scalability 

Flexibility 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 9: Refactoring to Design Patterns (phpDay 2009)

… and we need it for yesterday! 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 10: Refactoring to Design Patterns (phpDay 2009)

Design for Changes 

The key to maximizing reuse lies in an=cipa=ng new requirements and changes to exis=ng 

requirements, and design your system so that they can evolve accordingly. 

Design Pa*erns (GoF) 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 11: Refactoring to Design Patterns (phpDay 2009)
Page 12: Refactoring to Design Patterns (phpDay 2009)

Bad Smell 

hVp://www.codinghorror.com/blog/archives/000589.html 

Page 13: Refactoring to Design Patterns (phpDay 2009)

D.R.Y. 

Don’t repeat yourself 

Page 14: Refactoring to Design Patterns (phpDay 2009)

K.I.S.S. 

Keep it Simple, Stupid 

Page 15: Refactoring to Design Patterns (phpDay 2009)

Y.A.G.N.I. 

You Ain't Gonna Need It  

Page 16: Refactoring to Design Patterns (phpDay 2009)

Over‐Engineering 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 17: Refactoring to Design Patterns (phpDay 2009)

Under‐Engineering 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 18: Refactoring to Design Patterns (phpDay 2009)

Refactoring 

Page 19: Refactoring to Design Patterns (phpDay 2009)

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 20: Refactoring to Design Patterns (phpDay 2009)

What is Refactoring? 

Refactoring is a disciplined technique for restructuring an exis=ng body of code, altering 

its internal structure without changing its external behavior. 

Mar2n Fowler 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 21: Refactoring to Design Patterns (phpDay 2009)

Why? 

•  Improve readability •  Improve maintainability 

•  Gain a beVer understanding of code 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 22: Refactoring to Design Patterns (phpDay 2009)

How? 

•  Small steps •  Con=nuous •  Don’t add features •  … or bugs •  Have a good knowledge of the code 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 23: Refactoring to Design Patterns (phpDay 2009)

Design Pa0erns 

Page 24: Refactoring to Design Patterns (phpDay 2009)

Design PaVerns describe simple and elegant solu=ons to specific problems in object 

oriented soKware design. 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 25: Refactoring to Design Patterns (phpDay 2009)

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 26: Refactoring to Design Patterns (phpDay 2009)

Each paVerns describes a problem which occurs over and over again in our environment, and then describes the core of the solu=on to that problem, in such a way that you can use this 

solu=on a million =mes over. 

Christopher Alexander 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 27: Refactoring to Design Patterns (phpDay 2009)

Context 

Problem 

Solu=on 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 28: Refactoring to Design Patterns (phpDay 2009)

A word of warning 

Ideally when the appropriate problem comes along, you should trigger the design paVern 

and your problem is solved. 

Russ Olsen, Design Pa*erns in Ruby 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 29: Refactoring to Design Patterns (phpDay 2009)

Design Pa0erns by Purpose 

•  Crea=onal PaVerns •  Structural PaVerns •  Behavioral PaVerns 

•  Concurrency PaVerns •  Architectural PaVerns 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 30: Refactoring to Design Patterns (phpDay 2009)

There is a natural rela=on  between paVerns and refactorings.  

PaVerns are where you want to be,  

refactorings are ways to get there  

from somewhere else. 

Mar2n Fowler 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 31: Refactoring to Design Patterns (phpDay 2009)

TesRng 

Page 32: Refactoring to Design Patterns (phpDay 2009)

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 33: Refactoring to Design Patterns (phpDay 2009)

TesRng 

performance 

integra=on 

func=onal 

unit 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 34: Refactoring to Design Patterns (phpDay 2009)

Test Driven Development 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 35: Refactoring to Design Patterns (phpDay 2009)

Tes=ng 

Refactoring 

Design PaVerns 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 36: Refactoring to Design Patterns (phpDay 2009)

Refactoring to Design Pa0erns 

Page 37: Refactoring to Design Patterns (phpDay 2009)

Observer 

Page 38: Refactoring to Design Patterns (phpDay 2009)

Observer 

The Observer paVern defines dependency between objects so that when one object change state, all its dependents are no=fied 

and updated automa=cally 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 39: Refactoring to Design Patterns (phpDay 2009)

Observer 

Post 

CacheSweeper  Logger EmailNo=fier 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 40: Refactoring to Design Patterns (phpDay 2009)

Observer 

Post 

CacheSweeper  Logger EmailNo=fier 

$post‐>=tle = “foo”; $post‐>save(); 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 41: Refactoring to Design Patterns (phpDay 2009)

Observer 

Post 

CacheSweeper  Logger EmailNo=fier 

$post‐>=tle = “foo”; $post‐>save(); 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 42: Refactoring to Design Patterns (phpDay 2009)

Observer 

Post 

CacheSweeper  Logger EmailNo=fier 

$post‐>=tle = “foo”; $post‐>save(); 

aKer_save()  aKer_save()  aKer_save() 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 43: Refactoring to Design Patterns (phpDay 2009)

Observer 

Post 

CacheSweeper  Logger EmailNo=fier 

$post‐>delete(); 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 44: Refactoring to Design Patterns (phpDay 2009)

Observer 

Post 

CacheSweeper  Logger EmailNo=fier 

$post‐>delete(); 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 45: Refactoring to Design Patterns (phpDay 2009)

Observer 

Post 

CacheSweeper  Logger EmailNo=fier 

aKer_delete()  aKer_delete() 

$post‐>delete(); 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 46: Refactoring to Design Patterns (phpDay 2009)

Code Time 

Page 47: Refactoring to Design Patterns (phpDay 2009)

Strategy 

Page 48: Refactoring to Design Patterns (phpDay 2009)

Strategy 

function doSomething($even, $odd){ if ($even == "secret") { if ($odd == "other secret") { return 1; } else if ("pizza" == "good") { return "Yes it is!"; } } else if ($odd != $even) { return "yeah, they are"; } else { return "I'm getting crazy"; }} 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 49: Refactoring to Design Patterns (phpDay 2009)

Strategy 

method($params){

// complex logic// conditional strategy

}

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 50: Refactoring to Design Patterns (phpDay 2009)

Strategy 

Strategy 1  Strategy 2  Strategy 3 

method($params){

$s = new Strategy();$s->method($params);

}

$s = new Strategy()

$s->method($params);

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 51: Refactoring to Design Patterns (phpDay 2009)

Code Time 

Page 52: Refactoring to Design Patterns (phpDay 2009)

QuesRons? 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 53: Refactoring to Design Patterns (phpDay 2009)

License This presenta=on is published under the AVribu=on‐Share Alike Unported Crea=ve Commons License. 

You are free: 

•  to Share — to copy, distribute and transmit the work 

•  to Remix — to adapt the work 

Under the following condi=ons: 

•  A0ribuRon — You must aVribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). 

•  Share Alike — If you alter, transform, or build upon this work, you may distribute the resul=ng work only under the same, similar or a compa=ble license. 

Visit hVp://crea=vecommons.org/licenses/by‐sa/3.0/ for more details. 

phpDay 2009 – 15 e 16 Maggio  Verona, Italy 

Page 54: Refactoring to Design Patterns (phpDay 2009)

Thank you! 

Slides will be available at www.simonecarleq.com 

www.slideshare.net/weppos 

Rate this talk at 

hVp://joind.in/talk/view/417