Your first 5 PHP design patterns - ThatConference 2012

Preview:

DESCRIPTION

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

Citation preview

Your First 5 PHP Design Patternsby Aaron Saray

Why trust this guy?

● 20 years of programming

● MKEPUG

● I wrote the book on this topic

● ... you paid! ;)

Obligatory Slide

● Web Development Manager○ Liturgical Publications, Inc.

What we're here to do...

● Talk about what Design Patterns are○ you don't need a comp-sci phd

● Show you where you've seen them before

● Introduce 5 design patterns○ real-people-talk descriptions○ code samples

● Q&A

What are Patterns?

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 we'll save the world, or own it?■ FB is worth billions - and is PHP right?

Software Design has Three Parts

● "What"○ business and functionality

● "How"○ which design you choose

● "Work"○ implementation - or using "how"

Where do Design Patterns fall?

Design Patterns are the "How"

They make the "work" less sucky.

This isn't new...

● Non-PHP Programmers have seen this before○ I vaguely remember something from college JAVA

● PHP Guys, you've seen this before○ PEAR DB○ Zend Registry○ Zend URI Factory○ Doctrine DAO

The Godfathers

● Gang of Four○ Erich Gamma○ Richard Helm○ Ralph Johnson○ John Vlissides

What Design Patterns are NOT

● Not Plug n Play code

● You can't blindly copy these

● Unproven theory○ Design Laws?

So, before we go on...

● Solving the same problem over and over○ correctly

● Language agnostic○ but we're going to focus on PHP

● These are not new - you've used them

● GoF

● Not Copy and Paste

Let's do this!

#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

#1 continued

● Sometimes 1 instance isn't 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 aren't responsible.

Singleton Code

Singleton Code

or... with Abstracts too...There are many ways to skin a cat...

wait.. what? Poor cat...

Singleton Code

Singleton Code

#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

Factory Code

Factory Code

#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○ Don't want to modify shared code○ Licensing○ Enable / Disable auxiliary functions

Observer Code

Observer Code

Observer Code

Observer Code

#4 - Decorator Pattern

● The Decorator Design Pattern is best suited for altering or "decorating" portions of an existing object's 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

Decorator Code

Decorator Code

Decorator Code

#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

Strategy Code

Strategy Code

Strategy Code

What's next?

● Put this into practice

● Refactor your code / Plan new code

● Learn more design patterns:○ GoF: http://saray.me/KqqHm1○ Me: http://saray.me/JIP98B

Questions?

● Questions about PHP Design Patterns?

Aaron SarayOpen Source DeveloperMilwaukee, WI

http://aaronsaray.com

@aaronsaray

Milwaukee PHP Users Grouphttp://mkepug.org@mkepug

Recommended