SlideShare ist ein Scribd-Unternehmen logo
1 von 61
Hello there!
Who am I?
Yoan-Alexander Grigorov
PHP developer (with pride!)
Software Engineer
Critic
Design Patterns adventurer
SOLID design?
Imagine code as a painting
Well... maybe not this one in particular!
Now that's better!
Nothing against modern art, but...
Code should be easy to recognize!
How a project starts:
Several months later...
What is going wrong?
● Frequently logic changes are made with “hacks”
without refactoring
● Lack of knowledge on … stuff
● Lack of discipline
SOLID? What's that?
● Single responsibility
● Open-closed
● Liskov substitution
● Interface segregation
● Dependency inversion
What SOLID can't do?
● Cure ebola
● Explain why is there PHP 7 coming out, but no PHP
6?!?!?
● Make Bulgarian politicians suck less
Let's break it down!
Single responsibility principle
Every variable, class, function, etc. should have a single
responsibility, described in it's name (if any).
Single responsibility principle
Basically it's bad if we have a function (or class)
that does more then what it's name says.
Single responsibility principle
Here is one real-life example how bad it is to break
this rule:
Single responsibility principle
Hidden features can have unexpected effects
Single responsibility principle
● Bad code example:
<?php
class InsurancePDFCreator {
// … some code
public function createPDFContents(Policy $policy);
public function downloadInsuranceCompanyLogos();
}
Single responsibility principle
● Good code example:
<?php
class InsurancePDFCreator {
// … some code
public function createPDFContents(Policy $policy);
}
// somewhere else
class InsuranceCompanyLogosDownloader {
public function download();
}
Conclusion
● Define your desired contexts carefully
● Make sure that there is only one responsibility for a
operation, class, method, module, etc.
● If you need to have a more abstract operations that
combine several stuff, find for them more abstract
names.
● And... Single Responsibility Principle is not
enough
Interface Segregation Principle
No system (module, class) should be required to depend
on methods it does not use.
Interface Segregation Principle
This is pretty much an extension to the SRP
Interface Segregation Principle
● Avoid having classes of “mothership” type
● Avoid the “Manager” type classes
Interface Segregation Principle
● Imagine this code:
<?php
class UsersManager {
public function createUser($username, $email);
public function deleteUserPicture($userId);
public function notifyUser($userId, $msg);
}
Interface Segregation Principle
It's bad because:
● It's provides a general-purpose interface
● When you include this class, you usually call one of
those methods
Interface Segregation Principle
The good way to do it:
● Use a set of client-specific classes (interfaces)
instead of one general-purpose interface
● Be more specific in your context
Interface Segregation Principle
Good example:
<?php
class UserCreator {}
class UserPictureDeleter {}
class UserNotifier {}
Conclusion
● Is extension to the Single Responsibility Principle
● Favored usage of client-specific interfaces, rather
then one general-purpose (mothership) interface
Open-Closed Principle
Classes should be opened for extension, but closed for
modification
Open-Closed Principle
<?php
class ProductDiscountCalculator {
public function calculateDiscount(Product $product)
{
// some serious business here for $discountRate
return $product->getPrice() / $discountRate;
}
}
Open-Closed Principle
<?php
// somewhere else a naughty developer writes this:
class MyProductDiscountCalc extends ProductDiscountCalculator {
public function calculateDiscount(Product $product)
{
// Hardcoded discount here
return 300;
}
}
Open-Closed Principle
Why is this so bad?
● ProductDiscountCalculator is not an interface, but a
specific class
● We can push into usage it's replacement
● The code relies on ProductDiscountCalculator, but
it's children are changing the behavior and this is
bad
Open-Closed Principle
REMEMBER!
● Modifying (overriding) existing methods is bad
● Extending them (e.g. add new methods or implement
abstract methods) is good
Open-Closed Principle
“But who is using specific types anyways? Can't we just use
interfaces?”
- Yes, that's why we should look at the next principles.
Open-Closed Principle
I bet you are puzzled now!
Don't be afraid, open-closed principle is used best
with his fellow brother the Liskov Substitution
principle!
Open-Closed Principle
● It bit better way to do it:
class MyProductDiscountCalc extends ProductDiscountCalculator {
public function myCalculateDiscount(Product $product)
{
// Hardcoded discount here
return 300;
}
}
Open-Closed Principle
Not the best way, but Open-Closed principle works
best with his friends from SOLID!
Liskov Substitution Principle
Subtypes could replace their parents, and the program
should have the same behaviour.
Liskov Substitution Principle
Basically we must make sure that new derived
classes are extending the base classes without
changing their behavior. It's that easy.
???
Liskov Substitution Principle
As ridiculous as it may sound I'll explain you what
it's the meaning behind this.
Liskov Substitution Principle
Let's remember the previous example:
class ProductDiscountCalculator {
public function calculateDiscount(Product $product);
}
class MyProductDiscountCalculator ...
public function myCalculateDiscount(Product $product);
}
Liskov Substitution Principle
How do you know which is which?
<?php
if ($calc instanceof ProductDiscountCalculator) {
$calc->calculateDiscount($product);
}
if ($calc instanceof MyProductDiscountCalculator) {
$calc->myCalculateDiscount($product);
}
Liskov Substitution Principle
I suppose you don't do such horrible things like
type checking
Liskov Substitution Principle
● One solution is to use abstract method:
<?php
abstract class AbstractDiscountCalc {
public function calculateDiscount(Product $product) {
return $this->getDiscountForProduct($product);
}
abstract protected function
getDiscountForProduct(Product $product);
}
Liskov Substitution Principle
<?php
class DefaultProductDiscountCalc extends AbstractDiscountCalc
{
protected function getDiscountForProduct(Product $product)
{
// our main algorithm for $discountRate
return $product->getPrice / $discountRate;
}
}
Liskov Substitution Principle
<?php
class MyProductDiscountCalc extends AbstractDiscountCalc
{
protected function getDiscountForProduct(Product $product)
{
// Fixed discount
return 300;
}
}
Liskov Substitution Principle
Still not the best way, but much better!
Dependency inversion principle
One system should depend on abstractions (interfaces)
rather then concretions
Dependency inversion principle
Remember Dependency Injection?
Pretty much DI is following the Dependency
Inversion principle
Dependency inversion principle
● So our previous example would use an interface:
<?php
interface DiscountCalculator {
public function calculateDiscount(Product $product);
}
Dependency inversion principle
This way we depend on interfaces, not on specific
classes (which is the main idea behind interfaces)
Dependency inversion principle
You can still define a mid-level abstract class to
implement just some parts of the logic.
SOLID and others
● Use SOLID with TDD
● You can make DDD even better with SOLID
● SOLID principles are universal
Thank you for listening!
● joan.grigorov@gmail.com
● @YoanDev
● http://bgscripts.com

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (8)

