Successfully reported this slideshow.

# Some OOP paradigms & SOLID

Upcoming SlideShare
Introduction to Clean Code
×

# Some OOP paradigms & SOLID

Talk given to university students during the "Seminari UPC 2017"

Talk given to university students during the "Seminari UPC 2017"

## More Related Content

### Some OOP paradigms & SOLID

2. 2. ● Basic principles ○ Favor Composition over Inheritance ○ Law of Demeter ○ Dependency Injection ● S.O.L.I.D. principles SOLID OOP paradigms
3. 3. SOLID Favor Composition over Inheritance
4. 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. 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. 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. 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. 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 ...)
10. 10. ● Improves decoupling ● Avoid hardcoding dependencies ● [Hint] Look for: ○ new Class() ○ build-in functions ■ Ex. date(), fopen() SOLID Dependency Injection
11. 11. ● Mnemonic acronym (by Feathers) ● “first five principles” of OOP design (by Uncle Bob) SOLID SOLID
12. 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. 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. 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. 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(); //(...)