Date post: | 16-Jul-2015 |
Category: |
Technology |
Upload: | david-stockton |
View: | 167 times |
Download: | 5 times |
Common PHP Design Patterns
David Stockton Front Range PHP User Group - Feb 18, 2015
What are design patterns?
Common Language
Gang of FourCartman, Kyle, Stan, Kenny
Gang of FourGamma, Helm, Johnson, Vlissides
What aren’t design patterns?
Silver Bullets
Legos for Coding
Scratch is Legos for Coding
This talk is not about scratch
Design Patterns Help You Talk to
Other Coders
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…
LOL WUT?
I’m building a factory.
What does a factory do?
• Builds objects
• Encapsulates building of objects so there’s only one place to find how to build it
Singleton
SingletonThere can be only one
Singleton
• For classes where we want only one instance
Downsides of Singleton
• It is a global
• Hard to test
• Better ways to do this
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
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
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
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.
Command Pattern
• Uses:
• Recording Macros
• Multi-level Undo
• Progress Bars
• Transactional Behavior
• Wizards
Chain of Responsibility
• Each object can take care of something (or not), then it passes to the next object
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
Observer Pattern
• Notify interested objects of state changes in subject
• Interested objects subscribe to subject
• When subject changes, it calls update on all observers
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
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
Adapter Pattern
• Allow interface for an existing class to be used from another interface
Facade Pattern
• Provide simplified access to a complex system
Other Patterns• Bridge
• Composite
• Decorator
• Front Controller
• Blackboard
• Iterator
• Mediator
• Memento
• Null Object
• Specification
• State
• Template Method
• Visitor
• Abstract Factory
• Prototype