SlideShare una empresa de Scribd logo
1 de 34
Descargar para leer sin conexión
Common PHP Design
Patterns
David Stockton
Front Range PHP User Group - Feb 18, 2015
What are design patterns?
Common Language
Gang of Four
Cartman, Kyle, Stan, Kenny
Gang of Four
Gamma, 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
Singleton
There 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
Common PHP Design Patterns Explained

Más contenido relacionado

La actualidad más candente

How To Become A Good C# Programmer
How To Become A Good C# ProgrammerHow To Become A Good C# Programmer
How To Become A Good C# ProgrammerLearnItFirst.com
 
Clean Code summary
Clean Code summaryClean Code summary
Clean Code summaryJan de Vries
 
Coding standards and guidelines
Coding standards and guidelinesCoding standards and guidelines
Coding standards and guidelinesbrijraj_singh
 
Clean code, Feb 2012
Clean code, Feb 2012Clean code, Feb 2012
Clean code, Feb 2012cobyst
 
Standard coding practices
Standard coding practicesStandard coding practices
Standard coding practicesAnilkumar Patil
 
Coding standards for java
Coding standards for javaCoding standards for java
Coding standards for javamaheshm1206
 
Best Practices of Software Development
Best Practices of Software DevelopmentBest Practices of Software Development
Best Practices of Software DevelopmentFolio3 Software
 
Reactive GUI Implemented in Clojure
Reactive GUI Implemented in ClojureReactive GUI Implemented in Clojure
Reactive GUI Implemented in Clojuredenyslebediev
 
Geecon10: Object Oriented for nonbelievers
Geecon10: Object Oriented for nonbelieversGeecon10: Object Oriented for nonbelievers
Geecon10: Object Oriented for nonbelieversBruno Bossola
 
Writing code for others
Writing code for othersWriting code for others
Writing code for othersAmol Pujari
 
Design pattern in an expressive language java script
Design pattern in an expressive language java scriptDesign pattern in an expressive language java script
Design pattern in an expressive language java scriptAmit Thakkar
 
Introduction to Frontend Development - Session 1 - HTML Fundamentals
Introduction to Frontend Development - Session 1 - HTML FundamentalsIntroduction to Frontend Development - Session 1 - HTML Fundamentals
Introduction to Frontend Development - Session 1 - HTML FundamentalsKalin Chernev
 
21o. RubyFloripa - Maintaining legacy Rails app and introducing Elixir
21o. RubyFloripa - Maintaining legacy Rails app and introducing Elixir21o. RubyFloripa - Maintaining legacy Rails app and introducing Elixir
21o. RubyFloripa - Maintaining legacy Rails app and introducing ElixirWeverton Timoteo
 

La actualidad más candente (20)

How To Become A Good C# Programmer
How To Become A Good C# ProgrammerHow To Become A Good C# Programmer
How To Become A Good C# Programmer
 
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
 
Clean Code summary
Clean Code summaryClean Code summary
Clean Code summary
 
Coding standards and guidelines
Coding standards and guidelinesCoding standards and guidelines
Coding standards and guidelines
 
Clean code, Feb 2012
Clean code, Feb 2012Clean code, Feb 2012
Clean code, Feb 2012
 
Coding standard
Coding standardCoding standard
Coding standard
 
Standard coding practices
Standard coding practicesStandard coding practices
Standard coding practices
 
Coding standards for java
Coding standards for javaCoding standards for java
Coding standards for java
 
Best Practices of Software Development
Best Practices of Software DevelopmentBest Practices of Software Development
Best Practices of Software Development
 
Reactive GUI Implemented in Clojure
Reactive GUI Implemented in ClojureReactive GUI Implemented in Clojure
Reactive GUI Implemented in Clojure
 
CLEAN CODE
CLEAN CODECLEAN CODE
CLEAN CODE
 
Geecon10: Object Oriented for nonbelievers
Geecon10: Object Oriented for nonbelieversGeecon10: Object Oriented for nonbelievers
Geecon10: Object Oriented for nonbelievers
 
Software development fundamentals
Software development fundamentalsSoftware development fundamentals
Software development fundamentals
 
Writing code for others
Writing code for othersWriting code for others
Writing code for others
 
Clean Code
Clean CodeClean Code
Clean Code
 
Design pattern in an expressive language java script
Design pattern in an expressive language java scriptDesign pattern in an expressive language java script
Design pattern in an expressive language java script
 
