Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

EvoluRon

Slide 5

Slide 5 text

Maintainability phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 6

Slide 6 text

Maintainability Long project lifecycle phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 7

Slide 7 text

Maintainability Long project lifecycle Scalability phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 8

Slide 8 text

Maintainability Long project lifecycle Scalability Flexibility phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 9

Slide 9 text

… and we need it for yesterday! phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

D.R.Y. Don’t repeat yourself

Slide 14

Slide 14 text

K.I.S.S. Keep it Simple, Stupid

Slide 15

Slide 15 text

Y.A.G.N.I. You Ain't Gonna Need It

Slide 16

Slide 16 text

Over‐Engineering phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 17

Slide 17 text

Under‐Engineering phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 18

Slide 18 text

Refactoring

Slide 19

Slide 19 text

phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Why? •  Improve readability •  Improve maintainability •  Gain a beVer understanding of code phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Design Pa0erns

Slide 24

Slide 24 text

Design PaVerns describe simple and elegant solu=ons to specific problems in object oriented soKware design. phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 25

Slide 25 text

phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Context Problem Solu=on phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

Design Pa0erns by Purpose •  Crea=onal PaVerns •  Structural PaVerns •  Behavioral PaVerns •  Concurrency PaVerns •  Architectural PaVerns phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

TesRng

Slide 32

Slide 32 text

phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 33

Slide 33 text

TesRng performance integra=on func=onal unit phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 34

Slide 34 text

Test Driven Development phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 35

Slide 35 text

Tes=ng Refactoring Design PaVerns phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 36

Slide 36 text

Refactoring to Design Pa0erns

Slide 37

Slide 37 text

Observer

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

Observer Post CacheSweeper Logger EmailNo=fier phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 40

Slide 40 text

Observer Post CacheSweeper Logger EmailNo=fier $post‐>=tle = “foo”; $post‐>save(); phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 41

Slide 41 text

Observer Post CacheSweeper Logger EmailNo=fier $post‐>=tle = “foo”; $post‐>save(); phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

Observer Post CacheSweeper Logger EmailNo=fier $post‐>delete(); phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 44

Slide 44 text

Observer Post CacheSweeper Logger EmailNo=fier $post‐>delete(); phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 45

Slide 45 text

Observer Post CacheSweeper Logger EmailNo=fier aKer_delete() aKer_delete() $post‐>delete(); phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 46

Slide 46 text

Code Time

Slide 47

Slide 47 text

Strategy

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

Strategy method($params) { // complex logic // conditional strategy } phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

Code Time

Slide 52

Slide 52 text

QuesRons? phpDay 2009 – 15 e 16 Maggio Verona, Italy

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

Thank you! Slides will be available at www.simonecarleq.com www.slideshare.net/weppos Rate this talk at hVp://joind.in/talk/view/417