SlideShare a Scribd company logo
1 of 23
Download to read offline
SOLID
OOP paradigms
● Basic principles
○ Favor Composition over Inheritance
○ Law of Demeter
○ Dependency Injection
● S.O.L.I.D. principles
SOLID
OOP paradigms
SOLID
Favor Composition over Inheritance
● Inheritance generates LOTS of problems
○ Ex: Circle-Ellipse problem
■ Class Ellipse with radiusX, radiusY
■ Class Circle extends from Ellipse
■ later add stretchX()
SOLID
Favor Composition over Inheritance
● Minimize coupling (least knowledge principle)
● In a class method you can use:
○ 1. Object itself
○ 2. Method’s parameters
○ 3. Objects created inside
○ 4. Any class’ components (properties+methods)
SOLID
Law of Demeter (LoD)
● “Don’t ask your dog to move legs but walk”
○ As code: dog.leg.move()
○ You do not care how your dog walk
○ Only the dog knows the way it walks
SOLID
Law of Demeter (LoD)
● What if the XML is located in a DB?
SOLID
Dependency Injection
class FeedParser
{
public function __construct($filename)
{
$this->XmlFilename = $filename;
}
public function doParse()
{
$xmlData = $this->readXML($this->XmlFilename);
$products = $this->extractProductsFromXmlData($xmlData);
return $products;
}
private function readXML($filename)
{
$xmlData = simplexml_load_file($filename);
//(... Guard clauses ...)
return $xmlData;
}
● One object supplies the dependencies of another object
SOLID
Dependency Injection
class FeedParser
{
private $xmlReader;
public function __construct(XmlReader $xmlReader)
{
$this->xmlReader = $xmlReader;
}
public function doParse()
{
$xmlData = $this->readXML();
$products = $this->extractProductsFromXmlData($xmlData);
return $products;
}
private function readXML()
{
$xmlData = $this->xmlReader->read();
//(... Guard clauses ...)
● When a new object is build, you can set concrete dependencies
SOLID
Dependency Injection
class FeedParser
{
private $xmlReader;
public function __construct(XmlReader $xmlReader)
{
$this->xmlReader = $xmlReader;
}
//(...)
}
class XmlReader
{
//(...)
}
$myXmlReader = new XMLReader($filename);
$myParser = new FeedParser($myXmlReader);
● Improves decoupling
● Avoid hardcoding dependencies
● [Hint] Look for:
○ new Class()
○ build-in functions
■ Ex. date(), fopen()
SOLID
Dependency Injection
● Mnemonic acronym (by Feathers)
● “first five principles” of OOP design (by Uncle Bob)
SOLID
SOLID
● A class should have only a single responsibility
SOLID
S: Single Responsibility Principle (SRP)
public function doParse()
{
$xmlData = $this->readXML();
$products = $this->extractProductsFromXmlData($xmlData);
return $products;
}
private function readXML()
{
$this->checkFileExists($filename);
$xmlData = $this->xmlReader->read();
$this->checkXmlHasContent($xmlData);
return $xmlData;
}
● A class should have only a single responsibility
SOLID
S: Single Responsibility Principle (SRP)
public function doParse()
{
$xmlData = $this->xmlReader->read();
$products = $this->extractProductsFromXmlData($xmlData);
return $products;
}
private function readXML()
{
$this->checkFileExists($filename);
$xmlData = $this->xmlReader->read();
$this->checkXmlHasContent($xmlData);
return $xmlData;
}
● Software entities (classes, modules, etc) should be:
○ Open for extension
■ Doesn’t mean inheritance
○ Closed for modification
■ No need to touch the original code
● How could we do so?
SOLID
O: Open-Closed Principle
● What if our data comes from a CSV? And later from a JSON?
SOLID
O: Open-Closed Principle
class FeedParser
{
private $xmlReader;
public function __construct(XmlReader $xmlReader)
{
$this->xmlReader = $xmlReader;
}
public function doParse()
{
$xmlData = $this->xmlReader->read();
//(...)
SOLID
O: Open-Closed Principle, using interfaces!
class FeedParser
{
private $reader;
public function __construct(DataReaderWriter $reader)
{
$this->reader = $reader;
}
public function doParse()
{
$xmlData = $this->reader->read();
//(...)
}
}
interface DataReaderWriter
{
public function read();
public function write($data);
}
class XMLReaderWriter implements DataReaderWriter
{ // ... implement read() and write() }
● If ChildClass is a subtype of ParentClass, then a ParentClass object
can replace a ChildClass object
● Remember Circle-Ellipse problem
● “Avoid most inheritance cases”
SOLID
L: Liskov Substitution Principle
● No client should be forced to depend on methods it doesn’t use
● “Split a large interface in separated small interfaces”
SOLID
I: Interface Segregation Principle
SOLID
I: Interface Segregation Principle
class FeedParser
{
private $reader;
public function __construct(DataReaderWriter $reader)
{
$this->reader = $reader;
}
public function doParse()
{
$xmlData = $this->reader->read();
//(...)
}
}
interface DataReaderWriter
{
public function read();
public function write($data);
}
class XMLReaderWriter implements DataReaderWriter
{ // ... implement read() and write() }
SOLID
I: Interface Segregation Principle
class FeedParser
{
private $reader;
public function __construct(DataReader $reader)
{
$this->reader = $reader;
}
public function doParse()
{
$xmlData = $this->reader->read();
//(...)
}
}
interface DataReader
{
public function read();
}
class XMLReaderWriter implements DataReader, DataWriter
{ // ... implement read() and write() }
● High-level modules should not depend on low-level modules.
Both should depend on abstractions.
● Does it look familiar?
SOLID
D: Dependency Inversion Principle
● We have already used it!
SOLID
D: Dependency Inversion Principle
class FeedParser
{
private $xmlReader;
public function __construct(XmlReader $xmlReader)
{
$this->xmlReader = $xmlReader;
}
//-----
class FeedParser
{
private $reader;
public function __construct(DataReaderWriter $reader)
{
$this->reader = $reader;
}
● Books:
○ Head First OO Analysis and Design (McLaughlin)
○ Head First Design Patterns (Freeman)
● Internet:
○ https://sites.google.com/site/unclebobconsultingllc/getting-a-solid-start
■ About SOLID: “They are not laws. They are not perfect truths.”
SOLID
References

More Related Content

What's hot

Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksLithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksNate Abele
 
Crafting beautiful software
Crafting beautiful softwareCrafting beautiful software
Crafting beautiful softwareJorn Oomen
 
PHP Traits
PHP TraitsPHP Traits
PHP Traitsmattbuzz
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium AppsNate Abele
 
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonfRafael Dohms
 
The State of Lithium
The State of LithiumThe State of Lithium
The State of LithiumNate Abele
 
Object Calisthenics Applied to PHP
Object Calisthenics Applied to PHPObject Calisthenics Applied to PHP
Object Calisthenics Applied to PHPGuilherme Blanco
 
Your code sucks, let's fix it
Your code sucks, let's fix itYour code sucks, let's fix it
Your code sucks, let's fix itRafael Dohms
 
Java and XML Schema
Java and XML SchemaJava and XML Schema
Java and XML SchemaRaji Ghawi
 
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...James Titcumb
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownpartsBastian Feder
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHPAhmed Swilam
 
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo EditionLithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo EditionNate Abele
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologyDaniel Knell
 
PHP for Adults: Clean Code and Object Calisthenics
PHP for Adults: Clean Code and Object CalisthenicsPHP for Adults: Clean Code and Object Calisthenics
PHP for Adults: Clean Code and Object CalisthenicsGuilherme Blanco
 
The Zen of Lithium
The Zen of LithiumThe Zen of Lithium
The Zen of LithiumNate Abele
 

What's hot (20)

Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksLithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate Frameworks
 
Crafting beautiful software
Crafting beautiful softwareCrafting beautiful software
Crafting beautiful software
 
Php & my sql
Php & my sqlPhp & my sql
Php & my sql
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
PHP Traits
PHP TraitsPHP Traits
PHP Traits
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium Apps
 
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf
 
Php variables
Php variablesPhp variables
Php variables
 
The State of Lithium
The State of LithiumThe State of Lithium
The State of Lithium
 
Object Calisthenics Applied to PHP
Object Calisthenics Applied to PHPObject Calisthenics Applied to PHP
Object Calisthenics Applied to PHP
 
Your code sucks, let's fix it
Your code sucks, let's fix itYour code sucks, let's fix it
Your code sucks, let's fix it
 
Java and XML Schema
Java and XML SchemaJava and XML Schema
Java and XML Schema
 
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHP
 
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo EditionLithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technology
 
PHP for Adults: Clean Code and Object Calisthenics
PHP for Adults: Clean Code and Object CalisthenicsPHP for Adults: Clean Code and Object Calisthenics
PHP for Adults: Clean Code and Object Calisthenics
 
The Zen of Lithium
The Zen of LithiumThe Zen of Lithium
The Zen of Lithium
 

Viewers also liked

Advanced PHP Concepts - Tutorial 2 of 3
Advanced PHP Concepts - Tutorial 2 of 3Advanced PHP Concepts - Tutorial 2 of 3
Advanced PHP Concepts - Tutorial 2 of 3James Whitehead
 
Object Oriented Terms
Object Oriented TermsObject Oriented Terms
Object Oriented Termsguest3c69aefa
 
Procedural to oop in php
Procedural to oop in phpProcedural to oop in php
Procedural to oop in phpBarrett Avery
 
Introduction to Docker
Introduction to DockerIntroduction to Docker
Introduction to DockerJulio Martinez
 
Clean code presentation
Clean code presentationClean code presentation
Clean code presentationBhavin Gandhi
 
Professional development
Professional developmentProfessional development
Professional developmentJulio Martinez
 
Solid and ioc principles
Solid and ioc principlesSolid and ioc principles
Solid and ioc principleseleksdev
 
Advanced java practical semester 6_computer science
Advanced java practical semester 6_computer scienceAdvanced java practical semester 6_computer science
Advanced java practical semester 6_computer scienceNiraj Bharambe
 
Practical OOP In Java
Practical OOP In JavaPractical OOP In Java
Practical OOP In Javawiradikusuma
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Javabackdoor
 
Object oriented programming (oop) cs304 power point slides lecture 01
Object oriented programming (oop)   cs304 power point slides lecture 01Object oriented programming (oop)   cs304 power point slides lecture 01
Object oriented programming (oop) cs304 power point slides lecture 01Adil Kakakhel
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programmingSachin Sharma
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingMoutaz Haddara
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
 

Viewers also liked (20)

OOP & SOLID
OOP & SOLIDOOP & SOLID
OOP & SOLID
 
Advanced PHP Concepts - Tutorial 2 of 3
Advanced PHP Concepts - Tutorial 2 of 3Advanced PHP Concepts - Tutorial 2 of 3
Advanced PHP Concepts - Tutorial 2 of 3
 
Object Oriented Terms
Object Oriented TermsObject Oriented Terms
Object Oriented Terms
 
Procedural to oop in php
Procedural to oop in phpProcedural to oop in php
Procedural to oop in php
 
Introduction to Docker
Introduction to DockerIntroduction to Docker
Introduction to Docker
 
Clean code presentation
Clean code presentationClean code presentation
Clean code presentation
 
Professional development
Professional developmentProfessional development
Professional development
 
Solid and ioc principles
Solid and ioc principlesSolid and ioc principles
Solid and ioc principles
 
Clean code
Clean codeClean code
Clean code
 
Advanced java practical semester 6_computer science
Advanced java practical semester 6_computer scienceAdvanced java practical semester 6_computer science
Advanced java practical semester 6_computer science
 
Clean Code
Clean CodeClean Code
Clean Code
 
Practical OOP In Java
Practical OOP In JavaPractical OOP In Java
Practical OOP In Java
 
OOP java
OOP javaOOP java
OOP java
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Java programming-examples
Java programming-examplesJava programming-examples
Java programming-examples
 
Object oriented programming (oop) cs304 power point slides lecture 01
Object oriented programming (oop)   cs304 power point slides lecture 01Object oriented programming (oop)   cs304 power point slides lecture 01
Object oriented programming (oop) cs304 power point slides lecture 01
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 

Similar to Some OOP paradigms & SOLID

Architecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designArchitecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designJean Michel
 
Advanced PHP Simplified - Sunshine PHP 2018
Advanced PHP Simplified - Sunshine PHP 2018Advanced PHP Simplified - Sunshine PHP 2018
Advanced PHP Simplified - Sunshine PHP 2018Mark Niebergall
 
Migration from Procedural to OOP
Migration from Procedural to OOP Migration from Procedural to OOP
Migration from Procedural to OOP GLC Networks
 
PHPID online Learning #6 Migration from procedural to OOP
PHPID online Learning #6 Migration from procedural to OOPPHPID online Learning #6 Migration from procedural to OOP
PHPID online Learning #6 Migration from procedural to OOPAchmad Mardiansyah
 
Dependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHPDependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHPmtoppa
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxDavidLazar17
 
Solid principles in action
Solid principles in actionSolid principles in action
Solid principles in actionArash Khangaldi
 
Revisiting SOLID Principles
Revisiting  SOLID Principles Revisiting  SOLID Principles
Revisiting SOLID Principles Anis Ahmad
 
Alexander Makarov "Let’s talk about code"
Alexander Makarov "Let’s talk about code"Alexander Makarov "Let’s talk about code"
Alexander Makarov "Let’s talk about code"Fwdays
 
Silex and Twig (PHP Dorset talk)
Silex and Twig (PHP Dorset talk)Silex and Twig (PHP Dorset talk)
Silex and Twig (PHP Dorset talk)Dave Hulbert
 
TWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonTWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonCodemotion
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingAhmed Swilam
 
Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#Svetlin Nakov
 

Similar to Some OOP paradigms & SOLID (20)

Architecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designArchitecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented design
 
Advanced PHP Simplified - Sunshine PHP 2018
Advanced PHP Simplified - Sunshine PHP 2018Advanced PHP Simplified - Sunshine PHP 2018
Advanced PHP Simplified - Sunshine PHP 2018
 
OOP
OOPOOP
OOP
 
Migration from Procedural to OOP
Migration from Procedural to OOP Migration from Procedural to OOP
Migration from Procedural to OOP
 
PHPID online Learning #6 Migration from procedural to OOP
PHPID online Learning #6 Migration from procedural to OOPPHPID online Learning #6 Migration from procedural to OOP
PHPID online Learning #6 Migration from procedural to OOP
 
SOLID
SOLIDSOLID
SOLID
 
SOLID
SOLIDSOLID
SOLID
 
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
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Dependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHPDependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHP
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptx
 
Solid principles in action
Solid principles in actionSolid principles in action
Solid principles in action
 
Revisiting SOLID Principles
Revisiting  SOLID Principles Revisiting  SOLID Principles
Revisiting SOLID Principles
 
Alexander Makarov "Let’s talk about code"
Alexander Makarov "Let’s talk about code"Alexander Makarov "Let’s talk about code"
Alexander Makarov "Let’s talk about code"
 
Silex and Twig (PHP Dorset talk)
Silex and Twig (PHP Dorset talk)Silex and Twig (PHP Dorset talk)
Silex and Twig (PHP Dorset talk)
 
TWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonTWINS: OOP and FP - Warburton
TWINS: OOP and FP - Warburton
 
Advanced PHP Simplified
Advanced PHP SimplifiedAdvanced PHP Simplified
Advanced PHP Simplified
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
FluentDom
FluentDomFluentDom
FluentDom
 
Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#
 

Recently uploaded

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

Recently uploaded (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Some OOP paradigms & SOLID

  • 2. ● Basic principles ○ Favor Composition over Inheritance ○ Law of Demeter ○ Dependency Injection ● S.O.L.I.D. principles SOLID OOP paradigms
  • 4. ● Inheritance generates LOTS of problems ○ Ex: Circle-Ellipse problem ■ Class Ellipse with radiusX, radiusY ■ Class Circle extends from Ellipse ■ later add stretchX() SOLID Favor Composition over Inheritance
  • 5. ● Minimize coupling (least knowledge principle) ● In a class method you can use: ○ 1. Object itself ○ 2. Method’s parameters ○ 3. Objects created inside ○ 4. Any class’ components (properties+methods) SOLID Law of Demeter (LoD)
  • 6. ● “Don’t ask your dog to move legs but walk” ○ As code: dog.leg.move() ○ You do not care how your dog walk ○ Only the dog knows the way it walks SOLID Law of Demeter (LoD)
  • 7. ● What if the XML is located in a DB? SOLID Dependency Injection class FeedParser { public function __construct($filename) { $this->XmlFilename = $filename; } public function doParse() { $xmlData = $this->readXML($this->XmlFilename); $products = $this->extractProductsFromXmlData($xmlData); return $products; } private function readXML($filename) { $xmlData = simplexml_load_file($filename); //(... Guard clauses ...) return $xmlData; }
  • 8. ● One object supplies the dependencies of another object SOLID Dependency Injection class FeedParser { private $xmlReader; public function __construct(XmlReader $xmlReader) { $this->xmlReader = $xmlReader; } public function doParse() { $xmlData = $this->readXML(); $products = $this->extractProductsFromXmlData($xmlData); return $products; } private function readXML() { $xmlData = $this->xmlReader->read(); //(... Guard clauses ...)
  • 9. ● When a new object is build, you can set concrete dependencies SOLID Dependency Injection class FeedParser { private $xmlReader; public function __construct(XmlReader $xmlReader) { $this->xmlReader = $xmlReader; } //(...) } class XmlReader { //(...) } $myXmlReader = new XMLReader($filename); $myParser = new FeedParser($myXmlReader);
  • 10. ● Improves decoupling ● Avoid hardcoding dependencies ● [Hint] Look for: ○ new Class() ○ build-in functions ■ Ex. date(), fopen() SOLID Dependency Injection
  • 11. ● Mnemonic acronym (by Feathers) ● “first five principles” of OOP design (by Uncle Bob) SOLID SOLID
  • 12. ● A class should have only a single responsibility SOLID S: Single Responsibility Principle (SRP) public function doParse() { $xmlData = $this->readXML(); $products = $this->extractProductsFromXmlData($xmlData); return $products; } private function readXML() { $this->checkFileExists($filename); $xmlData = $this->xmlReader->read(); $this->checkXmlHasContent($xmlData); return $xmlData; }
  • 13. ● A class should have only a single responsibility SOLID S: Single Responsibility Principle (SRP) public function doParse() { $xmlData = $this->xmlReader->read(); $products = $this->extractProductsFromXmlData($xmlData); return $products; } private function readXML() { $this->checkFileExists($filename); $xmlData = $this->xmlReader->read(); $this->checkXmlHasContent($xmlData); return $xmlData; }
  • 14. ● Software entities (classes, modules, etc) should be: ○ Open for extension ■ Doesn’t mean inheritance ○ Closed for modification ■ No need to touch the original code ● How could we do so? SOLID O: Open-Closed Principle
  • 15. ● What if our data comes from a CSV? And later from a JSON? SOLID O: Open-Closed Principle class FeedParser { private $xmlReader; public function __construct(XmlReader $xmlReader) { $this->xmlReader = $xmlReader; } public function doParse() { $xmlData = $this->xmlReader->read(); //(...)
  • 16. SOLID O: Open-Closed Principle, using interfaces! class FeedParser { private $reader; public function __construct(DataReaderWriter $reader) { $this->reader = $reader; } public function doParse() { $xmlData = $this->reader->read(); //(...) } } interface DataReaderWriter { public function read(); public function write($data); } class XMLReaderWriter implements DataReaderWriter { // ... implement read() and write() }
  • 17. ● If ChildClass is a subtype of ParentClass, then a ParentClass object can replace a ChildClass object ● Remember Circle-Ellipse problem ● “Avoid most inheritance cases” SOLID L: Liskov Substitution Principle
  • 18. ● No client should be forced to depend on methods it doesn’t use ● “Split a large interface in separated small interfaces” SOLID I: Interface Segregation Principle
  • 19. SOLID I: Interface Segregation Principle class FeedParser { private $reader; public function __construct(DataReaderWriter $reader) { $this->reader = $reader; } public function doParse() { $xmlData = $this->reader->read(); //(...) } } interface DataReaderWriter { public function read(); public function write($data); } class XMLReaderWriter implements DataReaderWriter { // ... implement read() and write() }
  • 20. SOLID I: Interface Segregation Principle class FeedParser { private $reader; public function __construct(DataReader $reader) { $this->reader = $reader; } public function doParse() { $xmlData = $this->reader->read(); //(...) } } interface DataReader { public function read(); } class XMLReaderWriter implements DataReader, DataWriter { // ... implement read() and write() }
  • 21. ● High-level modules should not depend on low-level modules. Both should depend on abstractions. ● Does it look familiar? SOLID D: Dependency Inversion Principle
  • 22. ● We have already used it! SOLID D: Dependency Inversion Principle class FeedParser { private $xmlReader; public function __construct(XmlReader $xmlReader) { $this->xmlReader = $xmlReader; } //----- class FeedParser { private $reader; public function __construct(DataReaderWriter $reader) { $this->reader = $reader; }
  • 23. ● Books: ○ Head First OO Analysis and Design (McLaughlin) ○ Head First Design Patterns (Freeman) ● Internet: ○ https://sites.google.com/site/unclebobconsultingllc/getting-a-solid-start ■ About SOLID: “They are not laws. They are not perfect truths.” SOLID References