SlideShare ist ein Scribd-Unternehmen logo
1 von 35
1
Design Patterns
Daniel Waligóra
Wrocław 09/01/2012
2
Bibliography
• Martin Fowler,Patterns of Enterprise Appication Architecture
• Matt Zandstra , PHP5 Objects, Patters, and Practice
• http://www.ustream.tv - NewYork PHP channel
• Symfony Live Berlin 2012 - https://joind.in/event/view/1114
• http://scholar.google.pl
• http://pzielinski.com
3
Gang of Four
4
Breakdown Design
Patterns by GoF
Design PatternsDesign Patterns
creationalcreational structuralstructural behavioralbehavioral
- abstract factory
- builder
- prototype
- decorator
- facade
- composite
- observer
- strategy
- command
5
6
7
Implementation related
to the context
programmingprogramming
languagelanguage
scalabilityscalability
modularitymodularity
testabletestable
(integration test)(integration test)
time-consumingtime-consuming size of projectsize of project
8
ContextContext ISIS
related to therelated to the
programmingprogramming
languagelanguage
Design patternsDesign patterns
IS NOTIS NOT relatedrelated
to theto the
programmingprogramming
languagelanguage
9
Transaction Script1. class Hotel  
2. {   
3.     private    
4.         $_gateway;
5.     public function __construct(Data_Access_Gateway $gateway)    
6.     {    
7.         $this->_gateway = $gateway;    
8.     }    
9.   
10.     public function bookRoom($userId, $fromDate, $toDate)    
11.     {    
12.         $roomId = $this->_gateway->_getRoomIdBetweenDates($fromDate, $toDate);    
13.   
14.         if (!$roomId) {    
15.             return false;    
16.         }    
17.   
18.         $days = $this->_getAmountOfDays($fromDate, $toDate);    
19.   
20.         if ($days < = 7) {  
21.             $price = $days * 100;  
22.         } else {   
23.             $price = $days * 80;  
24.         }  
25.           
26.         $data = array(  
27.             'userId' => $userId,    
28.             'roomId' => $roomId,  
29.             'fromDate' => $fromDate,    
30.             'toDate' => $toDate,    
31.             'price' => $price,    
32.         );    
33.   
34.         $bookingId = $this->_gateway->insert('bookings', $data);    
35.   
36.         return $bookingId;    
37.     }    
38. } 
10
Transaction Script
• simple procedural model
• works well with a simple
data access layer
• easy implementation of use
cases
• difficult to maintenance
• code duplication
Disadvantages:Advantages:
11
Table Module #11. class Hotel  
2. {    
3.     public function __construct(Data_Access_Gateway $gateway, Booking $booking)    
•     {    
•         $this->_gateway = $gateway;  
•         $this->_booking = $booking;  
•     }    
•   
•     public function bookRoom($userId, $fromDate, $toDate)    
•     {    
•         $roomId = $this->_booking->getRoomBetweenDates($fromDate, $toDate);  
•   
•         if (!$roomId) {    
•             return false;    
•         }    
•   
•         $days = $this->_getAmountOfDays($fromDate, $toDate);    
•   
•         if ($days < = 7) {  
•             $price = $days * 100;  
•         } else {   
•             $price = $days * 80;  
•         }  
•           
•         $bookingId = $this->_booking-
>addBooking($userId, $roomId, $fromDate, $toDate, $price);    
•   
•         return $bookingId;    
•     }    
• }  
•   
12
Table Module #2
1. class Booking  
2. {    
3.     public function __construct(Data_Access_Gateway $gateway)    
4.     {    
5.         $this->_gateway = $gateway;    
6.     }  
7.       
8.     public function getRoomBetweenDates($dateFrom, $dateTo)  
9.     {  
10.        return $this->_gateway->getRoomBetweenDates($dateFrom, $dateTo);  
11.    }  
12.      
13.    public function addBooking($userId, $roomId, $fromDate, $toDate, $price)    
14.    {    
15.        $data = array(  
16.            'userId' => $userId,    
17.            'roomId' => $roomId,  
18.            'fromDate' => $fromDate,    
19.            'toDate' => $toDate,    
20.            'price' => $price,    
21.        );    
22.  
23.        $bookingId = $this->_gateway->insert('bookings', $data);    
24.  
25.        return $bookingId;    
26.    }    
27.}  
13
Table Module vs
Transaction Script
• less duplication
• encapsulation
• more organized and
structured code
• easy implementation by
technology support
• weak support for
polymorphism
• no support ORM
Disadvantages:Advantages:
14
Domain Model #11. class Hotel  
2. {    
3.     protected $_hotelId;  
4.     protected $_rooms;  
5.       
6.     public function bookRoom(User $user, $fromDate, $toDate)    
7.     {    
8.         $room = $this->_getRoomBetweenDates($fromDate, $toDate);  
9.   
10.         if (is_null($room)) {    
11.             return false;    
12.         }    
13.   
14.         $booking = $room->bookRoom(User $user, $fromDate, $toDate);  
15.   
16.         return $booking;    
17.     }  
18. }  
19.   
20. class Room  
21. {  
22.     protected $_roomId;  
23.     protected $_bookings = array();  
24.       
25.     public function bookRoom(User $user, $fromDate, $toDate)  
26.     {  
27.         $days = $this->_getAmountOfDays($fromDate, $toDate);  
28.           
29.         if ($days < = 7) {  
30.             $booking = new Booking($user, new ShortBookingStrategy($user, $days));  
31.         } else {   
32.             $booking = new Booking($user, new NormalBookingStrategy($user, $days));  
33.         }  
34.           
35.         return $booking;  
36.     }  
37. } 
15
Domain Model #2
1. class NormalBookingPriceStrategy extends BookingPriceStrategy  
2. {  
3.     public function getPrice()  
4.     {  
5.         $price = $this->_days * 80;  
6.           
7.         if ($this->_user->isLoyal()) {  
8.             $price = $price / 2;  
9.         }  
10.          
11.        return $price;  
12.    }  
13.}  
14.  
15.class ShortBookingPriceStrategy extends BookingPriceStrategy  
16.{    
17.    public function getPrice()  
18.    {  
19.        return $this->_days * 100;  
20.    }  
21.}
16
Domain Model vs
Procedural Pattern
• prevents logic duplication
• more code readability
• independence from the data
source
• much easier to unit test
• time-consuming
implementation
• additional patterns
- ORM
- data source
Disadvantages:Advantages:
17
SUMMARY
programmingprogramming
languagelanguage
scalabilityscalability
modularitymodularity
testabletestable
(integration test)(integration test)
time-consumingtime-consuming
size of projectsize of project
skill of the developers
18
Don’t be STUPID,
GRASP SOLID!
19
Sorry, but your code is
STUPID!
20
1.class DB 
2.{  
3.    private static $instance;
4.    public static function getInstance()
5.    {  
6.        if(!isset(self::$instance)) {  
7.            self::$instance = new self;  
8.        }  
9.          
10.        return self::$instance;  
11.    }  
12.}  
•Singleton
21
Dupleton?
21
•Singleton (ctd)
1.class DB 
2. {  
3.     private static $instance;
•     public static function getInstance()
•     {  
•         if(!isset(self::$instance)) {  
•             self::$instance = new self;  
•         }  
•           
•         return self::$instance;  
•     }  
• }  
• class Order 
• {  
•     protected $db;
•     public function __construct()
•     {  
•         $this->db = DB::getInstance();           
•     }  
• }
22
•Singleton (ctd)
1.class DB 
2. {
3.     //body
4. }
5.class Order 
6. {  
•     protected $db;
•     public function __construct(DB $db)
•     {  
•         $this->db = $db;           
•     }  
• }
23
•Tight Coupling
1.Order::buy();
1.class House 
2. {
3.     public function __construct()
•     {
•          $this->door = new Door();
•          $this->window = new Window();
•     }
• }
1.class House 
2. {
3.     public function __construct(Door $door, Window 
$window)
•     {
•          $this->door = $door;
•          $this->window = $window;
•     }
• }
24
•Untestable Code
I donI don’’tt
have ahave a
time!time!
25
Never Make Code
Faster Than Necessary,
Something Important Is
Always Lost When You Do
•Premature Optimization
26
•Premature Optimization (ctd)
PerformancePerformance
problemsproblems
20%20% of codeof code
27
•Indescriptive Naming
char * strpbrk ( const char *, const char * ); 
??
28
•Indescriptive Naming (ctd)
Code is Read Far More Often Than Written
29
DRY (Don’t Repeat Yourself!)
KISS (Keep It Smile, Stupid!)
•Duplication
30
•Singleton
•Tight Coupling
•Untestable Code
•Premature Optimization
•Indescriptive Naming
•Duplication
31
•Single Responsibility Principle
•Open/Closed Principle
•Liskov Substitution Principle
•Interface Segregation Principle
•Dependency Inversion Principle
•Single Responsibility Principle
•Open/Closed Principle
•Liskov Substitution Principle
•Interface Segregation Principle
•Dependency Inversion Principle
32
33
Test Driven-
Development
34
Advantages of Design
Pattern?
• speed up the development process,
• helps to prevent issues that can cause
major problems,
• patterns allow developers to communicate
using well-known, well understood names
for software interactions
35
EoT
ThankYou

