41
Your First 5 PHP Design Patterns by Aaron Saray

Your first 5 PHP design patterns - ThatConference 2012

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Your first 5 PHP design patterns - ThatConference 2012

Your First 5 PHP Design Patternsby Aaron Saray

Page 2: Your first 5 PHP design patterns - ThatConference 2012
Page 3: Your first 5 PHP design patterns - ThatConference 2012

Why trust this guy?

● 20 years of programming

● MKEPUG

● I wrote the book on this topic

● ... you paid! ;)

Page 4: Your first 5 PHP design patterns - ThatConference 2012

Obligatory Slide

● Web Development Manager○ Liturgical Publications, Inc.

Page 5: Your first 5 PHP design patterns - ThatConference 2012

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

Page 6: Your first 5 PHP design patterns - ThatConference 2012

What are Patterns?

Page 7: Your first 5 PHP design patterns - ThatConference 2012

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?

Page 8: Your first 5 PHP design patterns - ThatConference 2012

Software Design has Three Parts

● "What"○ business and functionality

● "How"○ which design you choose

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

Page 9: Your first 5 PHP design patterns - ThatConference 2012

Where do Design Patterns fall?

Page 10: Your first 5 PHP design patterns - ThatConference 2012

Design Patterns are the "How"

They make the "work" less sucky.

Page 11: Your first 5 PHP design patterns - ThatConference 2012

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

Page 12: Your first 5 PHP design patterns - ThatConference 2012

The Godfathers

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

Page 13: Your first 5 PHP design patterns - ThatConference 2012
Page 14: Your first 5 PHP design patterns - ThatConference 2012

What Design Patterns are NOT

● Not Plug n Play code

● You can't blindly copy these

● Unproven theory○ Design Laws?

Page 15: Your first 5 PHP design patterns - ThatConference 2012

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

Page 16: Your first 5 PHP design patterns - ThatConference 2012

Let's do this!

Page 17: Your first 5 PHP design patterns - ThatConference 2012

#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

Page 18: Your first 5 PHP design patterns - ThatConference 2012

#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.

Page 19: Your first 5 PHP design patterns - ThatConference 2012

Singleton Code

Page 20: Your first 5 PHP design patterns - ThatConference 2012

Singleton Code

Page 21: Your first 5 PHP design patterns - ThatConference 2012

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

wait.. what? Poor cat...

Page 22: Your first 5 PHP design patterns - ThatConference 2012

Singleton Code

Page 23: Your first 5 PHP design patterns - ThatConference 2012

Singleton Code

Page 24: Your first 5 PHP design patterns - ThatConference 2012

#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

Page 25: Your first 5 PHP design patterns - ThatConference 2012

Factory Code

Page 26: Your first 5 PHP design patterns - ThatConference 2012

Factory Code

Page 27: Your first 5 PHP design patterns - ThatConference 2012

#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

Page 28: Your first 5 PHP design patterns - ThatConference 2012

Observer Code

Page 29: Your first 5 PHP design patterns - ThatConference 2012

Observer Code

Page 30: Your first 5 PHP design patterns - ThatConference 2012

Observer Code

Page 31: Your first 5 PHP design patterns - ThatConference 2012

Observer Code

Page 32: Your first 5 PHP design patterns - ThatConference 2012

#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

Page 33: Your first 5 PHP design patterns - ThatConference 2012

Decorator Code

Page 34: Your first 5 PHP design patterns - ThatConference 2012

Decorator Code

Page 35: Your first 5 PHP design patterns - ThatConference 2012

Decorator Code

Page 36: Your first 5 PHP design patterns - ThatConference 2012

#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

Page 37: Your first 5 PHP design patterns - ThatConference 2012

Strategy Code

Page 38: Your first 5 PHP design patterns - ThatConference 2012

Strategy Code

Page 39: Your first 5 PHP design patterns - ThatConference 2012

Strategy Code

Page 40: Your first 5 PHP design patterns - ThatConference 2012

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

Page 41: Your first 5 PHP design patterns - ThatConference 2012

Questions?

● Questions about PHP Design Patterns?

Aaron SarayOpen Source DeveloperMilwaukee, WI

http://aaronsaray.com

@aaronsaray

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