SlideShare a Scribd company logo
1 of 94
Download to read offline
Advanced PHP
               A touch of Design Patterns
                   Dennis-Jan Broerse - 13 juni 2008




Advanced PHP
Sources

   • Download the sources:
       http://www.ibuildings.nl/downloads/training
       username: dpc_2008
       password: dpc_2008



Advanced PHP
Introduction




   • First conference where I’m a speaker
Advanced PHP
Topics
   • Introduction to Design Patterns
   • Duck simulator
   • Weather application
   • Coffee bar
   • Traveller

Advanced PHP
Introduction to

               Design Patterns
   • Best-practise ‘design’ solution
   • Lessons learned by other developers
   • Based on the OO principles
   • Language in-depended
   • Puzzles for the creative mind of developer
Advanced PHP
Introduction to

               Design Patterns




Advanced PHP
Introduction to

               Design Patterns
               OO Basics

          * Abstraction
          * Encapsulation
          * Polymorphism
          * Inheritance




Advanced PHP
Introduction to

               Design Patterns
               OO Basics             OO Principles

          * Abstraction          * Encapsulate what varies
          * Encapsulation        * Favour composition
          * Polymorphism         over inheritance
          * Inheritance          * Program to interfaces,
                                 not implementations




Advanced PHP
Introduction to

               Design Patterns
   • It’s all about creating flexible designs that
       are maintainable and are able to deal with
       changes!
   • Don’t re-invent the wheel!
   • Load the design patterns into your brain
       and then design/build your application

Advanced PHP
Duck simulator




Advanced PHP
Duck simulator
               What we want


   • Creating as many ducks as needed
   • Ducks quack and swim


Advanced PHP
Duck simulator
               What we want

   • That’s easy!!
   • We can use INHERITANCE
   • Yes, we know our OO basics

Advanced PHP
Duck simulator
               What we want




Advanced PHP
Duck simulator
               What we want




Advanced PHP
Duck simulator
               First change

   • Our ducks have to fly!


   • No problem

Advanced PHP
Duck simulator
               First change

   • Our ducks have to fly!


   • No problem

Advanced PHP
Duck simulator
                     First problem




   • Rubber ducks don’t fly!
       (and I don’t mean throwing)




Advanced PHP
Duck simulator
                    First change




   • This can't be the solution
       (imagine what you have to do if you want to create a decoy duck)

Advanced PHP
Duck simulator
                    First change




   • This can't be the solution
       (imagine what you have to do if you want to create a decoy duck)

Advanced PHP
Duck simulator
               Cleaner solution

   • How about an interface?
   • Only implementing the interface if the duck
       is flyable
   • Hmm that’s it, that’s the solution

Advanced PHP
Duck simulator
               Cleaner solution




Advanced PHP
Duck simulator
               Cleaner solution




Advanced PHP
Duck simulator
               Think again
   • What happens if we have to change the fly
       behaviour?
   • Exactly, we have to change all instances!
   • And we duplicate code
   • Wouldn’t it be dreamy if we can change its
       behaviour at runtime?

Advanced PHP
Duck simulator
                Think again

   • Do you remember the OO basics and
       principles?
   • ‘Identify the aspects of your application that
       vary and separate them from what stays
       the same’


Advanced PHP
Duck simulator
               Last change




Advanced PHP
Duck simulator
               Last change




Advanced PHP
Duck simulator
                Benefits
   • Flexible
   • Maintainable
   • Extendable
   • No code duplication
   • Encapsulated
   • Change behaviour at runtime
Advanced PHP
Duck simulator
               Code sample


   • Code sample


Advanced PHP
Duck simulator
 Strategy Design Pattern
   • ‘The strategy pattern defines a family of
       algorithms, encapsulates each one, and
       makes them interchangeable. Strategy lets
       the algorithm vary independently from
       clients that use it.’
   • Do you remember the OO basics and
       principles?

Advanced PHP
Duck simulator
                    Guru


   • Now you’ll understand why you should
       know about design patterns!




Advanced PHP
Weather application




Advanced PHP
Weather application
               What we want




Advanced PHP
Weather application
               What we want

                                           Displays
               Pulls data
                            WheatherData
                               object




Advanced PHP
Weather application
               Requirements
   •   There are 3 get methods for temperature,
       humidity and pressure

   •   measurementsChanged method will be
       automatically called if the measurements are
       changed

   •   We have to implement the 3 display elements and
       they must be updated when measurements change


Advanced PHP
Weather application
      First implementation




Advanced PHP
Weather application
      First implementation




Advanced PHP
Weather application
      First implementation




Advanced PHP
Weather application
               What’s wrong

   •   What if we want to add a new display?




   •   Exactly: we have to change the code



Advanced PHP
Weather application
               What’s wrong

   •   Remember the OO basics and principles!

   •   Not flexible

   •   Not extendable

   •   No encapsulation



Advanced PHP
Weather application
                   Let’s look

   •   Investigate what’s going on

   •   What is the relationship between the weather
       object and the display elements?

   •   Maybe we can apply a design pattern?




Advanced PHP
Weather application
                  Let’s look
   •   The relationship looks like a newspaper
       subscription

   •   If you want to receive the newspaper, you have to
       subscribe yourself at the publisher

   •   The publisher will send you your newspaper every
       time until you unsubscribe


Advanced PHP
Weather application
                        So....

   •   The weather object is the publisher

   •   The display elements are the people who are
       willing to receive the newspaper




Advanced PHP
Weather application
                And that is...
   •   Yes, that is a design pattern!

   •   Called ‘The Observer Pattern’


   •   Publisher == Weather object == Subject

   •   People == Display elements == Observers


   •   Subject notifies every observer to update

Advanced PHP
Weather application
        Formal description

   •   ‘The Observer Pattern defines a one-to-many
       dependency between objects so that when one
       object changes state, all of its dependents are
       notified and updated automatically.’




