SlideShare uma empresa Scribd logo
1 de 41
Baixar para ler offline
Your First 5 PHP Design Patterns
           by 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
Aaron Saray
                         Open Source Developer
                         Milwaukee, WI
Questions?
                             http://aaronsaray.com

● Questions about            @aaronsaray
  PHP Design Patterns?


                         Milwaukee PHP Users Group
                         http://mkepug.org
                         @mkepug

Mais conteúdo relacionado

Mais procurados

Clean code, Feb 2012
Clean code, Feb 2012Clean code, Feb 2012
Clean code, Feb 2012
cobyst
 

Mais procurados (20)

Presentatie xPage & Beer
Presentatie xPage & BeerPresentatie xPage & Beer
Presentatie xPage & Beer
 
How to write bad code using C#
How to write bad code using C#How to write bad code using C#
How to write bad code using C#
 
Clean code coding like a professional
Clean code   coding like a professionalClean code   coding like a professional
Clean code coding like a professional
 
Prototyping Workshop - Wireframes, Mockups, Prototypes
Prototyping Workshop - Wireframes, Mockups, PrototypesPrototyping Workshop - Wireframes, Mockups, Prototypes
Prototyping Workshop - Wireframes, Mockups, Prototypes
 
Java interfaces design perspective
Java interfaces design perspectiveJava interfaces design perspective
Java interfaces design perspective
 
BDD with Behat
BDD with BehatBDD with Behat
BDD with Behat
 
Behat - human-readable automated testing
Behat - human-readable automated testingBehat - human-readable automated testing
Behat - human-readable automated testing
 
Boston Startup School - OO Design
Boston Startup School - OO DesignBoston Startup School - OO Design
Boston Startup School - OO Design
 
Webdev bootcamp
Webdev bootcampWebdev bootcamp
Webdev bootcamp
 
Language portfolio
Language portfolioLanguage portfolio
Language portfolio
 
Web application
Web applicationWeb application
Web application
 
Geecon10: Object Oriented for nonbelievers
Geecon10: Object Oriented for nonbelieversGeecon10: Object Oriented for nonbelievers
Geecon10: Object Oriented for nonbelievers
 
Clean code, Feb 2012
Clean code, Feb 2012Clean code, Feb 2012
Clean code, Feb 2012
 
Templates: Where Should Its Elements Be? In the Template or on Another Module?
Templates: Where Should Its Elements Be? In the Template or on Another Module?Templates: Where Should Its Elements Be? In the Template or on Another Module?
Templates: Where Should Its Elements Be? In the Template or on Another Module?
 
Javascript 01 (js)
Javascript 01 (js)Javascript 01 (js)
Javascript 01 (js)
 
Metaprogramming Go
Metaprogramming GoMetaprogramming Go
Metaprogramming Go
 
Web development basics (Part-3)
Web development basics (Part-3)Web development basics (Part-3)
Web development basics (Part-3)
 
Jak aspekty uporządkują twój kod.
Jak aspekty uporządkują twój kod.Jak aspekty uporządkują twój kod.
Jak aspekty uporządkują twój kod.
 
Visual testing using PhantomCss
Visual testing using PhantomCssVisual testing using PhantomCss
Visual testing using PhantomCss
 
Php traits
Php traitsPhp traits
Php traits
 

Destaque

Design patterns revisited with PHP 5.3
Design patterns revisited with PHP 5.3Design patterns revisited with PHP 5.3
Design patterns revisited with PHP 5.3
Fabien Potencier
 
Zend PHP 5.3 Demo Certification Test
Zend PHP 5.3 Demo Certification TestZend PHP 5.3 Demo Certification Test
Zend PHP 5.3 Demo Certification Test
Carlos Buenosvinos
 
Top 100 PHP Interview Questions and Answers
Top 100 PHP Interview Questions and AnswersTop 100 PHP Interview Questions and Answers
Top 100 PHP Interview Questions and Answers
Vineet Kumar Saini
 

