SlideShare a Scribd company logo
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

oojs
oojsoojs
Better Drupal 8 Batch Services
Better Drupal 8 Batch ServicesBetter Drupal 8 Batch Services
Better Drupal 8 Batch Services
Aaron Crosman
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
baabtra.com - No. 1 supplier of quality freshers
 
Hipster Oriented Programming
Hipster Oriented ProgrammingHipster Oriented Programming
Hipster Oriented Programming
Jens Ravens
 
An introduction to Object Oriented JavaScript
An introduction to Object Oriented JavaScriptAn introduction to Object Oriented JavaScript
An introduction to Object Oriented JavaScript
TO THE NEW | Technology
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
Teja Swaroop
 
Kelompok 8 Pbw
Kelompok 8 PbwKelompok 8 Pbw
Kelompok 8 Pbw
guest390cde
 
Kelompok 8 Pbw
Kelompok 8 PbwKelompok 8 Pbw
Kelompok 8 Pbw
guestdf5a09
 
Introduction to RxJava on Android
Introduction to RxJava on AndroidIntroduction to RxJava on Android
Introduction to RxJava on Android
Chris Arriola
 
JavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat SheetJavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat Sheet
HDR1001
 
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
ESUG
 
Ejb3.1
Ejb3.1Ejb3.1
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
Piyush Katariya
 
JavaScript Beyond jQuery
JavaScript Beyond jQueryJavaScript Beyond jQuery
JavaScript Beyond jQuery
Bobby Bryant
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
Bhanuka Uyanage
 
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
Piyush Jamwal
 
this is simple
this is simplethis is simple
this is simple
Nir 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 3
Chris Farrell
 
Introduction to Object Oriented Javascript
Introduction to Object Oriented JavascriptIntroduction to Object Oriented Javascript
Introduction to Object Oriented Javascript
nodeninjas
 

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 - Warburton
Codemotion
 
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
RichardWarburton
 
Effective PHP. Part 1
Effective PHP. Part 1Effective PHP. Part 1
Effective PHP. Part 1
Vasily Kartashov
 
Twins: OOP and FP
Twins: OOP and FPTwins: OOP and FP
Twins: OOP and FP
RichardWarburton
 
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
ICS
 
Design Patterns and Usage
Design Patterns and UsageDesign Patterns and Usage
Design Patterns and Usage
Mindfire Solutions
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
Zohar Arad
 
Javascript Best Practices
Javascript Best PracticesJavascript Best Practices
Javascript Best Practices
Christian Heilmann
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
James Casey
 
Software design principles SOLID
Software design principles SOLIDSoftware design principles SOLID
Software design principles SOLID
Foyzul 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
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
Omnia Helmi
 
Legacy is Good
Legacy is GoodLegacy is Good
Legacy is Good
Uberto Barbini
 
Clean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionClean Code: Chapter 3 Function
Clean Code: Chapter 3 Function
Kent Huang
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
Subramanyan Murali
 
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
 
Design patterns in PHP
Design patterns in PHPDesign patterns in PHP
Design patterns in PHP
Jason Straughan
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
Anjan 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
 
SOLID Java Code
SOLID Java CodeSOLID Java Code
SOLID Java Code
Omar Bashir
 

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

Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
Hiike
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
Miro Wengner
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
Shinana2
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Tatiana Kojar
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
AstuteBusiness
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
Antonios Katsarakis
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
Alex Pruden
 
SAP S/4 HANA sourcing and procurement to Public cloud
SAP S/4 HANA sourcing and procurement to Public cloudSAP S/4 HANA sourcing and procurement to Public cloud
SAP S/4 HANA sourcing and procurement to Public cloud
maazsz111
 

Recently uploaded (20)

Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
 
SAP S/4 HANA sourcing and procurement to Public cloud
SAP S/4 HANA sourcing and procurement to Public cloudSAP S/4 HANA sourcing and procurement to Public cloud
SAP S/4 HANA sourcing and procurement to Public cloud
 

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” ; }