SlideShare a Scribd company logo
1 of 23
Download to read offline
Understanding Solid
Principles
About Me
● Babatunde Otaru
● Software Engineer @ Cotta & Cush
● Tourist
● Liverpool Fan
● Twitter: @mendizel
Case study
Class Book {
Function getTitle() ;
Function getAuthor() ;
Function turnPage() ;
Function printCurrentPage (String printerType) :
Switch (printerType) :
Case “plainTextPrinter” :
return “plain text printout” ;
Case “htmlPrinter” :
return “html printout" ;
Function savePage(String persistenceType) ;
Switch (persistenceType) :
Case “flatFile” :
return “save page in flat file” ;
Case “mysql” :
return “save page in mysql DB” ;
}
New Feature
Can you help print the page in xml format?
Wrong! why?
Class Book {
Function getTitle() ;
Function getAuthor() ;
Function turnPage() ;
Function printCurrentPage (String printerType) :
Switch (printerType) :
Case “plainTextPrinter” :
return “plain text print out” ;
Case “htmlPrinter” :
return “html print out" ;
Case “xmlPrinter” :
Return “xml print out” ;
Function savePage (String persistenceType) :
Switch (persistenceType) :
Case “flatFile” :
return “save in flat file” ;
Case “mysql” :
return “save in mysql DB” ;
}
Why these solid principles?
● System Maintainability
● Changes always happen
● Reduce time to add feature/fix bug
● Easier for other engineers or ‘a future you’ to quickly understand the
codebase
Solid Principles
S - single responsibility principle
O - open/closed principle
L - liskov substitution principle
I - interface segregation principle
D - dependency inversion principle
Solid Principles: Single Responsibility Principle
● Your class should have just one reason to change
● Look for actors in your system
● An actor should have a single job description
● Don’t be confused with a single person acting multiple roles
● Create a class/module for each actor
Solid Principles: Single Responsibility Principle
● Three actors
○ Book Reader
○ Printer Man
○ Records Man
Solid Principles: Single Responsibility Principle
Class Book {
Function getTitle() ;
Function getAuthor() ;
Function turnPage() ;
Function getCurrentPage() ;
Function getNextPage() ;
}
Class Database {
Function saveBook(String databaseType) ;
Switch (databaseType) :
Case “flatFile” :
return “save in flat file” ;
Case “mysql” :
return “save in mysql DB” ;
}
Class Printer {
Function printBookPage (String printerType) :
Switch (printerType) :
Case “plainTextPrinter” :
return “plain text printout” ;
Case “htmlPrinter” :
return “html printout" ;
}
Solid Principles: Open/Closed Principle
● Classes should be open to extension but closed to modifications
● Do not modify classes to add a type of actor in your system
Solid Principles: Open/Closed Principle
Printer Class Case Study:
Class Printer {
Function printBookPage (Printer printer) :
printer.print() ;
}
Interface PrinterInterface {
print() ;
}
Class HtmlPrinter implements PrinterInterface {
Function print() :
return “html print out” ;
}
Class PlainTextPrinter implements PrinterInterface {
Function print() :
return “plain text print out” ;
}
New Feature
Can you help print the page in xml format?
Solid Principles: Liskov Substitution Principle
● Subclasses can substitute parent classes without breaking functionality
(or changing behaviour)
○ New exceptions shouldn’t be thrown in the derived classes
○ Pre conditions cannot be strengthened in the derived classes : integer only
negative example
○ Post conditions cannot be weakened : DB open/close example
Solid Principles: Liskov Substitution Principle
Class Printer {
Function printBookPage (PrinterInterface printer) :
printer.print() ;
}
Interface PrinterInterface {
print() ;
}
Class HtmlPrinter implements PrinterInterface {
Function print() :
return “html print out” ;
}
Class BrowserPrinter extends HtmlPrinter {
Function print() :
If (internet explorer) {
Throw IHateInternetExplorerException();
}
return “print browser page” ;
}
Solid Principles: Interface Segregation Principle
● Interface should represent one discrete cohesive behaviour
● All methods in an interface should be implemented
Solid Principles: Interface Segregation Principle
Interface PrinterInterface {
print() ;
removeInk();
}
Class POSPrinter implements PrinterInterface {
Function print() :
return “POS print out” ;
Function removeInk()
Throw new doesNotHaveInkException;
}
Interface InkableInterface() {
Function removeInk();
}
Solid Principles: Dependency Inversion Principle
● A high level module should not depend on a low level module, both should
depend on abstractions
● Abstractions should never depend upon details, details should depend upon
abstractions
Solid Principles: Dependency Inversion Principle
Class Library {
HtmlPrinter printer = new HtmlPrinter;
Books[] book;
Function printBook (Book) :
Foreach (bookPage) {
This.printer.print(currentPage) ;
}
}
}
Class Library {
PrinterInterface printer;
Books[] book;
Init (PrinterInterface printer) {
This.printer = printer;
}
Function printBook (Book) :
Foreach (bookPage) {
This.printer.print(currentPage) ;
}
}
}
Final Piece!
Class Book {
Function getTitle() ;
Function getAuthor() ;
Function turnPage() ;
Function getCurrentPage() ;
Function getNextPage() ;
}
Class Printer {
Function printBookPage (PrinterInteface printer) :
printer.print() ;
}
Interface PrinterInterface {
print() ;
}
Class HtmlPrinter implements PrinterInterface {
Function print() :
return “html print out” ;
}
Class PlainTextPrinter implements PrinterInterface {
Function print() :
return “plain text print out” ;
}
Class Store {
Function savePage (StoreInterface saver) :
saver.save() ;
}
Interface StoreInterface {
save() ;
}
Class MysqlStore implements StoreInterface {
Function save() :
return “Save Item in Mysql DB” ;
}
Class FlatFileStore implements StoreInterface {
Function save() :
return “Save Item in Flat File” ;
}
Questions….
Kahoot!https://play.kahoot.it/#/k/309f004e-e52e-4f14-bef6-8a2f471e3da8
Thank You

