+ All Categories
Home > Technology > Common design patterns in php

Common design patterns in php

Date post: 16-Jul-2015
Category:
Upload: david-stockton
View: 167 times
Download: 5 times
Share this document with a friend
Popular Tags:
34
Common PHP Design Patterns David Stockton Front Range PHP User Group - Feb 18, 2015
Transcript
Page 1: Common design patterns in php

Common PHP Design Patterns

David Stockton Front Range PHP User Group - Feb 18, 2015

Page 2: Common design patterns in php

What are design patterns?

Page 3: Common design patterns in php

Common Language

Page 4: Common design patterns in php

Gang of FourCartman, Kyle, Stan, Kenny

Page 5: Common design patterns in php

Gang of FourGamma, Helm, Johnson, Vlissides

Page 6: Common design patterns in php

What aren’t design patterns?

Page 7: Common design patterns in php

Silver Bullets

Page 8: Common design patterns in php

Legos for Coding

Page 9: Common design patterns in php

Scratch is Legos for Coding

This talk is not about scratch

Page 10: Common design patterns in php

Design Patterns Help You Talk to

Other Coders

Page 11: Common design patterns in php

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

LOL WUT?

Page 13: Common design patterns in php

I’m building a factory.

Page 14: Common design patterns in php

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
Page 16: Common design patterns in php

Singleton

Page 17: Common design patterns in php

SingletonThere can be only one

Page 18: Common design patterns in php

Singleton

• For classes where we want only one instance

Page 19: Common design patterns in php
Page 20: Common design patterns in php

Downsides of Singleton

• It is a global

• Hard to test

• Better ways to do this

Page 21: Common design patterns in php

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

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

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

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

Command Pattern

• Uses:

• Recording Macros

• Multi-level Undo

• Progress Bars

• Transactional Behavior

• Wizards

Page 26: Common design patterns in php

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

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

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

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

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

Adapter Pattern

• Allow interface for an existing class to be used from another interface

Page 32: Common design patterns in php

Facade Pattern

• Provide simplified access to a complex system

Page 33: Common design patterns in php

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

Recommended