common design patterns in php
TRANSCRIPT
![Page 1: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/1.jpg)
Common PHP Design Patterns
David Stockton Front Range PHP User Group - Feb 18, 2015
![Page 2: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/2.jpg)
What are design patterns?
![Page 3: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/3.jpg)
Common Language
![Page 4: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/4.jpg)
Gang of FourCartman, Kyle, Stan, Kenny
![Page 5: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/5.jpg)
Gang of FourGamma, Helm, Johnson, Vlissides
![Page 6: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/6.jpg)
What aren’t design patterns?
![Page 7: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/7.jpg)
Silver Bullets
![Page 8: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/8.jpg)
Legos for Coding
![Page 9: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/9.jpg)
Scratch is Legos for Coding
This talk is not about scratch
![Page 10: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/10.jpg)
Design Patterns Help You Talk to
Other Coders
![Page 11: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/11.jpg)
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…
![Page 12: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/12.jpg)
LOL WUT?
![Page 13: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/13.jpg)
I’m building a factory.
![Page 14: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/14.jpg)
What does a factory do?
• Builds objects
• Encapsulates building of objects so there’s only one place to find how to build it
![Page 15: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/15.jpg)
![Page 16: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/16.jpg)
Singleton
![Page 17: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/17.jpg)
SingletonThere can be only one
![Page 18: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/18.jpg)
Singleton
• For classes where we want only one instance
![Page 19: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/19.jpg)
![Page 20: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/20.jpg)
Downsides of Singleton
• It is a global
• Hard to test
• Better ways to do this
![Page 21: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/21.jpg)
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
![Page 22: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/22.jpg)
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
![Page 23: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/23.jpg)
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
![Page 24: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/24.jpg)
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.
![Page 25: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/25.jpg)
Command Pattern
• Uses:
• Recording Macros
• Multi-level Undo
• Progress Bars
• Transactional Behavior
• Wizards
![Page 26: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/26.jpg)
Chain of Responsibility
• Each object can take care of something (or not), then it passes to the next object
![Page 27: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/27.jpg)
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
![Page 28: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/28.jpg)
Observer Pattern
• Notify interested objects of state changes in subject
• Interested objects subscribe to subject
• When subject changes, it calls update on all observers
![Page 29: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/29.jpg)
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
![Page 30: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/30.jpg)
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
![Page 31: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/31.jpg)
Adapter Pattern
• Allow interface for an existing class to be used from another interface
![Page 32: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/32.jpg)
Facade Pattern
• Provide simplified access to a complex system
![Page 33: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/33.jpg)
Other Patterns• Bridge
• Composite
• Decorator
• Front Controller
• Blackboard
• Iterator
• Mediator
• Memento
• Null Object
• Specification
• State
• Template Method
• Visitor
• Abstract Factory
• Prototype
![Page 34: Common design patterns in php](https://reader033.vdocument.in/reader033/viewer/2022042701/55a7779c1a28ab530a8b46e3/html5/thumbnails/34.jpg)