Destaque (17)

25 php interview questions – codementor
25 php interview questions – codementor25 php interview questions – codementor
25 php interview questions – codementor
 
Design patterns in PHP
Design patterns in PHPDesign patterns in PHP
Design patterns in PHP
 
Design patterns revisited with PHP 5.3
Design patterns revisited with PHP 5.3Design patterns revisited with PHP 5.3
Design patterns revisited with PHP 5.3
 
Object Oriented Design Patterns for PHP
Object Oriented Design Patterns for PHPObject Oriented Design Patterns for PHP
Object Oriented Design Patterns for PHP
 
Design Patterns & JDK Examples
Design Patterns & JDK ExamplesDesign Patterns & JDK Examples
Design Patterns & JDK Examples
 
PHP client - Mongo db User Group Pune
PHP client - Mongo db User Group PunePHP client - Mongo db User Group Pune
PHP client - Mongo db User Group Pune
 
Zend Php Certification Study Guide
Zend Php Certification Study GuideZend Php Certification Study Guide
Zend Php Certification Study Guide
 
Zend PHP 5.3 Demo Certification Test
Zend PHP 5.3 Demo Certification TestZend PHP 5.3 Demo Certification Test
Zend PHP 5.3 Demo Certification Test
 
Introduction to Design Patterns and Singleton
Introduction to Design Patterns and SingletonIntroduction to Design Patterns and Singleton
Introduction to Design Patterns and Singleton
 
Practice exam php
Practice exam phpPractice exam php
Practice exam php
 
1000+ php questions
1000+ php questions1000+ php questions
1000+ php questions
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)
 
Top 100 PHP Interview Questions and Answers
Top 100 PHP Interview Questions and AnswersTop 100 PHP Interview Questions and Answers
Top 100 PHP Interview Questions and Answers
 
Business analysis interview question and answers
Business analysis interview question and answersBusiness analysis interview question and answers
Business analysis interview question and answers
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
 
85 business analyst interview questions and answers
85 business analyst interview questions and answers85 business analyst interview questions and answers
85 business analyst interview questions and answers
 
Basic design & visual arts (Elements of design)
Basic design & visual arts (Elements of design)Basic design & visual arts (Elements of design)
Basic design & visual arts (Elements of design)
 

Semelhante a Your first 5 PHP design patterns - ThatConference 2012

Everyone hacks design at a hackathon
Everyone hacks   design at a hackathonEveryone hacks   design at a hackathon
Everyone hacks design at a hackathon
Willow Brugh
 
Bdd agile requirements
Bdd agile requirementsBdd agile requirements
Bdd agile requirements
Agile Vietnam
 
Beginner's Guide to Frontend Development: Comparing Angular, React, Ember, an...
Beginner's Guide to Frontend Development: Comparing Angular, React, Ember, an...Beginner's Guide to Frontend Development: Comparing Angular, React, Ember, an...
Beginner's Guide to Frontend Development: Comparing Angular, React, Ember, an...
Prasid Pathak
 

Semelhante a Your first 5 PHP design patterns - ThatConference 2012 (20)

Xlab #2: wzorce projektowe
Xlab #2: wzorce projektoweXlab #2: wzorce projektowe
Xlab #2: wzorce projektowe
 
Everyone hacks design at a hackathon
Everyone hacks   design at a hackathonEveryone hacks   design at a hackathon
Everyone hacks design at a hackathon
 
Bdd agile requirements
Bdd agile requirementsBdd agile requirements
Bdd agile requirements
 
Meetup 18/10/2018 - Artificiële intelligentie en mobiliteit
Meetup 18/10/2018 - Artificiële intelligentie en mobiliteitMeetup 18/10/2018 - Artificiële intelligentie en mobiliteit
Meetup 18/10/2018 - Artificiële intelligentie en mobiliteit
 