Advanced PHP
Weather application
          The class diagram




Advanced PHP
Weather application
          The class diagram




Advanced PHP
Weather application
       New design principle
   •   Creating new observers won’t require a change in
       the subject

   •   Changes to either the subject or an observer will
       not affect the other

   •   Reusing a subject or an observer can
       independently of each other


Advanced PHP
Weather application
     New design principle




Advanced PHP
                        38
Weather application
     New design principle

                           OO Principles

  * Encapsulate what varies
  * Favour composition over inheritance
  * Program to interfaces, not implementations
  * Strive for loosely coupled designs between objects that interact




Advanced PHP
                                 38
Weather application
               New design




Advanced PHP
Weather application
               New design




Advanced PHP
Weather application
               New design


   • Code sample


Advanced PHP
Coffee bar




Advanced PHP
Coffee bar
               What we want

   • An application that calculates the price of
       different beverages
   • The flexibility to add new beverages

Advanced PHP
Coffee bar
               Simple solution




Advanced PHP
Coffee bar
               Simple solution




Advanced PHP
Coffee bar
               Condiments??


   • No problem!
   • We have an OO application; just inherit


Advanced PHP
Coffee bar
               First attempt




Advanced PHP
Coffee bar
               First attempt




Advanced PHP
Coffee bar
               First attempt

   • Euhm, this isn’t maintainable, this is a class
       explosion!!
   • Try again!!

Advanced PHP
Coffee bar
               OK, sorry




   • Well that’s better, wouldn’t you agree?
Advanced PHP
Coffee bar
               OK, sorry




   • Well that’s better, wouldn’t you agree?
Advanced PHP
Coffee bar
                     Really?

   • Did you really learn something?
   • Do you remember the OO basics and
       principles?



Advanced PHP
Coffee bar
     New design principle




Advanced PHP
                   49
Coffee bar
     New design principle
                           OO Principles

  * Encapsulate what varies
  * Favour composition over inheritance
  * Program to interfaces, not implementations
  * Strive for loosely coupled designs between objects that interact
  * Classes should be open for extension, but closed for
  modification




Advanced PHP
                                 49
Coffee bar
       What we really want

   •   We’d like to extend a beverage with condiments

   •   We wouldn’t like to change the beverage

   •   We’d like to add new beverages and condiments
       without changing the existing code




Advanced PHP
                            50
Coffee bar
       New design pattern

   •   The ‘Decorator Design Pattern’ saves our day


   •   ‘The decorator pattern attaches additional
       responsibilities to an object dynamically.
       Decorators provide a flexible alternative to
       subclassing for extending functionality.’



Advanced PHP
                             51
Coffee bar
               Our new design




Advanced PHP
                       52
Coffee bar
               Our new design




Advanced PHP
                       52
Coffee bar
               Our new design

   •   This is really great!

   •   Now we can say:
       One HouseBlend coffee with milk and mocha.
       What is the price?
       € 1,95



Advanced PHP
                               53
Coffee bar
        How does it work?

   •   Wrap the houseBlend object with Milk object and
       wrap that with a mocha object


   •   Ok, this is hard.
       Show me the code man.



Advanced PHP
                            54
Coffee bar
        How does it work?


   • Code sample


Advanced PHP
                       55
Traveller




Advanced PHP
Traveller
               What we want

   • An application which tells us if the
       conditions are suitable enough to make the
       trip




Advanced PHP
Traveller
               Wait a minute
   For example:

   • The traveller doesn’t want to make the trip
       if the average temperature of the
       destination is lower than his minimum
       required temperature



Advanced PHP
Traveller
      First implementation




   • What do you think?
Advanced PHP
Traveller
      First implementation




   • What do you think?
Advanced PHP
Traveller
      First implementation

   • Indeed, not very smart!
   • Not possible to add requirements
   • Requirements changes, so trip class has to
       be changed


Advanced PHP
Traveller
               Take that out




Advanced PHP
Traveller
               Take that out




Advanced PHP
Traveller
               That’s nice
   • We are able to create multiple
       specifications without changing other
       objects
   • Readable, maintainable, simple and effective
   • And it is a design pattern too!!
       ‘Hard-coded Specification Pattern’

Advanced PHP
Traveller
                    But...

   • The specification knows too much
   • Not able to make logical specifications
   • Does violate some OO principles
       You know them by now, right?


Advanced PHP
Traveller
     What we really want

   • A design which is flexible enough to
       validate almost every object
   • Validating against several conditions
   • Making logical expressions

Advanced PHP
Traveller
               Let’s redesign




Advanced PHP
Traveller
               Let’s redesign




Advanced PHP
Traveller
          Wow, that’s heavy


   • Code sample


Advanced PHP
Traveller
                This is ...
   • ‘The Specification Pattern’ by Martin
       Fowler (www.martinfowler.com)
   • Flexible, maintainable
   • Ready for complex validation
   • Loosely coupled
   • Most unknown design pattern
Advanced PHP
Summary

   • Best-practise ‘design’ solution
   • Lessons learned by other developers
   • Based on the OO principles
   • Language in-depended
   • Puzzles for the creative mind of developer
Advanced PHP
Summary




Advanced PHP
Summary

               OO Basics

          * Abstraction
          * Encapsulation
          * Polymorphism
          * Inheritance




Advanced PHP
Summary

                           OO Principles
               OO Basics
  * Encapsulate what varies
          * Abstraction
  * Favour composition over inheritance
          * Encapsulation
  * Program to interfaces, not implementations
          * Polymorphism
  * Strive for loosely coupled designs between objects that interact
          * Inheritance
  * Classes should be open for extension, but closed for
  modification




Advanced PHP
Summary

   • Strategy Design Pattern
   • Observer Design Pattern
   • Decorator Design Pattern
   • Specification Design Pattern

