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.

Common design patterns in php

1,195 views

Published on

A quick overview of some common design patterns in PHP

Published in: Technology
  • Be the first to comment

Common design patterns in php

  1. 1. Common PHP Design Patterns David Stockton Front Range PHP User Group - Feb 18, 2015
  2. 2. What are design patterns?
  3. 3. Common Language
  4. 4. Gang of Four Cartman, Kyle, Stan, Kenny
  5. 5. Gang of Four Gamma, Helm, Johnson, Vlissides
  6. 6. What aren’t design patterns?
  7. 7. Silver Bullets
  8. 8. Legos for Coding
  9. 9. Scratch is Legos for Coding This talk is not about scratch
  10. 10. Design Patterns Help You Talk to Other Coders
  11. 11. Hey, I’m building a chunk of code that is used to build an object which has a lot of dependencies and setup and each of those has a few of their own so this piece of code will take care of putting together all those pieces so that when I need the object I just have to ask this one object to give me the other one and then I can use it where ever I need instead of having to build a huge dependency tree before using this code…
  12. 12. LOL WUT?
  13. 13. I’m building a factory.
  14. 14. What does a factory do? • Builds objects • Encapsulates building of objects so there’s only one place to find how to build it
  15. 15. Singleton
  16. 16. Singleton There can be only one
  17. 17. Singleton • For classes where we want only one instance
  18. 18. Downsides of Singleton • It is a global • Hard to test • Better ways to do this
  19. 19. Another pattern description • I’m going to create a shopping cart where I can choose how the discounts or promo codes will be applied by injecting various objects, each of which can include how to apply these discounts; for example apply discounts only to items that don’t have other discounts, apply discount percentage on the base price not counting already applied discounts, apply discount percentage after other applied discounts, apply a flat amount of a discount for each item, apply a discount on an item based on what type of item it is, etc
  20. 20. Strategy pattern • I’m going to create a shopping cart where I can choose how the discounts or promo codes will be applied by injecting various objects, each of which can include how to apply these discounts; for example apply discounts only to items that don’t have other discounts, apply discount percentage on the base price not counting already applied discounts, apply discount percentage after other applied discounts, apply a flat amount of a discount for each item, apply a discount on an item based on what type of item it is, etc
  21. 21. Strategy pattern • Allow injection of object that provides some aspect of the functionality • Algorithm’s functionality can be decided at run-time • Examples: • usort - Provide comparison part of sort • Car - brakingStrategy
  22. 22. Command Pattern • I’m doing a bunch of stuff that I want to be able to keep track of and potentially save and replay later, possibly be able to undo. In order for this to work my object will have to capture everything it needs to know about as far as parameters and what-not in order to be able to run later as well as potentially capturing state so I can get back to where I was before.
  23. 23. Command Pattern • Uses: • Recording Macros • Multi-level Undo • Progress Bars • Transactional Behavior • Wizards
  24. 24. Chain of Responsibility • Each object can take care of something (or not), then it passes to the next object
  25. 25. Chain of Responsibility Examples • Loggers - set next logger in chain, each calls the next when it is done • Managers with purchasing power - if amount is above manager’s level, pass to the next object in the chain
  26. 26. Observer Pattern • Notify interested objects of state changes in subject • Interested objects subscribe to subject • When subject changes, it calls update on all observers
  27. 27. MVC - Model View Controller • Organizational pattern for web dev • View code contains output • Model code is for data/business logic • Controller links model and view code
  28. 28. Front Controller • Central entry point for handling requests for web application • Route all calls through index.php, then figure out what to do with it
  29. 29. Adapter Pattern • Allow interface for an existing class to be used from another interface
  30. 30. Facade Pattern • Provide simplified access to a complex system
  31. 31. Other Patterns • Bridge • Composite • Decorator • Front Controller • Blackboard • Iterator • Mediator • Memento • Null Object • Specification • State • Template Method • Visitor • Abstract Factory • Prototype

×