Weitere ähnliche Inhalte

Ähnlich wie Design Patterns Summary

WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег ЗинченкоWebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег ЗинченкоGeeksLab Odessa
 
Managing data workflows with Luigi
Managing data workflows with LuigiManaging data workflows with Luigi
Managing data workflows with LuigiTeemu Kurppa
 
Nodejs Chapter 3 - Design Pattern
Nodejs Chapter 3 - Design PatternNodejs Chapter 3 - Design Pattern
Nodejs Chapter 3 - Design PatternTalentica Software
 
DIとトレイとによるAndroid開発の効率化
DIとトレイとによるAndroid開発の効率化DIとトレイとによるAndroid開発の効率化
DIとトレイとによるAndroid開発の効率化Tomoharu ASAMI
 
Android App Architecture with modern libs in practice. Our way in R.I.D., Ser...
Android App Architecture with modern libs in practice. Our way in R.I.D., Ser...Android App Architecture with modern libs in practice. Our way in R.I.D., Ser...
Android App Architecture with modern libs in practice. Our way in R.I.D., Ser...Sigma Software
 
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023Nicolas HAAN
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascriptAyush Sharma
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design PatternsLilia Sfaxi
 
Design patterns
Design patternsDesign patterns
Design patternsnisheesh
 