Advanced PHP
Questions




Advanced PHP
References
   • dennisjan@ibuildings.nl
   • www.ibuildings.nl / www.ibuildings.com
   • php|architect’s Guide to PHP Design
       Patterns
   • O’Reilly Head First Design Patterns
   • www.ibuildings.com/training/
Advanced PHP

More Related Content

What's hot

Building Desktop RIAs With PHP And JavaScript
Building Desktop RIAs With PHP And JavaScriptBuilding Desktop RIAs With PHP And JavaScript
Building Desktop RIAs With PHP And JavaScriptfunkatron
 
PHP Doesn't Suck
PHP Doesn't SuckPHP Doesn't Suck
PHP Doesn't SuckJohn Hobbs
 
Abstract Class and Interface in PHP
Abstract Class and Interface in PHPAbstract Class and Interface in PHP
Abstract Class and Interface in PHPVineet Kumar Saini
 
PHP, PHP Developer, Hire PHP Developer, PHP Web Development, Hire PHP Program...
PHP, PHP Developer, Hire PHP Developer, PHP Web Development, Hire PHP Program...PHP, PHP Developer, Hire PHP Developer, PHP Web Development, Hire PHP Program...
PHP, PHP Developer, Hire PHP Developer, PHP Web Development, Hire PHP Program...bchrisopher
 
Software Design
Software DesignSoftware Design
Software DesignSpy Seat
 
25 php interview questions – codementor
25 php interview questions – codementor25 php interview questions – codementor
25 php interview questions – codementorArc & Codementor
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)Arjun Shanka
 
The Basics Of Page Creation
The Basics Of Page CreationThe Basics Of Page Creation
The Basics Of Page CreationWildan Maulana
 

What's hot (14)

Php tutorial
Php tutorialPhp tutorial
Php tutorial
 
Php simple
Php simplePhp simple
Php simple
 
Building Desktop RIAs With PHP And JavaScript
Building Desktop RIAs With PHP And JavaScriptBuilding Desktop RIAs With PHP And JavaScript
Building Desktop RIAs With PHP And JavaScript
 
PHP Doesn't Suck
PHP Doesn't SuckPHP Doesn't Suck
PHP Doesn't Suck
 
Abstract Class and Interface in PHP
Abstract Class and Interface in PHPAbstract Class and Interface in PHP
Abstract Class and Interface in PHP
 
PHP, PHP Developer, Hire PHP Developer, PHP Web Development, Hire PHP Program...
PHP, PHP Developer, Hire PHP Developer, PHP Web Development, Hire PHP Program...PHP, PHP Developer, Hire PHP Developer, PHP Web Development, Hire PHP Program...
PHP, PHP Developer, Hire PHP Developer, PHP Web Development, Hire PHP Program...
 
Software Design
Software DesignSoftware Design
Software Design
 
25 php interview questions – codementor
25 php interview questions – codementor25 php interview questions – codementor
25 php interview questions – codementor
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)
 
Php notes 01
Php notes 01Php notes 01
Php notes 01
 
Switch case looping
Switch case loopingSwitch case looping
Switch case looping
 
php_tizag_tutorial
php_tizag_tutorialphp_tizag_tutorial
php_tizag_tutorial
 
Php Ppt
Php PptPhp Ppt
Php Ppt
 
The Basics Of Page Creation
The Basics Of Page CreationThe Basics Of Page Creation
The Basics Of Page Creation
 

Viewers also liked

Php Security By Mugdha And Anish
Php Security By Mugdha And AnishPhp Security By Mugdha And Anish
Php Security By Mugdha And AnishOSSCube
 
Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010Michelangelo van Dam
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHPBradley Holt
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisIan Macali
 
PHP Advanced
PHP AdvancedPHP Advanced
PHP AdvancedNoveo
 
Advanced PHP Web Development Tools in 2015
Advanced PHP Web Development Tools in 2015Advanced PHP Web Development Tools in 2015
Advanced PHP Web Development Tools in 2015iScripts
 
Advanced php testing in action
Advanced php testing in actionAdvanced php testing in action
Advanced php testing in actionJace Ju
 
Mawea Profile Presentation Slides 2011 Hidden
Mawea Profile Presentation Slides 2011 HiddenMawea Profile Presentation Slides 2011 Hidden
Mawea Profile Presentation Slides 2011 Hiddenevebby526
 
Projet 1200 מדפסת שולחנית לייצור תכשיטים
Projet 1200 מדפסת שולחנית לייצור תכשיטים Projet 1200 מדפסת שולחנית לייצור תכשיטים
Projet 1200 מדפסת שולחנית לייצור תכשיטים Caliber_Engineering
 
Five Steps to Optimize Casting and Eliminate Defects
Five Steps to Optimize Casting and Eliminate DefectsFive Steps to Optimize Casting and Eliminate Defects
Five Steps to Optimize Casting and Eliminate DefectsDesign World
 
Toyota trunk Class A in Alias design
Toyota trunk  Class A in Alias designToyota trunk  Class A in Alias design
Toyota trunk Class A in Alias designPSH Mechanical Design
 
GFMI AEROSPACE DIVISION LINE CARD 4JUN10
GFMI AEROSPACE DIVISION LINE CARD 4JUN10GFMI AEROSPACE DIVISION LINE CARD 4JUN10
GFMI AEROSPACE DIVISION LINE CARD 4JUN10CATHERINEM1_
 
Product categort shenzhen advanced titanium
Product categort shenzhen advanced titaniumProduct categort shenzhen advanced titanium
Product categort shenzhen advanced titaniumIvy gtg
 

Viewers also liked (19)

Php Security By Mugdha And Anish
Php Security By Mugdha And AnishPhp Security By Mugdha And Anish
Php Security By Mugdha And Anish
 
Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
 