More Related Content

What's hot

Better Drupal 8 Batch Services
Better Drupal 8 Batch ServicesBetter Drupal 8 Batch Services
Better Drupal 8 Batch ServicesAaron Crosman
 
Hipster Oriented Programming
Hipster Oriented ProgrammingHipster Oriented Programming
Hipster Oriented ProgrammingJens Ravens
 
An introduction to Object Oriented JavaScript
An introduction to Object Oriented JavaScriptAn introduction to Object Oriented JavaScript
An introduction to Object Oriented JavaScriptTO THE NEW | Technology
 
Introduction to RxJava on Android
Introduction to RxJava on AndroidIntroduction to RxJava on Android
Introduction to RxJava on AndroidChris Arriola
 
JavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat SheetJavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat SheetHDR1001
 
A low Overhead Per Object Write Barrier for Smalltalk
A low Overhead Per Object Write Barrier for SmalltalkA low Overhead Per Object Write Barrier for Smalltalk
A low Overhead Per Object Write Barrier for SmalltalkESUG
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)Piyush Katariya
 
JavaScript Beyond jQuery
JavaScript Beyond jQueryJavaScript Beyond jQuery
JavaScript Beyond jQueryBobby Bryant
 
React js use contexts and useContext hook
React js use contexts and useContext hookReact js use contexts and useContext hook
React js use contexts and useContext hookPiyush Jamwal
 
this is simple
this is simplethis is simple
this is simpleNir Elbaz
 
Django mongodb -djangoday_
Django mongodb -djangoday_Django mongodb -djangoday_
Django mongodb -djangoday_WEBdeBS
 
JavaScript: Patterns, Part 3
JavaScript: Patterns, Part  3JavaScript: Patterns, Part  3
JavaScript: Patterns, Part 3Chris Farrell
 
Introduction to Object Oriented Javascript
Introduction to Object Oriented JavascriptIntroduction to Object Oriented Javascript
Introduction to Object Oriented Javascriptnodeninjas
 

What's hot (20)

oojs
oojsoojs
oojs
 
Better Drupal 8 Batch Services
Better Drupal 8 Batch ServicesBetter Drupal 8 Batch Services
Better Drupal 8 Batch Services
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Hipster Oriented Programming
Hipster Oriented ProgrammingHipster Oriented Programming
Hipster Oriented Programming
 
An introduction to Object Oriented JavaScript
An introduction to Object Oriented JavaScriptAn introduction to Object Oriented JavaScript
An introduction to Object Oriented JavaScript
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
 
Kelompok 8 Pbw
Kelompok 8 PbwKelompok 8 Pbw
Kelompok 8 Pbw
 
Kelompok 8 Pbw
Kelompok 8 PbwKelompok 8 Pbw
Kelompok 8 Pbw
 
Introduction to RxJava on Android
Introduction to RxJava on AndroidIntroduction to RxJava on Android
Introduction to RxJava on Android
 
JavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat SheetJavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat Sheet
 
A low Overhead Per Object Write Barrier for Smalltalk
A low Overhead Per Object Write Barrier for SmalltalkA low Overhead Per Object Write Barrier for Smalltalk
A low Overhead Per Object Write Barrier for Smalltalk
 
Ejb3.1
Ejb3.1Ejb3.1
Ejb3.1
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
 
JavaScript Beyond jQuery
JavaScript Beyond jQueryJavaScript Beyond jQuery
JavaScript Beyond jQuery
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
React js use contexts and useContext hook
React js use contexts and useContext hookReact js use contexts and useContext hook
React js use contexts and useContext hook
 