Learning solid principles using c#
Learning solid principles using c#Learning solid principles using c#
Learning solid principles using c#
 
Object Oriented Design SOLID Principles
Object Oriented Design SOLID PrinciplesObject Oriented Design SOLID Principles
Object Oriented Design SOLID Principles
 
The OO Design Principles
The OO Design PrinciplesThe OO Design Principles
The OO Design Principles
 
SOLID design principles applied in Java
SOLID design principles applied in JavaSOLID design principles applied in Java
SOLID design principles applied in Java
 
Design patterns illustrated-2015-03
Design patterns illustrated-2015-03Design patterns illustrated-2015-03
Design patterns illustrated-2015-03
 
OO design principles & heuristics
OO design principles & heuristicsOO design principles & heuristics
OO design principles & heuristics
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
Solid principle
Solid principleSolid principle
Solid principle
 

Andere mochten auch (7)

A brief look inside UML
A brief look inside UMLA brief look inside UML
A brief look inside UML
 
Introduction to Domain-Driven Design
Introduction to Domain-Driven DesignIntroduction to Domain-Driven Design
Introduction to Domain-Driven Design
 
Software_Architectures_from_SOA_to_MSA
Software_Architectures_from_SOA_to_MSASoftware_Architectures_from_SOA_to_MSA
Software_Architectures_from_SOA_to_MSA
 
Don't be STUPID, Grasp SOLID - North East PHP
Don't be STUPID, Grasp SOLID - North East PHPDon't be STUPID, Grasp SOLID - North East PHP
Don't be STUPID, Grasp SOLID - North East PHP
 
Zero to SOLID
Zero to SOLIDZero to SOLID
Zero to SOLID
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
 
WebSockets with PHP: Mission impossible
WebSockets with PHP: Mission impossibleWebSockets with PHP: Mission impossible
WebSockets with PHP: Mission impossible
 

Ähnlich wie SOLID design

SOLID principles-Present
SOLID principles-PresentSOLID principles-Present
SOLID principles-Present
Quang Nguyen
 
Front-End Modernization for Mortals
Front-End Modernization for MortalsFront-End Modernization for Mortals
Front-End Modernization for Mortals
cgack
 

Ähnlich wie SOLID design (20)

Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
 
Oop principles
Oop principlesOop principles
Oop principles
 
2009 Dotnet Information Day: More effective c#
2009 Dotnet Information Day: More effective c#2009 Dotnet Information Day: More effective c#
2009 Dotnet Information Day: More effective c#
 
SOLID
SOLIDSOLID
SOLID
 
Ood and solid principles
Ood and solid principlesOod and solid principles
Ood and solid principles
 
OOP vs COP
OOP vs COPOOP vs COP
OOP vs COP
 
SOLID principles-Present
SOLID principles-PresentSOLID principles-Present
SOLID principles-Present
 
Inversion of Control
Inversion of ControlInversion of Control
Inversion of Control
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
 
Evgeniy Khyst - why does software design matter and how to keep it in good shape
Evgeniy Khyst - why does software design matter and how to keep it in good shapeEvgeniy Khyst - why does software design matter and how to keep it in good shape
Evgeniy Khyst - why does software design matter and how to keep it in good shape
 
Agile design pattern
Agile design patternAgile design pattern
Agile design pattern
 
Single Responsibility Principle
Single Responsibility PrincipleSingle Responsibility Principle
Single Responsibility Principle
 
Front-End Modernization for Mortals
Front-End Modernization for MortalsFront-End Modernization for Mortals
Front-End Modernization for Mortals
 
Front end-modernization
Front end-modernizationFront end-modernization
Front end-modernization
 
Front end-modernization
Front end-modernizationFront end-modernization
Front end-modernization
 
Use Design Principle to Improve code quality
Use Design Principle to Improve code qualityUse Design Principle to Improve code quality
Use Design Principle to Improve code quality
 
Object Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshopObject Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshop
 
Object-oriented design principles
Object-oriented design principlesObject-oriented design principles
Object-oriented design principles
 
Sec1_SOLID Principles_Software Engineering.pptx
Sec1_SOLID Principles_Software Engineering.pptxSec1_SOLID Principles_Software Engineering.pptx
Sec1_SOLID Principles_Software Engineering.pptx
 
Object Oriented Concepts in Real Projects
Object Oriented Concepts in Real ProjectsObject Oriented Concepts in Real Projects
Object Oriented Concepts in Real Projects
 

Kürzlich hochgeladen

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
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 

Kürzlich hochgeladen (20)

%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
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
 
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
 
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 🔝✔️✔️
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.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 🔝✔️✔️
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
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
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 

SOLID design