Beginner's Guide to Frontend Development: Comparing Angular, React, Ember, an...
Beginner's Guide to Frontend Development: Comparing Angular, React, Ember, an...Beginner's Guide to Frontend Development: Comparing Angular, React, Ember, an...
Beginner's Guide to Frontend Development: Comparing Angular, React, Ember, an...
 
Create your library
Create your libraryCreate your library
Create your library
 
Professional development
Professional developmentProfessional development
Professional development
 
Fundamentals of App & Web Design
Fundamentals of App & Web DesignFundamentals of App & Web Design
Fundamentals of App & Web Design
 
Deep Learning Applications (dadada2017)
Deep Learning Applications (dadada2017)Deep Learning Applications (dadada2017)
Deep Learning Applications (dadada2017)
 
Coding dojo
Coding dojoCoding dojo
Coding dojo
 
2024.04 - AI in Code Generation - April User Group Meeting
2024.04 - AI in Code Generation - April User Group Meeting2024.04 - AI in Code Generation - April User Group Meeting
2024.04 - AI in Code Generation - April User Group Meeting
 
WordCamp Milwaukee 2012 - Aaron Saray - Secure Wordpress Coding
WordCamp Milwaukee 2012 - Aaron Saray - Secure Wordpress CodingWordCamp Milwaukee 2012 - Aaron Saray - Secure Wordpress Coding
WordCamp Milwaukee 2012 - Aaron Saray - Secure Wordpress Coding
 
Getting started contributing to Apache Spark
Getting started contributing to Apache SparkGetting started contributing to Apache Spark
Getting started contributing to Apache Spark
 
Open source, What | Why | How
Open source, What | Why | How Open source, What | Why | How
Open source, What | Why | How
 
Software Engineering Primer
Software Engineering PrimerSoftware Engineering Primer
Software Engineering Primer
 
Failing the coding interview
Failing the coding interviewFailing the coding interview
Failing the coding interview
 
Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...
Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...
Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...
 
Why no one is looking for rockstar programmers (updated version)
Why no one is looking for rockstar programmers (updated version)Why no one is looking for rockstar programmers (updated version)
Why no one is looking for rockstar programmers (updated version)
 
Design of everyday things fundamental principles of interaction - V2
Design of everyday things   fundamental principles of interaction - V2Design of everyday things   fundamental principles of interaction - V2
Design of everyday things fundamental principles of interaction - V2
 
Sharpening the Axe
Sharpening the AxeSharpening the Axe
Sharpening the Axe
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

Your first 5 PHP design patterns - ThatConference 2012

  • 1. Your First 5 PHP Design Patterns by Aaron Saray
  • 2.
  • 3. Why trust this guy? ● 20 years of programming ● MKEPUG ● I wrote the book on this topic ● ... you paid! ;)
  • 4. Obligatory Slide ● Web Development Manager ○ Liturgical Publications, Inc.
  • 5. 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
  • 7. 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?
  • 8. Software Design has Three Parts ● "What" ○ business and functionality ● "How" ○ which design you choose ● "Work" ○ implementation - or using "how"
  • 9. Where do Design Patterns fall?
  • 10. Design Patterns are the "How" They make the "work" less sucky.
  • 11. 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
  • 12. The Godfathers ● Gang of Four ○ Erich Gamma ○ Richard Helm ○ Ralph Johnson ○ John Vlissides
  • 13.
  • 14. What Design Patterns are NOT ● Not Plug n Play code ● You can't blindly copy these ● Unproven theory ○ Design Laws?
  • 15. 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
  • 17. #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
  • 18. #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.
  • 21. or... with Abstracts too... There are many ways to skin a cat... wait.. what? Poor cat...
  • 24. #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
  • 27. #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
  • 32. #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
  • 36. #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
  • 40. 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
  • 41. Aaron Saray Open Source Developer Milwaukee, WI Questions? http://aaronsaray.com ● Questions about @aaronsaray PHP Design Patterns? Milwaukee PHP Users Group http://mkepug.org @mkepug