this is simple
this is simplethis is simple
this is simple
 
Django mongodb -djangoday_
Django mongodb -djangoday_Django mongodb -djangoday_
Django mongodb -djangoday_
 
JavaScript: Patterns, Part 3
JavaScript: Patterns, Part  3JavaScript: Patterns, Part  3
JavaScript: Patterns, Part 3
 
Introduction to Object Oriented Javascript
Introduction to Object Oriented JavascriptIntroduction to Object Oriented Javascript
Introduction to Object Oriented Javascript
 

Similar to Understanding solid principles

TWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonTWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonCodemotion
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingRichardWarburton
 
Best Practices in Qt Quick/QML - Part IV
Best Practices in Qt Quick/QML - Part IVBest Practices in Qt Quick/QML - Part IV
Best Practices in Qt Quick/QML - Part IVICS
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design PatternsZohar Arad
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to DjangoJames Casey
 
Software design principles SOLID
Software design principles SOLIDSoftware design principles SOLID
Software design principles SOLIDFoyzul Karim
 
Three Simple Chords of Alternative PageObjects and Hardcore of LoadableCompon...
Three Simple Chords of Alternative PageObjects and Hardcore of LoadableCompon...Three Simple Chords of Alternative PageObjects and Hardcore of LoadableCompon...
Three Simple Chords of Alternative PageObjects and Hardcore of LoadableCompon...Iakiv Kramarenko
 
Clean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionClean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionKent Huang
 
From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)Jose Manuel Pereira Garcia
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to JavascriptAnjan Banda
 
Asynchronous single page applications without a line of HTML or Javascript, o...
Asynchronous single page applications without a line of HTML or Javascript, o...Asynchronous single page applications without a line of HTML or Javascript, o...
Asynchronous single page applications without a line of HTML or Javascript, o...Robert Schadek
 

Similar to Understanding solid principles (20)

TWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonTWINS: OOP and FP - Warburton
TWINS: OOP and FP - Warburton
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
 
Effective PHP. Part 1
Effective PHP. Part 1Effective PHP. Part 1
Effective PHP. Part 1
 
Twins: OOP and FP
Twins: OOP and FPTwins: OOP and FP
Twins: OOP and FP
 
Best Practices in Qt Quick/QML - Part IV
Best Practices in Qt Quick/QML - Part IVBest Practices in Qt Quick/QML - Part IV
Best Practices in Qt Quick/QML - Part IV
 
Design Patterns and Usage
Design Patterns and UsageDesign Patterns and Usage
Design Patterns and Usage
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
Javascript Best Practices
Javascript Best PracticesJavascript Best Practices
Javascript Best Practices
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Software design principles SOLID
Software design principles SOLIDSoftware design principles SOLID
Software design principles SOLID
 
Three Simple Chords of Alternative PageObjects and Hardcore of LoadableCompon...
Three Simple Chords of Alternative PageObjects and Hardcore of LoadableCompon...Three Simple Chords of Alternative PageObjects and Hardcore of LoadableCompon...
Three Simple Chords of Alternative PageObjects and Hardcore of LoadableCompon...
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
Legacy is Good
Legacy is GoodLegacy is Good
Legacy is Good
 
Clean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionClean Code: Chapter 3 Function
Clean Code: Chapter 3 Function
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)
 
Design patterns in PHP
Design patterns in PHPDesign patterns in PHP
Design patterns in PHP
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
Asynchronous single page applications without a line of HTML or Javascript, o...
Asynchronous single page applications without a line of HTML or Javascript, o...Asynchronous single page applications without a line of HTML or Javascript, o...
Asynchronous single page applications without a line of HTML or Javascript, o...
 
SOLID Java Code
SOLID Java CodeSOLID Java Code
SOLID Java Code
 

Recently uploaded

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
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

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
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 