PHP Advanced
PHP AdvancedPHP Advanced
PHP Advanced
 
Advanced PHP Simplified
Advanced PHP SimplifiedAdvanced PHP Simplified
Advanced PHP Simplified
 
Advanced PHP Web Development Tools in 2015
Advanced PHP Web Development Tools in 2015Advanced PHP Web Development Tools in 2015
Advanced PHP Web Development Tools in 2015
 
Advanced php testing in action
Advanced php testing in actionAdvanced php testing in action
Advanced php testing in action
 
Mawea Profile Presentation Slides 2011 Hidden
Mawea Profile Presentation Slides 2011 HiddenMawea Profile Presentation Slides 2011 Hidden
Mawea Profile Presentation Slides 2011 Hidden
 
Projet 1200 מדפסת שולחנית לייצור תכשיטים
Projet 1200 מדפסת שולחנית לייצור תכשיטים Projet 1200 מדפסת שולחנית לייצור תכשיטים
Projet 1200 מדפסת שולחנית לייצור תכשיטים
 
apostila de catia
apostila de catiaapostila de catia
apostila de catia
 
Five Steps to Optimize Casting and Eliminate Defects
Five Steps to Optimize Casting and Eliminate DefectsFive Steps to Optimize Casting and Eliminate Defects
Five Steps to Optimize Casting and Eliminate Defects
 
Catia Part07
Catia Part07Catia Part07
Catia Part07
 
Auto Tuning
Auto TuningAuto Tuning
Auto Tuning
 
Toyota trunk Class A in Alias design
Toyota trunk  Class A in Alias designToyota trunk  Class A in Alias design
Toyota trunk Class A in Alias design
 
GFMI AEROSPACE DIVISION LINE CARD 4JUN10
GFMI AEROSPACE DIVISION LINE CARD 4JUN10GFMI AEROSPACE DIVISION LINE CARD 4JUN10
GFMI AEROSPACE DIVISION LINE CARD 4JUN10
 
Mould in Reverse Engineering
Mould in Reverse Engineering Mould in Reverse Engineering
Mould in Reverse Engineering
 
Product categort shenzhen advanced titanium
Product categort shenzhen advanced titaniumProduct categort shenzhen advanced titanium
Product categort shenzhen advanced titanium
 

Similar to Advanced PHP: Design Patterns - Dennis-Jan Broerse

Enterprise PHP Development - ZendCon 2008
Enterprise PHP Development - ZendCon 2008Enterprise PHP Development - ZendCon 2008
Enterprise PHP Development - ZendCon 2008Ivo Jansch
 