10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboardsDenis Ristic
 
Design patterns for fun and profit
Design patterns for fun and profitDesign patterns for fun and profit
Design patterns for fun and profitNikolas Vourlakis
 
DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)Oleg Zinchenko
 
Introduction to Using PHP & MVC Frameworks
Introduction to Using PHP & MVC FrameworksIntroduction to Using PHP & MVC Frameworks
Introduction to Using PHP & MVC FrameworksGerald Krishnan
 
PHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodePHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodeSWIFTotter Solutions
 
The Key to Machine Learning is Prepping the Right Data with Jean Georges Perrin
The Key to Machine Learning is Prepping the Right Data with Jean Georges Perrin The Key to Machine Learning is Prepping the Right Data with Jean Georges Perrin
The Key to Machine Learning is Prepping the Right Data with Jean Georges Perrin Databricks
 

Ähnlich wie Design Patterns Summary (20)

WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег ЗинченкоWebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
 
Managing data workflows with Luigi
Managing data workflows with LuigiManaging data workflows with Luigi
Managing data workflows with Luigi
 
What is DDD and how could it help you
What is DDD and how could it help youWhat is DDD and how could it help you
What is DDD and how could it help you
 
Nodejs Chapter 3 - Design Pattern
Nodejs Chapter 3 - Design PatternNodejs Chapter 3 - Design Pattern
Nodejs Chapter 3 - Design Pattern
 
DIとトレイとによるAndroid開発の効率化
DIとトレイとによるAndroid開発の効率化DIとトレイとによるAndroid開発の効率化
DIとトレイとによるAndroid開発の効率化
 
Android App Architecture with modern libs in practice. Our way in R.I.D., Ser...
Android App Architecture with modern libs in practice. Our way in R.I.D., Ser...Android App Architecture with modern libs in practice. Our way in R.I.D., Ser...
Android App Architecture with modern libs in practice. Our way in R.I.D., Ser...
 
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
getting-your-groovy-on
getting-your-groovy-ongetting-your-groovy-on
getting-your-groovy-on
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Introduction to Domain-Driven Design
Introduction to Domain-Driven DesignIntroduction to Domain-Driven Design
Introduction to Domain-Driven Design
 
10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards
 
Design patterns for fun and profit
Design patterns for fun and profitDesign patterns for fun and profit
Design patterns for fun and profit
 
DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)
 
L07 Frameworks
L07 FrameworksL07 Frameworks
L07 Frameworks
 
Introduction to Using PHP & MVC Frameworks
Introduction to Using PHP & MVC FrameworksIntroduction to Using PHP & MVC Frameworks
Introduction to Using PHP & MVC Frameworks
 
PHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodePHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better Code
 