Introduction to Frontend Development - Session 1 - HTML Fundamentals
Introduction to Frontend Development - Session 1 - HTML FundamentalsIntroduction to Frontend Development - Session 1 - HTML Fundamentals
Introduction to Frontend Development - Session 1 - HTML Fundamentals
 
Php
PhpPhp
Php
 
21o. RubyFloripa - Maintaining legacy Rails app and introducing Elixir
21o. RubyFloripa - Maintaining legacy Rails app and introducing Elixir21o. RubyFloripa - Maintaining legacy Rails app and introducing Elixir
21o. RubyFloripa - Maintaining legacy Rails app and introducing Elixir
 

Destacado

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.3Fabien Potencier
 
Object Oriented Design Patterns for PHP
Object Oriented Design Patterns for PHPObject Oriented Design Patterns for PHP
Object Oriented Design Patterns for PHPRobertGonzalez
 
Introduction to Design Patterns and Singleton
Introduction to Design Patterns and SingletonIntroduction to Design Patterns and Singleton
Introduction to Design Patterns and SingletonJonathan Simon
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)paramisoft
 
Design Patterns & JDK Examples
Design Patterns & JDK ExamplesDesign Patterns & JDK Examples
Design Patterns & JDK ExamplesEnder Aydin Orak
 

Destacado (6)

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
 
Introduction to Design Patterns and Singleton
Introduction to Design Patterns and SingletonIntroduction to Design Patterns and Singleton
Introduction to Design Patterns and Singleton
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)
 
Design Patterns & JDK Examples
Design Patterns & JDK ExamplesDesign Patterns & JDK Examples
Design Patterns & JDK Examples
 

Similar a Common PHP Design Patterns Explained

Segue to design patterns
Segue to design patternsSegue to design patterns
Segue to design patternsRahul Singh
 
Weekly Meeting: Basic Design Pattern
Weekly Meeting: Basic Design PatternWeekly Meeting: Basic Design Pattern
Weekly Meeting: Basic Design PatternNguyen Trung Kien
 
Driving application development through behavior driven development
Driving application development through behavior driven developmentDriving application development through behavior driven development
Driving application development through behavior driven developmentEinar Ingebrigtsen
 
Kaseya Connect 2013: Templates and Policy: The Next Steps
Kaseya Connect 2013: Templates and Policy: The Next StepsKaseya Connect 2013: Templates and Policy: The Next Steps
Kaseya Connect 2013: Templates and Policy: The Next StepsKaseya
 
Principled And Clean Coding
Principled And Clean CodingPrincipled And Clean Coding
Principled And Clean CodingMetin Ogurlu
 
Episode 3 – Classes, Inheritance, Abstract Class, and Interfaces
Episode 3 – Classes, Inheritance, Abstract Class, and InterfacesEpisode 3 – Classes, Inheritance, Abstract Class, and Interfaces
Episode 3 – Classes, Inheritance, Abstract Class, and InterfacesJitendra Zaa
 
How to crack java script certification
How to crack java script certificationHow to crack java script certification
How to crack java script certificationKadharBashaJ
 
Unit Testing and role of Test doubles
Unit Testing and role of Test doublesUnit Testing and role of Test doubles
Unit Testing and role of Test doublesRitesh Mehrotra
 
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...AgileNetwork
 
An Introduction To Software Development - Final Review
An Introduction To Software Development - Final ReviewAn Introduction To Software Development - Final Review
An Introduction To Software Development - Final ReviewBlue Elephant Consulting
 
Create Your Own Starter Files
Create Your Own Starter FilesCreate Your Own Starter Files
Create Your Own Starter FilesEmily Lewis
 
Refactoring Legacy Code - true story
Refactoring Legacy Code - true storyRefactoring Legacy Code - true story
Refactoring Legacy Code - true storyAki Salmi
 
Best practices with development of enterprise-scale SharePoint solutions - Pa...
Best practices with development of enterprise-scale SharePoint solutions - Pa...Best practices with development of enterprise-scale SharePoint solutions - Pa...
Best practices with development of enterprise-scale SharePoint solutions - Pa...SPC Adriatics
 

Similar a Common PHP Design Patterns Explained (20)

Design p atterns
Design p atternsDesign p atterns
Design p atterns
 
Software Design principales
Software Design principalesSoftware Design principales
Software Design principales
 
Orchestration, the conductor's score
Orchestration, the conductor's scoreOrchestration, the conductor's score
Orchestration, the conductor's score
 
Segue to design patterns
Segue to design patternsSegue to design patterns
Segue to design patterns
 