Understanding solid principles

  • 2. About Me ● Babatunde Otaru ● Software Engineer @ Cotta & Cush ● Tourist ● Liverpool Fan ● Twitter: @mendizel
  • 3. Case study Class Book { Function getTitle() ; Function getAuthor() ; Function turnPage() ; Function printCurrentPage (String printerType) : Switch (printerType) : Case “plainTextPrinter” : return “plain text printout” ; Case “htmlPrinter” : return “html printout" ; Function savePage(String persistenceType) ; Switch (persistenceType) : Case “flatFile” : return “save page in flat file” ; Case “mysql” : return “save page in mysql DB” ; }
  • 4. New Feature Can you help print the page in xml format?
  • 5. Wrong! why? Class Book { Function getTitle() ; Function getAuthor() ; Function turnPage() ; Function printCurrentPage (String printerType) : Switch (printerType) : Case “plainTextPrinter” : return “plain text print out” ; Case “htmlPrinter” : return “html print out" ; Case “xmlPrinter” : Return “xml print out” ; Function savePage (String persistenceType) : Switch (persistenceType) : Case “flatFile” : return “save in flat file” ; Case “mysql” : return “save in mysql DB” ; }
  • 6. Why these solid principles? ● System Maintainability ● Changes always happen ● Reduce time to add feature/fix bug ● Easier for other engineers or ‘a future you’ to quickly understand the codebase
  • 7. Solid Principles S - single responsibility principle O - open/closed principle L - liskov substitution principle I - interface segregation principle D - dependency inversion principle
  • 8. Solid Principles: Single Responsibility Principle ● Your class should have just one reason to change ● Look for actors in your system ● An actor should have a single job description ● Don’t be confused with a single person acting multiple roles ● Create a class/module for each actor
  • 9. Solid Principles: Single Responsibility Principle ● Three actors ○ Book Reader ○ Printer Man ○ Records Man
  • 10. Solid Principles: Single Responsibility Principle Class Book { Function getTitle() ; Function getAuthor() ; Function turnPage() ; Function getCurrentPage() ; Function getNextPage() ; } Class Database { Function saveBook(String databaseType) ; Switch (databaseType) : Case “flatFile” : return “save in flat file” ; Case “mysql” : return “save in mysql DB” ; } Class Printer { Function printBookPage (String printerType) : Switch (printerType) : Case “plainTextPrinter” : return “plain text printout” ; Case “htmlPrinter” : return “html printout" ; }
  • 11. Solid Principles: Open/Closed Principle ● Classes should be open to extension but closed to modifications ● Do not modify classes to add a type of actor in your system
  • 12. Solid Principles: Open/Closed Principle Printer Class Case Study: Class Printer { Function printBookPage (Printer printer) : printer.print() ; } Interface PrinterInterface { print() ; } Class HtmlPrinter implements PrinterInterface { Function print() : return “html print out” ; } Class PlainTextPrinter implements PrinterInterface { Function print() : return “plain text print out” ; }
  • 13. New Feature Can you help print the page in xml format?
  • 14. Solid Principles: Liskov Substitution Principle ● Subclasses can substitute parent classes without breaking functionality (or changing behaviour) ○ New exceptions shouldn’t be thrown in the derived classes ○ Pre conditions cannot be strengthened in the derived classes : integer only negative example ○ Post conditions cannot be weakened : DB open/close example
  • 15. Solid Principles: Liskov Substitution Principle Class Printer { Function printBookPage (PrinterInterface printer) : printer.print() ; } Interface PrinterInterface { print() ; } Class HtmlPrinter implements PrinterInterface { Function print() : return “html print out” ; } Class BrowserPrinter extends HtmlPrinter { Function print() : If (internet explorer) { Throw IHateInternetExplorerException(); } return “print browser page” ; }
  • 16. Solid Principles: Interface Segregation Principle ● Interface should represent one discrete cohesive behaviour ● All methods in an interface should be implemented
  • 17. Solid Principles: Interface Segregation Principle Interface PrinterInterface { print() ; removeInk(); } Class POSPrinter implements PrinterInterface { Function print() : return “POS print out” ; Function removeInk() Throw new doesNotHaveInkException; } Interface InkableInterface() { Function removeInk(); }
  • 18. Solid Principles: Dependency Inversion Principle ● A high level module should not depend on a low level module, both should depend on abstractions ● Abstractions should never depend upon details, details should depend upon abstractions
  • 19. Solid Principles: Dependency Inversion Principle Class Library { HtmlPrinter printer = new HtmlPrinter; Books[] book; Function printBook (Book) : Foreach (bookPage) { This.printer.print(currentPage) ; } } } Class Library { PrinterInterface printer; Books[] book; Init (PrinterInterface printer) { This.printer = printer; } Function printBook (Book) : Foreach (bookPage) { This.printer.print(currentPage) ; } } }
  • 20. Final Piece! Class Book { Function getTitle() ; Function getAuthor() ; Function turnPage() ; Function getCurrentPage() ; Function getNextPage() ; } Class Printer { Function printBookPage (PrinterInteface printer) : printer.print() ; } Interface PrinterInterface { print() ; } Class HtmlPrinter implements PrinterInterface { Function print() : return “html print out” ; } Class PlainTextPrinter implements PrinterInterface { Function print() : return “plain text print out” ; } Class Store { Function savePage (StoreInterface saver) : saver.save() ; } Interface StoreInterface { save() ; } Class MysqlStore implements StoreInterface { Function save() : return “Save Item in Mysql DB” ; } Class FlatFileStore implements StoreInterface { Function save() : return “Save Item in Flat File” ; }