Advanced PHP: Design Patterns - Dennis-Jan Broerse

80 %
20 %
Information about Advanced PHP: Design Patterns - Dennis-Jan Broerse

Published on June 17, 2008

Author: dpc

Source: slideshare.net

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

Add a comment

Related presentations

Related pages

Advanced PHP: Design Patterns - Dennis-Jan Broerse ...

Enterprise PHP Architecture through Design Patterns and Modularization (MidwestPHP 2013)
Read more

Advanced Php | LinkedIn

Vice President at Advanced PHP Solutions See less. View More View Less. View Profile. Aniesh Joseh PHP Developer. Cochin Area, India. Information ...
Read more

Php Design | LinkedIn

View 2130 Php Design posts, presentations, experts, and more. Get the professional knowledge you need on LinkedIn. LinkedIn Home What is LinkedIn?
Read more

Dennis | LinkedIn

Dennis Nichols (Dennis J) Dia'ani Entertainment Group / Interscope Records / Treezyville Music. Charlotte, North Carolina Area. Entertainment
Read more