Weekly Meeting: Basic Design Pattern
Weekly Meeting: Basic Design PatternWeekly Meeting: Basic Design Pattern
Weekly Meeting: Basic Design Pattern
 
Driving application development through behavior driven development
Driving application development through behavior driven developmentDriving application development through behavior driven development
Driving application development through behavior driven development
 
Kaseya Connect 2013: Templates and Policy: The Next Steps
Kaseya Connect 2013: Templates and Policy: The Next StepsKaseya Connect 2013: Templates and Policy: The Next Steps
Kaseya Connect 2013: Templates and Policy: The Next Steps
 
Principled And Clean Coding
Principled And Clean CodingPrincipled And Clean Coding
Principled And Clean Coding
 
Episode 3 – Classes, Inheritance, Abstract Class, and Interfaces
Episode 3 – Classes, Inheritance, Abstract Class, and InterfacesEpisode 3 – Classes, Inheritance, Abstract Class, and Interfaces
Episode 3 – Classes, Inheritance, Abstract Class, and Interfaces
 
How to crack java script certification
How to crack java script certificationHow to crack java script certification
How to crack java script certification
 
Unit Testing and role of Test doubles
Unit Testing and role of Test doublesUnit Testing and role of Test doubles
Unit Testing and role of Test doubles
 
Clean code
Clean codeClean code
Clean code
 
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
 
An Introduction To Software Development - Final Review
An Introduction To Software Development - Final ReviewAn Introduction To Software Development - Final Review
An Introduction To Software Development - Final Review
 
jForce In Action
jForce In ActionjForce In Action
jForce In Action
 
Create Your Own Starter Files
Create Your Own Starter FilesCreate Your Own Starter Files
Create Your Own Starter Files
 
CPAN Curation
CPAN CurationCPAN Curation
CPAN Curation
 
Refactoring Legacy Code - true story
Refactoring Legacy Code - true storyRefactoring Legacy Code - true story
Refactoring Legacy Code - true story
 
Best practices with development of enterprise-scale SharePoint solutions - Pa...
Best practices with development of enterprise-scale SharePoint solutions - Pa...Best practices with development of enterprise-scale SharePoint solutions - Pa...
Best practices with development of enterprise-scale SharePoint solutions - Pa...
 
Software Design
Software DesignSoftware Design
Software Design
 

Más de David Stockton

Phone calls and sms from php
Phone calls and sms from phpPhone calls and sms from php
Phone calls and sms from phpDavid Stockton
 
The Art of Transduction
The Art of TransductionThe Art of Transduction
The Art of TransductionDavid Stockton
 
Using queues and offline processing to help speed up your application
Using queues and offline processing to help speed up your applicationUsing queues and offline processing to help speed up your application
Using queues and offline processing to help speed up your applicationDavid Stockton
 
Intermediate OOP in PHP
Intermediate OOP in PHPIntermediate OOP in PHP
Intermediate OOP in PHPDavid Stockton
 
Building APIs with Apigilty and Zend Framework 2
Building APIs with Apigilty and Zend Framework 2Building APIs with Apigilty and Zend Framework 2
Building APIs with Apigilty and Zend Framework 2David Stockton
 
Intermediate OOP in PHP
Intermediate OOP in PHPIntermediate OOP in PHP
Intermediate OOP in PHPDavid Stockton
 
Hacking sites for fun and profit
Hacking sites for fun and profitHacking sites for fun and profit
Hacking sites for fun and profitDavid Stockton
 
Intermediate oop in php
Intermediate oop in phpIntermediate oop in php
Intermediate oop in phpDavid Stockton
 
Hacking sites for fun and profit
Hacking sites for fun and profitHacking sites for fun and profit
Hacking sites for fun and profitDavid Stockton
 
Hacking sites for fun and profit
Hacking sites for fun and profitHacking sites for fun and profit
Hacking sites for fun and profitDavid Stockton
 
Increasing code quality with code reviews (poetry version)
Increasing code quality with code reviews (poetry version)Increasing code quality with code reviews (poetry version)
Increasing code quality with code reviews (poetry version)David Stockton
 
Tame Your Build And Deployment Process With Hudson, PHPUnit, and SSH
Tame Your Build And Deployment Process With Hudson, PHPUnit, and SSHTame Your Build And Deployment Process With Hudson, PHPUnit, and SSH
Tame Your Build And Deployment Process With Hudson, PHPUnit, and SSHDavid Stockton
 
Mercurial Distributed Version Control
Mercurial Distributed Version ControlMercurial Distributed Version Control
Mercurial Distributed Version ControlDavid Stockton
 