L09 Frameworks
L09 FrameworksL09 Frameworks
L09 Frameworks
 
The Key to Machine Learning is Prepping the Right Data with Jean Georges Perrin
The Key to Machine Learning is Prepping the Right Data with Jean Georges Perrin The Key to Machine Learning is Prepping the Right Data with Jean Georges Perrin
The Key to Machine Learning is Prepping the Right Data with Jean Georges Perrin
 

Kürzlich hochgeladen

How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 

Kürzlich hochgeladen (20)

Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 

Design Patterns Summary

  • 2. 2 Bibliography • Martin Fowler,Patterns of Enterprise Appication Architecture • Matt Zandstra , PHP5 Objects, Patters, and Practice • http://www.ustream.tv - NewYork PHP channel • Symfony Live Berlin 2012 - https://joind.in/event/view/1114 • http://scholar.google.pl • http://pzielinski.com
  • 4. 4 Breakdown Design Patterns by GoF Design PatternsDesign Patterns creationalcreational structuralstructural behavioralbehavioral - abstract factory - builder - prototype - decorator - facade - composite - observer - strategy - command
  • 5. 5
  • 6. 6
  • 7. 7 Implementation related to the context programmingprogramming languagelanguage scalabilityscalability modularitymodularity testabletestable (integration test)(integration test) time-consumingtime-consuming size of projectsize of project
  • 8. 8 ContextContext ISIS related to therelated to the programmingprogramming languagelanguage Design patternsDesign patterns IS NOTIS NOT relatedrelated to theto the programmingprogramming languagelanguage
  • 9. 9 Transaction Script1. class Hotel   2. {    3.     private     4.         $_gateway; 5.     public function __construct(Data_Access_Gateway $gateway)     6.     {     7.         $this->_gateway = $gateway;     8.     }     9.    10.     public function bookRoom($userId, $fromDate, $toDate)     11.     {     12.         $roomId = $this->_gateway->_getRoomIdBetweenDates($fromDate, $toDate);     13.    14.         if (!$roomId) {     15.             return false;     16.         }     17.    18.         $days = $this->_getAmountOfDays($fromDate, $toDate);     19.    20.         if ($days < = 7) {   21.             $price = $days * 100;   22.         } else {    23.             $price = $days * 80;   24.         }   25.            26.         $data = array(   27.             'userId' => $userId,     28.             'roomId' => $roomId,   29.             'fromDate' => $fromDate,     30.             'toDate' => $toDate,     31.             'price' => $price,     32.         );     33.    34.         $bookingId = $this->_gateway->insert('bookings', $data);     35.    36.         return $bookingId;     37.     }     38. } 
  • 10. 10 Transaction Script • simple procedural model • works well with a simple data access layer • easy implementation of use cases • difficult to maintenance • code duplication Disadvantages:Advantages:
  • 11. 11 Table Module #11. class Hotel   2. {     3.     public function __construct(Data_Access_Gateway $gateway, Booking $booking)     •     {     •         $this->_gateway = $gateway;   •         $this->_booking = $booking;   •     }     •    •     public function bookRoom($userId, $fromDate, $toDate)     •     {     •         $roomId = $this->_booking->getRoomBetweenDates($fromDate, $toDate);   •    •         if (!$roomId) {     •             return false;     •         }     •    •         $days = $this->_getAmountOfDays($fromDate, $toDate);     •    •         if ($days < = 7) {   •             $price = $days * 100;   •         } else {    •             $price = $days * 80;   •         }   •            •         $bookingId = $this->_booking- >addBooking($userId, $roomId, $fromDate, $toDate, $price);     •    •         return $bookingId;     •     }     • }   •   
  • 12. 12 Table Module #2 1. class Booking   2. {     3.     public function __construct(Data_Access_Gateway $gateway)     4.     {     5.         $this->_gateway = $gateway;     6.     }   7.        8.     public function getRoomBetweenDates($dateFrom, $dateTo)   9.     {   10.        return $this->_gateway->getRoomBetweenDates($dateFrom, $dateTo);   11.    }   12.       13.    public function addBooking($userId, $roomId, $fromDate, $toDate, $price)     14.    {     15.        $data = array(   16.            'userId' => $userId,     17.            'roomId' => $roomId,   18.            'fromDate' => $fromDate,     19.            'toDate' => $toDate,     20.            'price' => $price,     21.        );     22.   23.        $bookingId = $this->_gateway->insert('bookings', $data);     24.   25.        return $bookingId;     26.    }     27.}  
  • 13. 13 Table Module vs Transaction Script • less duplication • encapsulation • more organized and structured code • easy implementation by technology support • weak support for polymorphism • no support ORM Disadvantages:Advantages:
  • 14. 14 Domain Model #11. class Hotel   2. {     3.     protected $_hotelId;   4.     protected $_rooms;   5.        6.     public function bookRoom(User $user, $fromDate, $toDate)     7.     {     8.         $room = $this->_getRoomBetweenDates($fromDate, $toDate);   9.    10.         if (is_null($room)) {     11.             return false;     12.         }     13.    14.         $booking = $room->bookRoom(User $user, $fromDate, $toDate);   15.    16.         return $booking;     17.     }   18. }   19.    20. class Room   21. {   22.     protected $_roomId;   23.     protected $_bookings = array();   24.        25.     public function bookRoom(User $user, $fromDate, $toDate)   26.     {   27.         $days = $this->_getAmountOfDays($fromDate, $toDate);   28.            29.         if ($days < = 7) {   30.             $booking = new Booking($user, new ShortBookingStrategy($user, $days));   31.         } else {    32.             $booking = new Booking($user, new NormalBookingStrategy($user, $days));   33.         }   34.            35.         return $booking;   36.     }   37. } 
  • 15. 15 Domain Model #2 1. class NormalBookingPriceStrategy extends BookingPriceStrategy   2. {   3.     public function getPrice()   4.     {   5.         $price = $this->_days * 80;   6.            7.         if ($this->_user->isLoyal()) {   8.             $price = $price / 2;   9.         }   10.           11.        return $price;   12.    }   13.}   14.   15.class ShortBookingPriceStrategy extends BookingPriceStrategy   16.{     17.    public function getPrice()   18.    {   19.        return $this->_days * 100;   20.    }   21.}
  • 16. 16 Domain Model vs Procedural Pattern • prevents logic duplication • more code readability • independence from the data source • much easier to unit test • time-consuming implementation • additional patterns - ORM - data source Disadvantages:Advantages:
  • 19. 19 Sorry, but your code is STUPID!
  • 20. 20 1.class DB  2.{   3.    private static $instance; 4.    public static function getInstance() 5.    {   6.        if(!isset(self::$instance)) {   7.            self::$instance = new self;   8.        }   9.           10.        return self::$instance;   11.    }   12.}   •Singleton 21 Dupleton?
  • 21. 21 •Singleton (ctd) 1.class DB  2. {   3.     private static $instance; •     public static function getInstance() •     {   •         if(!isset(self::$instance)) {   •             self::$instance = new self;   •         }   •            •         return self::$instance;   •     }   • }   • class Order  • {   •     protected $db; •     public function __construct() •     {   •         $this->db = DB::getInstance();            •     }   • }
  • 22. 22 •Singleton (ctd) 1.class DB  2. { 3.     //body 4. } 5.class Order  6. {   •     protected $db; •     public function __construct(DB $db) •     {   •         $this->db = $db;            •     }   • }
  • 23. 23 •Tight Coupling 1.Order::buy(); 1.class House  2. { 3.     public function __construct() •     { •          $this->door = new Door(); •          $this->window = new Window(); •     } • } 1.class House  2. { 3.     public function __construct(Door $door, Window  $window) •     { •          $this->door = $door; •          $this->window = $window; •     } • }
  • 24. 24 •Untestable Code I donI don’’tt have ahave a time!time!
  • 25. 25 Never Make Code Faster Than Necessary, Something Important Is Always Lost When You Do •Premature Optimization
  • 28. 28 •Indescriptive Naming (ctd) Code is Read Far More Often Than Written
  • 29. 29 DRY (Don’t Repeat Yourself!) KISS (Keep It Smile, Stupid!) •Duplication
  • 30. 30 •Singleton •Tight Coupling •Untestable Code •Premature Optimization •Indescriptive Naming •Duplication
  • 31. 31 •Single Responsibility Principle •Open/Closed Principle •Liskov Substitution Principle •Interface Segregation Principle •Dependency Inversion Principle •Single Responsibility Principle •Open/Closed Principle •Liskov Substitution Principle •Interface Segregation Principle •Dependency Inversion Principle
  • 32. 32
  • 34. 34 Advantages of Design Pattern? • speed up the development process, • helps to prevent issues that can cause major problems, • patterns allow developers to communicate using well-known, well understood names for software interactions