Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Your first 5 PHP design patterns - ThatConference 2012


Published on

Slides from the ThatConference talk I gave on PHP Design patterns. .net and Ruby developers were also there and chimed in. :)

Published in: Technology
  • Follow the link, new dating source: ♥♥♥ ♥♥♥
    Are you sure you want to  Yes  No
    Your message goes here
  • Sex in your area is here: ♥♥♥ ♥♥♥
    Are you sure you want to  Yes  No
    Your message goes here

Your first 5 PHP design patterns - ThatConference 2012

  1. 1. Your First 5 PHP Design Patterns by Aaron Saray
  2. 2. Why trust this guy? ● 20 years of programming ● MKEPUG ● I wrote the book on this topic ● ... you paid! ;)
  3. 3. Obligatory Slide● Web Development Manager ○ Liturgical Publications, Inc.
  4. 4. What were here to do...● Talk about what Design Patterns are ○ you dont need a comp-sci phd● Show you where youve seen them before● Introduce 5 design patterns ○ real-people-talk descriptions ○ code samples● Q&A
  5. 5. What are Patterns?
  6. 6. What are Design Patterns?● Solving the same problem over and over ○ but the right way ○ the old guys got it right● Why is this important to PHP programmers? ○ PHP is easy to get into ○ We think well save the world, or own it? ■ FB is worth billions - and is PHP right?
  7. 7. Software Design has Three Parts● "What" ○ business and functionality● "How" ○ which design you choose● "Work" ○ implementation - or using "how"
  8. 8. Where do Design Patterns fall?
  9. 9. Design Patterns are the "How" They make the "work" less sucky.
  10. 10. This isnt new...● Non-PHP Programmers have seen this before ○ I vaguely remember something from college JAVA● PHP Guys, youve seen this before ○ PEAR DB ○ Zend Registry ○ Zend URI Factory ○ Doctrine DAO
  11. 11. The Godfathers● Gang of Four ○ Erich Gamma ○ Richard Helm ○ Ralph Johnson ○ John Vlissides
  12. 12. What Design Patterns are NOT● Not Plug n Play code● You cant blindly copy these● Unproven theory ○ Design Laws?
  13. 13. So, before we go on...● Solving the same problem over and over ○ correctly● Language agnostic ○ but were going to focus on PHP● These are not new - youve used them● GoF● Not Copy and Paste
  14. 14. Lets do this!
  15. 15. #1 - Singleton Pattern(purists, stop groaning)● The Singleton Design Pattern is used to restrict the number of times a specific object can be created to a single time by providing access to a shared instance of itself.● Why might you use this? ○ Db ○ Heavy Front Build ○ Static Content
  16. 16. #1 continued● Sometimes 1 instance isnt right... ○ could restrict to 5 instances ■ pool?● Was someone next to you groaning? ○ "A proper registry of objects do away with this pattern. Mrrr mRrrr mrrr! Rabble Rabble!" ○ programmers arent responsible.
  17. 17. Singleton Code
  18. 18. Singleton Code
  19. 19. or... with Abstracts too...There are many ways to skin a cat...wait.. what? Poor cat...
  20. 20. Singleton Code
  21. 21. Singleton Code
  22. 22. #2 - Factory Pattern● The Factory Design Pattern provides a simple interface to acquire a new instance of an object while sheltering the calling code from the steps to determine which base class is actually instantiated.● Why might we use this? ○ Inventory system dealing with types of objects ○ View system requesting objects ○ Youtube vs Vimeo URL
  23. 23. Factory Code
  24. 24. Factory Code
  25. 25. #3 - Observer Pattern● The Observer Design pattern facilitates the creation of objects that watch the state of a targeted functionality that is uncoupled from the core object.● Why might we use this ○ Plugins ○ Dont want to modify shared code ○ Licensing ○ Enable / Disable auxiliary functions
  26. 26. Observer Code
  27. 27. Observer Code
  28. 28. Observer Code
  29. 29. Observer Code
  30. 30. #4 - Decorator Pattern● The Decorator Design Pattern is best suited for altering or "decorating" portions of an existing objects content or functionality without modifying the structure of the original object.● Why might we use this? ○ quick small changes to internal content / values ○ modify user input (filter) ○ pretty output
  31. 31. Decorator Code
  32. 32. Decorator Code
  33. 33. Decorator Code
  34. 34. #5 - Strategy Pattern● The Strategy Design Pattern helps architect an object that can make use of algorithms in other objects on demand in lieu of containing the logic itself.● Why might we use this? ○ reduce code duplication on similar models ○ no re-inventing the wheel ○ quickly add a different process without changing the base object ■ Ex: YouTube changes the URL format
  35. 35. Strategy Code
  36. 36. Strategy Code
  37. 37. Strategy Code
  38. 38. Whats next?● Put this into practice● Refactor your code / Plan new code● Learn more design patterns: ○ GoF: ○ Me:
  39. 39. Aaron Saray Open Source Developer Milwaukee, WIQuestions?● Questions about @aaronsaray PHP Design Patterns? Milwaukee PHP Users Group @mkepug