Regular expressions and php
Regular expressions and phpRegular expressions and php
Regular expressions and phpDavid Stockton
 

Más de David Stockton (19)

Phone calls and sms from php
Phone calls and sms from phpPhone calls and sms from php
Phone calls and sms from php
 
The Art of Transduction
The Art of TransductionThe Art of Transduction
The Art of Transduction
 
Using queues and offline processing to help speed up your application
Using queues and offline processing to help speed up your applicationUsing queues and offline processing to help speed up your application
Using queues and offline processing to help speed up your application
 
Intermediate OOP in PHP
Intermediate OOP in PHPIntermediate OOP in PHP
Intermediate OOP in PHP
 
Building APIs with Apigilty and Zend Framework 2
Building APIs with Apigilty and Zend Framework 2Building APIs with Apigilty and Zend Framework 2
Building APIs with Apigilty and Zend Framework 2
 
API All the Things!
API All the Things!API All the Things!
API All the Things!
 
Intermediate OOP in PHP
Intermediate OOP in PHPIntermediate OOP in PHP
Intermediate OOP in PHP
 
Hacking sites for fun and profit
Hacking sites for fun and profitHacking sites for fun and profit
Hacking sites for fun and profit
 
Beginning OOP in PHP
Beginning OOP in PHPBeginning OOP in PHP
Beginning OOP in PHP
 
Intermediate oop in php
Intermediate oop in phpIntermediate oop in php
Intermediate oop in php
 
Grokking regex
Grokking regexGrokking regex
Grokking regex
 
Hacking sites for fun and profit
Hacking sites for fun and profitHacking sites for fun and profit
Hacking sites for fun and profit
 
Hacking sites for fun and profit
Hacking sites for fun and profitHacking sites for fun and profit
Hacking sites for fun and profit
 
Increasing code quality with code reviews (poetry version)
Increasing code quality with code reviews (poetry version)Increasing code quality with code reviews (poetry version)
Increasing code quality with code reviews (poetry version)
 
Tame Your Build And Deployment Process With Hudson, PHPUnit, and SSH
Tame Your Build And Deployment Process With Hudson, PHPUnit, and SSHTame Your Build And Deployment Process With Hudson, PHPUnit, and SSH
Tame Your Build And Deployment Process With Hudson, PHPUnit, and SSH
 
Mercurial Distributed Version Control
Mercurial Distributed Version ControlMercurial Distributed Version Control
Mercurial Distributed Version Control
 
Regular expressions and php
Regular expressions and phpRegular expressions and php
Regular expressions and php
 
PHP 5 Magic Methods
PHP 5 Magic MethodsPHP 5 Magic Methods
PHP 5 Magic Methods
 
FireBug And FirePHP
FireBug And FirePHPFireBug And FirePHP
FireBug And FirePHP
 

Último

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 

Último (20)

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 

Common PHP Design Patterns Explained

  • 1. Common PHP Design Patterns David Stockton Front Range PHP User Group - Feb 18, 2015
  • 2. What are design patterns?
  • 4. Gang of Four Cartman, Kyle, Stan, Kenny
  • 5. Gang of Four Gamma, Helm, Johnson, Vlissides
  • 9. Scratch is Legos for Coding This talk is not about scratch
  • 10. Design Patterns Help You Talk to Other Coders
  • 11. 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…
  • 13. I’m building a factory.
  • 14. What does a factory do? • Builds objects • Encapsulates building of objects so there’s only one place to find how to build it
  • 15.
  • 18. Singleton • For classes where we want only one instance
  • 19.
  • 20. Downsides of Singleton • It is a global • Hard to test • Better ways to do this
  • 21. 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
  • 22. 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
  • 23. 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
  • 24. 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.
  • 25. Command Pattern • Uses: • Recording Macros • Multi-level Undo • Progress Bars • Transactional Behavior • Wizards
  • 26. Chain of Responsibility • Each object can take care of something (or not), then it passes to the next object
  • 27. 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
  • 28. Observer Pattern • Notify interested objects of state changes in subject • Interested objects subscribe to subject • When subject changes, it calls update on all observers
  • 29. 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
  • 30. 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
  • 31. Adapter Pattern • Allow interface for an existing class to be used from another interface
  • 32. Facade Pattern • Provide simplified access to a complex system
  • 33. Other Patterns • Bridge • Composite • Decorator • Front Controller • Blackboard • Iterator • Mediator • Memento • Null Object • Specification • State • Template Method • Visitor • Abstract Factory • Prototype