advanced php: design patterns - dennis-jan broerse
DESCRIPTION
TRANSCRIPT
![Page 1: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/1.jpg)
Advanced PHP
Advanced PHPA touch of Design Patterns
Dennis-Jan Broerse - 13 juni 2008
![Page 2: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/2.jpg)
Advanced PHP
Sources
• Download the sources:http://www.ibuildings.nl/downloads/trainingusername: dpc_2008password: dpc_2008
![Page 3: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/3.jpg)
Advanced PHP
Introduction
• First conference where I’m a speaker
![Page 4: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/4.jpg)
Advanced PHP
Topics
• Introduction to Design Patterns
• Duck simulator
• Weather application
• Coffee bar
• Traveller
![Page 5: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/5.jpg)
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
![Page 6: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/6.jpg)
Advanced PHP
Introduction to Design Patterns
![Page 7: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/7.jpg)
Advanced PHP
Introduction to Design Patterns
OO Basics
* Abstraction* Encapsulation* Polymorphism* Inheritance
![Page 8: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/8.jpg)
Advanced PHP
Introduction to Design Patterns
OO Basics
* Abstraction* Encapsulation* Polymorphism* Inheritance
OO Principles
* Encapsulate what varies* Favour composition over inheritance* Program to interfaces, not implementations
![Page 9: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/9.jpg)
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
![Page 10: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/10.jpg)
Advanced PHP
Duck simulator
![Page 11: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/11.jpg)
Advanced PHP
Duck simulator
What we want
• Creating as many ducks as needed
• Ducks quack and swim
![Page 12: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/12.jpg)
Advanced PHP
Duck simulator
What we want
• That’s easy!!
• We can use INHERITANCE
• Yes, we know our OO basics
![Page 13: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/13.jpg)
Advanced PHP
Duck simulator
What we want
![Page 14: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/14.jpg)
Advanced PHP
Duck simulator
What we want
![Page 15: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/15.jpg)
Advanced PHP
Duck simulator
First change
• Our ducks have to fly!
• No problem
![Page 16: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/16.jpg)
Advanced PHP
Duck simulator
First change
• Our ducks have to fly!
• No problem
![Page 17: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/17.jpg)
Advanced PHP
Duck simulator
First problem
• Rubber ducks don’t fly!(and I don’t mean throwing)
![Page 18: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/18.jpg)
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)
![Page 19: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/19.jpg)
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)
![Page 20: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/20.jpg)
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
![Page 21: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/21.jpg)
Advanced PHP
Duck simulator
Cleaner solution
![Page 22: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/22.jpg)
Advanced PHP
Duck simulator
Cleaner solution
![Page 23: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/23.jpg)
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?
![Page 24: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/24.jpg)
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’
![Page 25: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/25.jpg)
Advanced PHP
Duck simulator
Last change
![Page 26: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/26.jpg)
Advanced PHP
Duck simulator
Last change
![Page 27: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/27.jpg)
Advanced PHP
Duck simulator
Benefits• Flexible
• Maintainable
• Extendable
• No code duplication
• Encapsulated
• Change behaviour at runtime
![Page 28: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/28.jpg)
Advanced PHP
Duck simulator
Code sample
• Code sample
![Page 29: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/29.jpg)
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?
![Page 30: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/30.jpg)
Advanced PHP
Duck simulator
Guru
• Now you’ll understand why you should know about design patterns!
![Page 31: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/31.jpg)
Advanced PHP
Weather application
![Page 32: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/32.jpg)
Advanced PHP
Weather application
What we want
![Page 33: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/33.jpg)
Advanced PHP
Weather application
What we want
WheatherData object
Pulls dataDisplays
![Page 34: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/34.jpg)
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
![Page 35: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/35.jpg)
Advanced PHP
Weather application
First implementation
![Page 36: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/36.jpg)
Advanced PHP
Weather application
First implementation
![Page 37: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/37.jpg)
Advanced PHP
Weather application
First implementation
![Page 38: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/38.jpg)
Advanced PHP
Weather application
What’s wrong
• What if we want to add a new display?
• Exactly: we have to change the code
![Page 39: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/39.jpg)
Advanced PHP
Weather application
What’s wrong
• Remember the OO basics and principles!
• Not flexible
• Not extendable
• No encapsulation
![Page 40: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/40.jpg)
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?
![Page 41: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/41.jpg)
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
![Page 42: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/42.jpg)
Advanced PHP
Weather application
So....
• The weather object is the publisher
• The display elements are the people who are willing to receive the newspaper
![Page 43: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/43.jpg)
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
![Page 44: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/44.jpg)
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.’
![Page 45: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/45.jpg)
Advanced PHP
Weather application
The class diagram
![Page 46: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/46.jpg)
Advanced PHP
Weather application
The class diagram
![Page 47: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/47.jpg)
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
![Page 48: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/48.jpg)
Advanced PHP
Weather application
New design principle
38
![Page 49: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/49.jpg)
Advanced PHP
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
38
![Page 50: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/50.jpg)
Advanced PHP
Weather application
New design
![Page 51: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/51.jpg)
Advanced PHP
Weather application
New design
![Page 52: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/52.jpg)
Advanced PHP
Weather application
New design
• Code sample
![Page 53: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/53.jpg)
Advanced PHP
Coffee bar
![Page 54: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/54.jpg)
Advanced PHP
Coffee bar
What we want
• An application that calculates the price of different beverages
• The flexibility to add new beverages
![Page 55: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/55.jpg)
Advanced PHP
Coffee bar
Simple solution
![Page 56: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/56.jpg)
Advanced PHP
Coffee bar
Simple solution
![Page 57: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/57.jpg)
Advanced PHP
Coffee bar
Condiments??
• No problem!
• We have an OO application; just inherit
![Page 58: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/58.jpg)
Advanced PHP
Coffee bar
First attempt
![Page 59: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/59.jpg)
Advanced PHP
Coffee bar
First attempt
![Page 60: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/60.jpg)
Advanced PHP
Coffee bar
First attempt
• Euhm, this isn’t maintainable, this is a class explosion!!
• Try again!!
![Page 61: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/61.jpg)
Advanced PHP
Coffee bar
OK, sorry
• Well that’s better, wouldn’t you agree?
![Page 62: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/62.jpg)
Advanced PHP
Coffee bar
OK, sorry
• Well that’s better, wouldn’t you agree?
![Page 63: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/63.jpg)
Advanced PHP
Coffee bar
Really?
• Did you really learn something?
• Do you remember the OO basics and principles?
![Page 64: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/64.jpg)
Advanced PHP
Coffee bar
New design principle
49
![Page 65: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/65.jpg)
Advanced PHP
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
49
![Page 66: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/66.jpg)
Advanced PHP
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
50
![Page 67: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/67.jpg)
Advanced PHP
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.’
51
![Page 68: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/68.jpg)
Advanced PHP
Coffee bar
Our new design
52
![Page 69: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/69.jpg)
Advanced PHP
Coffee bar
Our new design
52
![Page 70: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/70.jpg)
Advanced PHP
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
53
![Page 71: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/71.jpg)
Advanced PHP
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.
54
![Page 72: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/72.jpg)
Advanced PHP
Coffee bar
How does it work?
• Code sample
55
![Page 73: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/73.jpg)
Advanced PHP
Traveller
![Page 74: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/74.jpg)
Advanced PHP
Traveller
What we want
• An application which tells us if the conditions are suitable enough to make the trip
![Page 75: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/75.jpg)
Advanced PHP
Traveller
Wait a minute
• The traveller doesn’t want to make the trip if the average temperature of the destination is lower than his minimum required temperature
For example:
![Page 76: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/76.jpg)
Advanced PHP
Traveller
First implementation
• What do you think?
![Page 77: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/77.jpg)
Advanced PHP
Traveller
First implementation
• What do you think?
![Page 78: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/78.jpg)
Advanced PHP
Traveller
First implementation
• Indeed, not very smart!
• Not possible to add requirements
• Requirements changes, so trip class has to be changed
![Page 79: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/79.jpg)
Advanced PHP
Traveller
Take that out
![Page 80: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/80.jpg)
Advanced PHP
Traveller
Take that out
![Page 81: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/81.jpg)
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’
![Page 82: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/82.jpg)
Advanced PHP
Traveller
But...
• The specification knows too much
• Not able to make logical specifications
• Does violate some OO principlesYou know them by now, right?
![Page 83: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/83.jpg)
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
![Page 84: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/84.jpg)
Advanced PHP
Traveller
Let’s redesign
![Page 85: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/85.jpg)
Advanced PHP
Traveller
Let’s redesign
![Page 86: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/86.jpg)
Advanced PHP
Traveller
Wow, that’s heavy
• Code sample
![Page 87: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/87.jpg)
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
![Page 88: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/88.jpg)
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
![Page 89: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/89.jpg)
Advanced PHP
Summary
![Page 90: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/90.jpg)
Advanced PHP
Summary
OO Basics
* Abstraction* Encapsulation* Polymorphism* Inheritance
![Page 91: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/91.jpg)
Advanced PHP
Summary
OO Basics
* Abstraction* Encapsulation* Polymorphism* Inheritance
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
![Page 92: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/92.jpg)
Advanced PHP
Summary
• Strategy Design Pattern
• Observer Design Pattern
• Decorator Design Pattern
• Specification Design Pattern
![Page 93: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/93.jpg)
Advanced PHP
Questions
![Page 94: Advanced PHP: Design Patterns - Dennis-Jan Broerse](https://reader038.vdocument.in/reader038/viewer/2022102922/54b7a0fe4a795998738b468a/html5/thumbnails/94.jpg)
Advanced PHP
References
• www.ibuildings.nl / www.ibuildings.com
• php|architect’s Guide to PHP Design Patterns
• O’Reilly Head First Design Patterns
• www.ibuildings.com/training/