Professional PHP: an open-source alternative for enterprise development [Kort...
Professional PHP: an open-source alternative for enterprise development [Kort...Professional PHP: an open-source alternative for enterprise development [Kort...
Professional PHP: an open-source alternative for enterprise development [Kort...Combell NV
 
The Power Of Refactoring (PHPNW)
The Power Of Refactoring (PHPNW)The Power Of Refactoring (PHPNW)
The Power Of Refactoring (PHPNW)Stefan Koopmanschap
 
Best Practices with Zend Framework - Matthew Weier O'Phinney
Best Practices with Zend Framework - Matthew Weier O'PhinneyBest Practices with Zend Framework - Matthew Weier O'Phinney
Best Practices with Zend Framework - Matthew Weier O'Phinneydpc
 
Phpworks enterprise-php-1227605806710884-9
Phpworks enterprise-php-1227605806710884-9Phpworks enterprise-php-1227605806710884-9
Phpworks enterprise-php-1227605806710884-9PrinceGuru MS
 
Scripting Recipes for Testers
Scripting Recipes for TestersScripting Recipes for Testers
Scripting Recipes for TestersAdam Goucher
 
The 7 Sins of Software Engineers in HEP
The 7 Sins of Software Engineers in HEPThe 7 Sins of Software Engineers in HEP
The 7 Sins of Software Engineers in HEPIoannis Baltopoulos
 
Article 01 What Is Php
Article 01   What Is PhpArticle 01   What Is Php
Article 01 What Is Phpdrperl
 
Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13DanWooster1
 
Understand Open Source ecosystems
Understand Open Source ecosystemsUnderstand Open Source ecosystems
Understand Open Source ecosystemsKnowmades.com
 
Dynamic Languages In The Enterprise (4developers march 2009)
Dynamic Languages In The Enterprise (4developers march 2009)Dynamic Languages In The Enterprise (4developers march 2009)
Dynamic Languages In The Enterprise (4developers march 2009)Ivo Jansch
 
Os Koziarsky
Os KoziarskyOs Koziarsky
Os Koziarskyoscon2007
 
Php tizag tutorial
Php tizag tutorial Php tizag tutorial
Php tizag tutorial jaggu536
 

Similar to Advanced PHP: Design Patterns - Dennis-Jan Broerse (20)

Enterprise PHP Development - ZendCon 2008
Enterprise PHP Development - ZendCon 2008Enterprise PHP Development - ZendCon 2008
Enterprise PHP Development - ZendCon 2008
 
Professional PHP: an open-source alternative for enterprise development [Kort...
Professional PHP: an open-source alternative for enterprise development [Kort...Professional PHP: an open-source alternative for enterprise development [Kort...
Professional PHP: an open-source alternative for enterprise development [Kort...
 
The Power Of Refactoring (PHPNW)
The Power Of Refactoring (PHPNW)The Power Of Refactoring (PHPNW)
The Power Of Refactoring (PHPNW)
 
The Power of Refactoring
The Power of RefactoringThe Power of Refactoring
The Power of Refactoring
 
Becoming A Php Ninja
Becoming A Php NinjaBecoming A Php Ninja
Becoming A Php Ninja
 
Best Practices with Zend Framework - Matthew Weier O'Phinney
Best Practices with Zend Framework - Matthew Weier O'PhinneyBest Practices with Zend Framework - Matthew Weier O'Phinney
Best Practices with Zend Framework - Matthew Weier O'Phinney
 
Phpworks enterprise-php-1227605806710884-9
Phpworks enterprise-php-1227605806710884-9Phpworks enterprise-php-1227605806710884-9
Phpworks enterprise-php-1227605806710884-9
 
Scripting Recipes for Testers
Scripting Recipes for TestersScripting Recipes for Testers
Scripting Recipes for Testers
 
PHP Lesson
PHP LessonPHP Lesson
PHP Lesson
 
The 7 Sins of Software Engineers in HEP
The 7 Sins of Software Engineers in HEPThe 7 Sins of Software Engineers in HEP
The 7 Sins of Software Engineers in HEP
 
Article 01 What Is Php
Article 01   What Is PhpArticle 01   What Is Php
Article 01 What Is Php
 
Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13
 
Understand Open Source ecosystems
Understand Open Source ecosystemsUnderstand Open Source ecosystems
Understand Open Source ecosystems
 
Dynamic Languages In The Enterprise (4developers march 2009)
Dynamic Languages In The Enterprise (4developers march 2009)Dynamic Languages In The Enterprise (4developers march 2009)
Dynamic Languages In The Enterprise (4developers march 2009)
 
Enterprise PHP
Enterprise PHPEnterprise PHP
Enterprise PHP
 
Os Koziarsky
Os KoziarskyOs Koziarsky
Os Koziarsky
 
Php tizag tutorial
Php tizag tutorialPhp tizag tutorial
Php tizag tutorial
 
Php tizag tutorial
Php tizag tutorial Php tizag tutorial
Php tizag tutorial
 
Php tizag tutorial
Php tizag tutorialPhp tizag tutorial
Php tizag tutorial
 
PHP learning
PHP learningPHP learning
PHP learning
 

More from dpc

ezComponents - Derick Rethans
ezComponents - Derick RethansezComponents - Derick Rethans
ezComponents - Derick Rethansdpc
 
Software And The Taste Of Mayo - Marco Tabini
Software And The Taste Of Mayo - Marco TabiniSoftware And The Taste Of Mayo - Marco Tabini
Software And The Taste Of Mayo - Marco Tabinidpc
 
Deployment With Subversion - Lorna Mitchell
Deployment With Subversion - Lorna MitchellDeployment With Subversion - Lorna Mitchell
Deployment With Subversion - Lorna Mitchelldpc
 
State Of PHP - Zeev Suraski
State Of PHP - Zeev SuraskiState Of PHP - Zeev Suraski
State Of PHP - Zeev Suraskidpc
 
Symfony 1.1 - Fabien Potencier
Symfony 1.1 - Fabien PotencierSymfony 1.1 - Fabien Potencier
Symfony 1.1 - Fabien Potencierdpc
 
New Features PHPUnit 3.3 - Sebastian Bergmann
New Features PHPUnit 3.3 - Sebastian BergmannNew Features PHPUnit 3.3 - Sebastian Bergmann
New Features PHPUnit 3.3 - Sebastian Bergmanndpc
 
PHP 5.3 and PHP 6; a look ahead - Stefan Priebsch
PHP 5.3 and PHP 6; a look ahead - Stefan PriebschPHP 5.3 and PHP 6; a look ahead - Stefan Priebsch
PHP 5.3 and PHP 6; a look ahead - Stefan Priebschdpc
 
Quality Assurance in PHP projects - Sebastian Bergmann
Quality Assurance in PHP projects - Sebastian BergmannQuality Assurance in PHP projects - Sebastian Bergmann
Quality Assurance in PHP projects - Sebastian Bergmanndpc
 
An Infrastructure for Team Development - Gaylord Aulke
An Infrastructure for Team Development - Gaylord AulkeAn Infrastructure for Team Development - Gaylord Aulke
An Infrastructure for Team Development - Gaylord Aulkedpc
 
Enterprise PHP Development - Ivo Jansch
Enterprise PHP Development - Ivo JanschEnterprise PHP Development - Ivo Jansch
Enterprise PHP Development - Ivo Janschdpc
 
DPC2008 Intro - Ivo Jansch
DPC2008 Intro - Ivo JanschDPC2008 Intro - Ivo Jansch
DPC2008 Intro - Ivo Janschdpc
 
DPC 2007 My First Mashup (Cal Evans)
DPC 2007 My First Mashup (Cal Evans)DPC 2007 My First Mashup (Cal Evans)
DPC 2007 My First Mashup (Cal Evans)dpc
 
DPC2007 CodeGear, Delphi For PHP (Pawel Glowacki)
DPC2007 CodeGear, Delphi For PHP (Pawel Glowacki)DPC2007 CodeGear, Delphi For PHP (Pawel Glowacki)
DPC2007 CodeGear, Delphi For PHP (Pawel Glowacki)dpc
 
DPC2007 Zend Framework (Gaylord Aulke)
DPC2007 Zend Framework (Gaylord Aulke)DPC2007 Zend Framework (Gaylord Aulke)
DPC2007 Zend Framework (Gaylord Aulke)dpc
 
DPC2007 Objects Of Desire (Kevlin Henney)
DPC2007 Objects Of Desire (Kevlin Henney)DPC2007 Objects Of Desire (Kevlin Henney)
DPC2007 Objects Of Desire (Kevlin Henney)dpc
 
DPC2007 Symfony (Stefan Koopmanschap)
DPC2007 Symfony (Stefan Koopmanschap)DPC2007 Symfony (Stefan Koopmanschap)
DPC2007 Symfony (Stefan Koopmanschap)dpc
 
DPC2007 PHP And Oracle (Kuassi Mensah)
DPC2007 PHP And Oracle (Kuassi Mensah)DPC2007 PHP And Oracle (Kuassi Mensah)
DPC2007 PHP And Oracle (Kuassi Mensah)dpc
 
DPC2007 Case Study Surfnet (Herman Van Dompseler)
DPC2007 Case Study Surfnet (Herman Van Dompseler)DPC2007 Case Study Surfnet (Herman Van Dompseler)
DPC2007 Case Study Surfnet (Herman Van Dompseler)dpc
 
DPC2007 Case Study Zoom & Webwereld (Sander vd Graaf)
DPC2007 Case Study Zoom & Webwereld (Sander vd Graaf)DPC2007 Case Study Zoom & Webwereld (Sander vd Graaf)
DPC2007 Case Study Zoom & Webwereld (Sander vd Graaf)dpc
 
DPC2007 PDO (Lukas Kahwe Smith)
DPC2007 PDO (Lukas Kahwe Smith)DPC2007 PDO (Lukas Kahwe Smith)
DPC2007 PDO (Lukas Kahwe Smith)dpc
 

More from dpc (20)

ezComponents - Derick Rethans
ezComponents - Derick RethansezComponents - Derick Rethans
ezComponents - Derick Rethans
 
Software And The Taste Of Mayo - Marco Tabini
Software And The Taste Of Mayo - Marco TabiniSoftware And The Taste Of Mayo - Marco Tabini
Software And The Taste Of Mayo - Marco Tabini
 
Deployment With Subversion - Lorna Mitchell
Deployment With Subversion - Lorna MitchellDeployment With Subversion - Lorna Mitchell
Deployment With Subversion - Lorna Mitchell
 
State Of PHP - Zeev Suraski
State Of PHP - Zeev SuraskiState Of PHP - Zeev Suraski
State Of PHP - Zeev Suraski
 
Symfony 1.1 - Fabien Potencier
Symfony 1.1 - Fabien PotencierSymfony 1.1 - Fabien Potencier
Symfony 1.1 - Fabien Potencier
 
New Features PHPUnit 3.3 - Sebastian Bergmann
New Features PHPUnit 3.3 - Sebastian BergmannNew Features PHPUnit 3.3 - Sebastian Bergmann
New Features PHPUnit 3.3 - Sebastian Bergmann
 
PHP 5.3 and PHP 6; a look ahead - Stefan Priebsch
PHP 5.3 and PHP 6; a look ahead - Stefan PriebschPHP 5.3 and PHP 6; a look ahead - Stefan Priebsch
PHP 5.3 and PHP 6; a look ahead - Stefan Priebsch
 
Quality Assurance in PHP projects - Sebastian Bergmann
Quality Assurance in PHP projects - Sebastian BergmannQuality Assurance in PHP projects - Sebastian Bergmann
Quality Assurance in PHP projects - Sebastian Bergmann
 
An Infrastructure for Team Development - Gaylord Aulke
An Infrastructure for Team Development - Gaylord AulkeAn Infrastructure for Team Development - Gaylord Aulke
An Infrastructure for Team Development - Gaylord Aulke
 
Enterprise PHP Development - Ivo Jansch
Enterprise PHP Development - Ivo JanschEnterprise PHP Development - Ivo Jansch
Enterprise PHP Development - Ivo Jansch
 
DPC2008 Intro - Ivo Jansch
DPC2008 Intro - Ivo JanschDPC2008 Intro - Ivo Jansch
DPC2008 Intro - Ivo Jansch
 
DPC 2007 My First Mashup (Cal Evans)
DPC 2007 My First Mashup (Cal Evans)DPC 2007 My First Mashup (Cal Evans)
DPC 2007 My First Mashup (Cal Evans)
 
DPC2007 CodeGear, Delphi For PHP (Pawel Glowacki)
DPC2007 CodeGear, Delphi For PHP (Pawel Glowacki)DPC2007 CodeGear, Delphi For PHP (Pawel Glowacki)
DPC2007 CodeGear, Delphi For PHP (Pawel Glowacki)
 
DPC2007 Zend Framework (Gaylord Aulke)
DPC2007 Zend Framework (Gaylord Aulke)DPC2007 Zend Framework (Gaylord Aulke)
DPC2007 Zend Framework (Gaylord Aulke)
 
DPC2007 Objects Of Desire (Kevlin Henney)
DPC2007 Objects Of Desire (Kevlin Henney)DPC2007 Objects Of Desire (Kevlin Henney)
DPC2007 Objects Of Desire (Kevlin Henney)
 
DPC2007 Symfony (Stefan Koopmanschap)
DPC2007 Symfony (Stefan Koopmanschap)DPC2007 Symfony (Stefan Koopmanschap)
DPC2007 Symfony (Stefan Koopmanschap)
 
DPC2007 PHP And Oracle (Kuassi Mensah)
DPC2007 PHP And Oracle (Kuassi Mensah)DPC2007 PHP And Oracle (Kuassi Mensah)
DPC2007 PHP And Oracle (Kuassi Mensah)
 
DPC2007 Case Study Surfnet (Herman Van Dompseler)
DPC2007 Case Study Surfnet (Herman Van Dompseler)DPC2007 Case Study Surfnet (Herman Van Dompseler)
DPC2007 Case Study Surfnet (Herman Van Dompseler)
 
DPC2007 Case Study Zoom & Webwereld (Sander vd Graaf)
DPC2007 Case Study Zoom & Webwereld (Sander vd Graaf)DPC2007 Case Study Zoom & Webwereld (Sander vd Graaf)
DPC2007 Case Study Zoom & Webwereld (Sander vd Graaf)
 
DPC2007 PDO (Lukas Kahwe Smith)
DPC2007 PDO (Lukas Kahwe Smith)DPC2007 PDO (Lukas Kahwe Smith)
DPC2007 PDO (Lukas Kahwe Smith)
 

Recently uploaded

Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
QMMS Lesson 2 - Using MS Excel Formula.pdf
QMMS Lesson 2 - Using MS Excel Formula.pdfQMMS Lesson 2 - Using MS Excel Formula.pdf
QMMS Lesson 2 - Using MS Excel Formula.pdfROWELL MARQUINA
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
Digital Tools & AI in Career Development
Digital Tools & AI in Career DevelopmentDigital Tools & AI in Career Development
Digital Tools & AI in Career DevelopmentMahmoud Rabie
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessWSO2
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxAna-Maria Mihalceanu
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsYoss Cohen
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Mark Simos
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 

Recently uploaded (20)

Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
QMMS Lesson 2 - Using MS Excel Formula.pdf
QMMS Lesson 2 - Using MS Excel Formula.pdfQMMS Lesson 2 - Using MS Excel Formula.pdf
QMMS Lesson 2 - Using MS Excel Formula.pdf
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
Digital Tools & AI in Career Development
Digital Tools & AI in Career DevelopmentDigital Tools & AI in Career Development
Digital Tools & AI in Career Development
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with Platformless
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance Toolbox
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platforms
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 

Advanced PHP: Design Patterns - Dennis-Jan Broerse

  • 1. Advanced PHP A touch of Design Patterns Dennis-Jan Broerse - 13 juni 2008 Advanced PHP
  • 2. Sources • Download the sources: http://www.ibuildings.nl/downloads/training username: dpc_2008 password: dpc_2008 Advanced PHP
  • 3. Introduction • First conference where I’m a speaker Advanced PHP
  • 4. Topics • Introduction to Design Patterns • Duck simulator • Weather application • Coffee bar • Traveller Advanced PHP
  • 5. Introduction to Design Patterns • Best-practise ‘design’ solution • Lessons learned by other developers • Based on the OO principles • Language in-depended • Puzzles for the creative mind of developer Advanced PHP
  • 6. Introduction to Design Patterns Advanced PHP
  • 7. Introduction to Design Patterns OO Basics * Abstraction * Encapsulation * Polymorphism * Inheritance Advanced PHP
  • 8. Introduction to Design Patterns OO Basics OO Principles * Abstraction * Encapsulate what varies * Encapsulation * Favour composition * Polymorphism over inheritance * Inheritance * Program to interfaces, not implementations Advanced PHP
  • 9. Introduction to Design Patterns • It’s all about creating flexible designs that are maintainable and are able to deal with changes! • Don’t re-invent the wheel! • Load the design patterns into your brain and then design/build your application Advanced PHP
  • 11. Duck simulator What we want • Creating as many ducks as needed • Ducks quack and swim Advanced PHP
  • 12. Duck simulator What we want • That’s easy!! • We can use INHERITANCE • Yes, we know our OO basics Advanced PHP
  • 13. Duck simulator What we want Advanced PHP
  • 14. Duck simulator What we want Advanced PHP
  • 15. Duck simulator First change • Our ducks have to fly! • No problem Advanced PHP
  • 16. Duck simulator First change • Our ducks have to fly! • No problem Advanced PHP
  • 17. Duck simulator First problem • Rubber ducks don’t fly! (and I don’t mean throwing) Advanced PHP
  • 18. Duck simulator First change • This can't be the solution (imagine what you have to do if you want to create a decoy duck) Advanced PHP
  • 19. Duck simulator First change • This can't be the solution (imagine what you have to do if you want to create a decoy duck) Advanced PHP
  • 20. Duck simulator Cleaner solution • How about an interface? • Only implementing the interface if the duck is flyable • Hmm that’s it, that’s the solution Advanced PHP
  • 21. Duck simulator Cleaner solution Advanced PHP
  • 22. Duck simulator Cleaner solution Advanced PHP
  • 23. Duck simulator Think again • What happens if we have to change the fly behaviour? • Exactly, we have to change all instances! • And we duplicate code • Wouldn’t it be dreamy if we can change its behaviour at runtime? Advanced PHP
  • 24. Duck simulator Think again • Do you remember the OO basics and principles? • ‘Identify the aspects of your application that vary and separate them from what stays the same’ Advanced PHP
  • 25. Duck simulator Last change Advanced PHP
  • 26. Duck simulator Last change Advanced PHP
  • 27. Duck simulator Benefits • Flexible • Maintainable • Extendable • No code duplication • Encapsulated • Change behaviour at runtime Advanced PHP
  • 28. Duck simulator Code sample • Code sample Advanced PHP
  • 29. Duck simulator Strategy Design Pattern • ‘The strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.’ • Do you remember the OO basics and principles? Advanced PHP
  • 30. Duck simulator Guru • Now you’ll understand why you should know about design patterns! Advanced PHP
  • 32. Weather application What we want Advanced PHP
  • 33. Weather application What we want Displays Pulls data WheatherData object Advanced PHP
  • 34. Weather application Requirements • There are 3 get methods for temperature, humidity and pressure • measurementsChanged method will be automatically called if the measurements are changed • We have to implement the 3 display elements and they must be updated when measurements change Advanced PHP
  • 35. Weather application First implementation Advanced PHP
  • 36. Weather application First implementation Advanced PHP
  • 37. Weather application First implementation Advanced PHP
  • 38. Weather application What’s wrong • What if we want to add a new display? • Exactly: we have to change the code Advanced PHP
  • 39. Weather application What’s wrong • Remember the OO basics and principles! • Not flexible • Not extendable • No encapsulation Advanced PHP
  • 40. Weather application Let’s look • Investigate what’s going on • What is the relationship between the weather object and the display elements? • Maybe we can apply a design pattern? Advanced PHP
  • 41. Weather application Let’s look • The relationship looks like a newspaper subscription • If you want to receive the newspaper, you have to subscribe yourself at the publisher • The publisher will send you your newspaper every time until you unsubscribe Advanced PHP
  • 42. Weather application So.... • The weather object is the publisher • The display elements are the people who are willing to receive the newspaper Advanced PHP
  • 43. Weather application And that is... • Yes, that is a design pattern! • Called ‘The Observer Pattern’ • Publisher == Weather object == Subject • People == Display elements == Observers • Subject notifies every observer to update Advanced PHP
  • 44. Weather application Formal description • ‘The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.’ Advanced PHP
  • 45. Weather application The class diagram Advanced PHP
  • 46. Weather application The class diagram Advanced PHP
  • 47. Weather application New design principle • Creating new observers won’t require a change in the subject • Changes to either the subject or an observer will not affect the other • Reusing a subject or an observer can independently of each other Advanced PHP
  • 48. Weather application New design principle Advanced PHP 38
  • 49. Weather application New design principle OO Principles * Encapsulate what varies * Favour composition over inheritance * Program to interfaces, not implementations * Strive for loosely coupled designs between objects that interact Advanced PHP 38
  • 50. Weather application New design Advanced PHP
  • 51. Weather application New design Advanced PHP
  • 52. Weather application New design • Code sample Advanced PHP
  • 54. Coffee bar What we want • An application that calculates the price of different beverages • The flexibility to add new beverages Advanced PHP
  • 55. Coffee bar Simple solution Advanced PHP
  • 56. Coffee bar Simple solution Advanced PHP
  • 57. Coffee bar Condiments?? • No problem! • We have an OO application; just inherit Advanced PHP
  • 58. Coffee bar First attempt Advanced PHP
  • 59. Coffee bar First attempt Advanced PHP
  • 60. Coffee bar First attempt • Euhm, this isn’t maintainable, this is a class explosion!! • Try again!! Advanced PHP
  • 61. Coffee bar OK, sorry • Well that’s better, wouldn’t you agree? Advanced PHP
  • 62. Coffee bar OK, sorry • Well that’s better, wouldn’t you agree? Advanced PHP
  • 63. Coffee bar Really? • Did you really learn something? • Do you remember the OO basics and principles? Advanced PHP
  • 64. Coffee bar New design principle Advanced PHP 49
  • 65. Coffee bar New design principle OO Principles * Encapsulate what varies * Favour composition over inheritance * Program to interfaces, not implementations * Strive for loosely coupled designs between objects that interact * Classes should be open for extension, but closed for modification Advanced PHP 49
  • 66. Coffee bar What we really want • We’d like to extend a beverage with condiments • We wouldn’t like to change the beverage • We’d like to add new beverages and condiments without changing the existing code Advanced PHP 50
  • 67. Coffee bar New design pattern • The ‘Decorator Design Pattern’ saves our day • ‘The decorator pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.’ Advanced PHP 51
  • 68. Coffee bar Our new design Advanced PHP 52
  • 69. Coffee bar Our new design Advanced PHP 52
  • 70. Coffee bar Our new design • This is really great! • Now we can say: One HouseBlend coffee with milk and mocha. What is the price? € 1,95 Advanced PHP 53
  • 71. Coffee bar How does it work? • Wrap the houseBlend object with Milk object and wrap that with a mocha object • Ok, this is hard. Show me the code man. Advanced PHP 54
  • 72. Coffee bar How does it work? • Code sample Advanced PHP 55
  • 74. Traveller What we want • An application which tells us if the conditions are suitable enough to make the trip Advanced PHP
  • 75. Traveller Wait a minute For example: • The traveller doesn’t want to make the trip if the average temperature of the destination is lower than his minimum required temperature Advanced PHP
  • 76. Traveller First implementation • What do you think? Advanced PHP
  • 77. Traveller First implementation • What do you think? Advanced PHP
  • 78. Traveller First implementation • Indeed, not very smart! • Not possible to add requirements • Requirements changes, so trip class has to be changed Advanced PHP
  • 79. Traveller Take that out Advanced PHP
  • 80. Traveller Take that out Advanced PHP
  • 81. Traveller That’s nice • We are able to create multiple specifications without changing other objects • Readable, maintainable, simple and effective • And it is a design pattern too!! ‘Hard-coded Specification Pattern’ Advanced PHP
  • 82. Traveller But... • The specification knows too much • Not able to make logical specifications • Does violate some OO principles You know them by now, right? Advanced PHP
  • 83. Traveller What we really want • A design which is flexible enough to validate almost every object • Validating against several conditions • Making logical expressions Advanced PHP
  • 84. Traveller Let’s redesign Advanced PHP
  • 85. Traveller Let’s redesign Advanced PHP
  • 86. Traveller Wow, that’s heavy • Code sample Advanced PHP
  • 87. Traveller This is ... • ‘The Specification Pattern’ by Martin Fowler (www.martinfowler.com) • Flexible, maintainable • Ready for complex validation • Loosely coupled • Most unknown design pattern Advanced PHP
  • 88. Summary • Best-practise ‘design’ solution • Lessons learned by other developers • Based on the OO principles • Language in-depended • Puzzles for the creative mind of developer Advanced PHP
  • 90. Summary OO Basics * Abstraction * Encapsulation * Polymorphism * Inheritance Advanced PHP
  • 91. Summary OO Principles OO Basics * Encapsulate what varies * Abstraction * Favour composition over inheritance * Encapsulation * Program to interfaces, not implementations * Polymorphism * Strive for loosely coupled designs between objects that interact * Inheritance * Classes should be open for extension, but closed for modification Advanced PHP
  • 92. Summary • Strategy Design Pattern • Observer Design Pattern • Decorator Design Pattern • Specification Design Pattern Advanced PHP
  • 94. References • dennisjan@ibuildings.nl • www.ibuildings.nl / www.ibuildings.com • php|architect’s Guide to PHP Design Patterns • O’Reilly Head First Design Patterns • www.ibuildings.com/training/ Advanced PHP