SlideShare une entreprise Scribd logo
1  sur  26
Télécharger pour lire hors ligne
S.O.L.I.D. Principles of Object-Oriented
Programming(OOP)
Prepared by – Kumaresh Chandra Baruri
Software Engineer
What is S.O.L.I.D?
The 5 principles of SOLID are -
1. Single-responsibility principle.
2. Open-closed principle.
3. Liskov substitution principle.
4. Interface segregation principle.
5. Dependency inversion principle.
SOLID is a mnemonic device for 5 design principles of object-oriented
programs (OOP) that result in readable, adaptable, and scalable code.
Single Responsibility Principle
Example: Software development team consists of -
1. Front-end designers do design,
2. The tester does testing and
3. Backend developer takes care of backend development.
Here everyone has a single job or responsibility.
“A class should have only one reason to change” i.e. every class should have
a single responsibility or single job or single purpose.
Single Responsibility Principle
Motivations-
• Maintainability.
• Testability.
• Flexibility and Extensibility.
• Parallel Development.
• Loose Coupling.
How?
• Single File - Single Class.
• The class/function itself should only perform one task.
Single Responsibility Principle
Problematic Codes:
RegisterService violates SRP
as it does –
1. Register a user,
2. Connect to the database, and
3. Send an email.
Single Responsibility Principle
Solution Codes:
It needs to split the class into three specific
classes that each accomplish a single job –
1. RegisterService
2. UserRepository and
3. EmailService
Open Closed Principle
Example: Derive new class from existing class -
1. Developer A needs to release an update for a library or framework.
2. Developer B wants some modification or add some feature on that.
3. Then developer B is allowed to extend the existing class created by
developer A which is extension.
4. Developer B is not supposed to modify the class directly. This refers
to closed for modification.
“software entities (classes, modules, functions, etc.) should be open for
extension, but closed for modification” which means you should be able to
extend a class behavior, without modifying it.
Open Closed Principle
Motivations-
• Maintain stability of the existing codes.
• Reduce the cost of a business change requirement.
• Reduce testing of existing code.
• Write new functionality without altering existing codes.
• Loose coupling.
How?
• By Inheritance, Polymorphism and Abstraction, OCP can
be accomplished.
Open Closed Principle
Problematic Codes:
AreaCalculator does not follow OCP as
1. It handles only rectangle and circle.
2. No way to calculate for square or others.
Open Closed Principle
Solution Codes: Using abstraction -
Using an abstract class – Shape, any area can
be managed.
Liskov Substitution Principle
Motivations-
• If S is a subtype of T, then objects of type T should be replaced with
the objects of type S.
• No type casting is required so that codebase maintainability is
easier.
• Without LSP, every time we add or modify a subclass and
consequently to change multiple places. This is difficult and error-
prone.
“Derived or child classes must be substitutable for their base or parent
classes” which means any class that is the child of a parent class should
be usable in place of its parent without any unexpected behavior.
Liskov Substitution Principle
How?
• It needs to create generic base class.
• With Inheritance, Polymorphism and Abstraction, LSP can be
accomplished.
Liskov Substitution Principle
Problematic Codes
Orange class is breaking base class
behavior -
1. The output is - apple color is orange.
2. Child class changed the behavior of base class which is
opposite of LSP.
Liskov Substitution Principle
Solution Codes
Based on generic base class Fruit
1. Both orange and apple implement it.
2. Parent class’s behavior is not changed.
Interface Segregation Principle
Motivations-
• First, no class should be forced to implement any method(s) of an interface
they don’t use.
• Secondly, instead of creating large or one can say fat interfaces, create
multiple smaller interfaces with the aim that the clients should only think about
the methods that are of interest to them.
• There is less code carried between classes. Less code means fewer bugs.
“Do not force any client to implement an interface which is irrelevant to them” which
means – “Many client-specific interfaces are better than one general-purpose
interface.”
Interface Segregation Principle
How?
• Any unused part of the method should be removed or split into a
separate method into the interfaces. Client implements interface as per
their demand.
Problematic Codes which violates ISP
Interface Segregation Principle
Problematic Codes which violates ISP
This code violates ISP in the following ways –
1. Both the classes implemented same interface
IWorker abd inherited unnecessary methods.
2. FullTimeEmployee class does not need the
CalculateWorkedSalary() function.
3. ContractEmployeeclass does not need the
CalculateNetSalary().
Interface Segregation Principle
Solution:
In order to solve the problems above –
1. It needs to split the general interface IWorker into one base interface,
IBaseWorker, and two child interfaces IFullTimeWorkerSalary and
IContractWorkerSalary.
2. The general interface - IBaseWorker contains methods that all workers
share.
3. The child interfaces split up methods by worker type, FullTime with a
salary or Contract that gets paid hourly.
Interface Segregation Principle
Solution Codes
Interface Segregation Principle
Solution Codes
The end classes now only contain methods and properties that further their goal and thus achieve the ISP principle.
Dependency Inversion Principle
Motivations-
• High-level modules should not depend on low-level modules. Instead,
both should depend on abstractions (interfaces)
• Abstractions should not depend on details. Details (like concrete
implementations) should depend on abstractions.
• If dependencies are minimized, changes will be more localized and
require less work to find all affected components.
The rule states that -
One should depend upon abstractions, but not concretions.
Dependency Inversion Principle
How?
• Create an abstraction layer for the lower-level classes.
• DIP decouples high and low-level components and instead connects both
to abstractions.
Problematic Codes which violates DIP
Dependency Inversion Principle
Problems:
• When the store asks us to enable customers to add DVDs to their
shelves.
• In order to fulfil the demand, it needs to –
1. Create a new DVD class.
2. Modify the Shelf class so that it can accept DVDs.
3. This also breaks OCP.
Dependency Inversion Principle
Solution:
The solution is to create an abstraction layer for the lower-level classes
(Book and DVD).
Dependency Inversion Principle
Solution:
1. DVD/Book implements Product interface(abstraction).
2. Shelf can reference the Product interface instead of its
implementations (Book and DVD)
The refactored code also allows us to introduce new product types (for instance, Magazine) later on.
Solid principles

Contenu connexe

Tendances

Introduction to SOLID Principles
Introduction to SOLID PrinciplesIntroduction to SOLID Principles
Introduction to SOLID PrinciplesGanesh Samarthyam
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration TestingDavid Berliner
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven DevelopmentJohn Blum
 
Cross-project defect prediction
Cross-project defect predictionCross-project defect prediction
Cross-project defect predictionThomas Zimmermann
 
The SOLID Principles Illustrated by Design Patterns
The SOLID Principles Illustrated by Design PatternsThe SOLID Principles Illustrated by Design Patterns
The SOLID Principles Illustrated by Design PatternsHayim Makabee
 
Object Oriented Design SOLID Principles
Object Oriented Design SOLID PrinciplesObject Oriented Design SOLID Principles
Object Oriented Design SOLID Principlesrainynovember12
 
Solid design principles
Solid design principlesSolid design principles
Solid design principlesMahmoud Asadi
 
Test Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)David Ehringer
 
Formation Gratuite Total Tests par les experts Java Ippon
Formation Gratuite Total Tests par les experts Java Ippon Formation Gratuite Total Tests par les experts Java Ippon
Formation Gratuite Total Tests par les experts Java Ippon Ippon
 
TDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionTDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionDionatan default
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testingikhwanhayat
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development CodeOps Technologies LLP
 
Software Development Life Cycle Model
Software Development Life Cycle ModelSoftware Development Life Cycle Model
Software Development Life Cycle ModelJ.T.A.JONES
 
Single Responsibility Principle
Single Responsibility PrincipleSingle Responsibility Principle
Single Responsibility PrincipleEyal Golan
 
Modern UI Development With Node.js
Modern UI Development With Node.jsModern UI Development With Node.js
Modern UI Development With Node.jsRyan Anklam
 

Tendances (20)

Introduction to SOLID Principles
Introduction to SOLID PrinciplesIntroduction to SOLID Principles
Introduction to SOLID Principles
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration Testing
 
SOLID principles
SOLID principlesSOLID principles
SOLID principles
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Cross-project defect prediction
Cross-project defect predictionCross-project defect prediction
Cross-project defect prediction
 
The SOLID Principles Illustrated by Design Patterns
The SOLID Principles Illustrated by Design PatternsThe SOLID Principles Illustrated by Design Patterns
The SOLID Principles Illustrated by Design Patterns
 
V model in SDLC
V model in SDLCV model in SDLC
V model in SDLC
 
Object Oriented Design SOLID Principles
Object Oriented Design SOLID PrinciplesObject Oriented Design SOLID Principles
Object Oriented Design SOLID Principles
 
Solid design principles
Solid design principlesSolid design principles
Solid design principles
 
Test Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)
 
Automation Testing & TDD
Automation Testing & TDDAutomation Testing & TDD
Automation Testing & TDD
 
Formation Gratuite Total Tests par les experts Java Ippon
Formation Gratuite Total Tests par les experts Java Ippon Formation Gratuite Total Tests par les experts Java Ippon
Formation Gratuite Total Tests par les experts Java Ippon
 
TDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionTDD Flow: The Mantra in Action
TDD Flow: The Mantra in Action
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 
Software Development Life Cycle Model
Software Development Life Cycle ModelSoftware Development Life Cycle Model
Software Development Life Cycle Model
 
Single Responsibility Principle
Single Responsibility PrincipleSingle Responsibility Principle
Single Responsibility Principle
 
Code Quality
Code QualityCode Quality
Code Quality
 
Code Review
Code ReviewCode Review
Code Review
 
Modern UI Development With Node.js
Modern UI Development With Node.jsModern UI Development With Node.js
Modern UI Development With Node.js
 

Similaire à Solid principles

Improving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design PrinciplesImproving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design PrinciplesDr. Syed Hassan Amin
 
Software Design Principles (SOLID)
Software Design Principles (SOLID)Software Design Principles (SOLID)
Software Design Principles (SOLID)ASIMYILDIZ
 
DesignPrinciples-and-DesignPatterns
DesignPrinciples-and-DesignPatternsDesignPrinciples-and-DesignPatterns
DesignPrinciples-and-DesignPatternsBasavaraj Patil
 
SOLID Design Principles for Test Automaion
SOLID Design Principles for Test AutomaionSOLID Design Principles for Test Automaion
SOLID Design Principles for Test AutomaionKnoldus Inc.
 
Is your code SOLID enough?
 Is your code SOLID enough? Is your code SOLID enough?
Is your code SOLID enough?SARCCOM
 
Clean code: SOLID (iOS)
Clean code: SOLID (iOS)Clean code: SOLID (iOS)
Clean code: SOLID (iOS)Maksym Husar
 
Entity Framework: To the Unit of Work Design Pattern and Beyond
Entity Framework: To the Unit of Work Design Pattern and BeyondEntity Framework: To the Unit of Work Design Pattern and Beyond
Entity Framework: To the Unit of Work Design Pattern and BeyondSteve Westgarth
 
Clean Code - Part 2
Clean Code - Part 2Clean Code - Part 2
Clean Code - Part 2Knoldus Inc.
 
Apply SOLID Design principles to javascript code with immediate function invo...
Apply SOLID Design principles to javascript code with immediate function invo...Apply SOLID Design principles to javascript code with immediate function invo...
Apply SOLID Design principles to javascript code with immediate function invo...Anuradha Bandara
 
Birth of a developer
Birth of a developerBirth of a developer
Birth of a developerPiyush Rahate
 
SOLID principles-Present
SOLID principles-PresentSOLID principles-Present
SOLID principles-PresentQuang Nguyen
 

Similaire à Solid principles (20)

android principle.pptx
android principle.pptxandroid principle.pptx
android principle.pptx
 
Improving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design PrinciplesImproving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design Principles
 
Software Design Principles (SOLID)
Software Design Principles (SOLID)Software Design Principles (SOLID)
Software Design Principles (SOLID)
 
DesignPrinciples-and-DesignPatterns
DesignPrinciples-and-DesignPatternsDesignPrinciples-and-DesignPatterns
DesignPrinciples-and-DesignPatterns
 
Soild principles
Soild principlesSoild principles
Soild principles
 
OO Design Principles
OO Design PrinciplesOO Design Principles
OO Design Principles
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
 
Solid
SolidSolid
Solid
 
SOLID Design Principles for Test Automaion
SOLID Design Principles for Test AutomaionSOLID Design Principles for Test Automaion
SOLID Design Principles for Test Automaion
 
Is your code SOLID enough?
 Is your code SOLID enough? Is your code SOLID enough?
Is your code SOLID enough?
 
Clean code: SOLID (iOS)
Clean code: SOLID (iOS)Clean code: SOLID (iOS)
Clean code: SOLID (iOS)
 
Clean code: SOLID
Clean code: SOLIDClean code: SOLID
Clean code: SOLID
 
Entity Framework: To the Unit of Work Design Pattern and Beyond
Entity Framework: To the Unit of Work Design Pattern and BeyondEntity Framework: To the Unit of Work Design Pattern and Beyond
Entity Framework: To the Unit of Work Design Pattern and Beyond
 
Software development fundamentals
Software development fundamentalsSoftware development fundamentals
Software development fundamentals
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
Clean Code - Part 2
Clean Code - Part 2Clean Code - Part 2
Clean Code - Part 2
 
Android architecture
Android architectureAndroid architecture
Android architecture
 
Apply SOLID Design principles to javascript code with immediate function invo...
Apply SOLID Design principles to javascript code with immediate function invo...Apply SOLID Design principles to javascript code with immediate function invo...
Apply SOLID Design principles to javascript code with immediate function invo...
 
Birth of a developer
Birth of a developerBirth of a developer
Birth of a developer
 
SOLID principles-Present
SOLID principles-PresentSOLID principles-Present
SOLID principles-Present
 

Plus de Kumaresh Chandra Baruri

Plus de Kumaresh Chandra Baruri (8)

Introduction to OAuth2
Introduction to OAuth2Introduction to OAuth2
Introduction to OAuth2
 
Introduction to git, a version control system
Introduction to git, a version control systemIntroduction to git, a version control system
Introduction to git, a version control system
 
Fundamentasl of DFD
Fundamentasl of DFDFundamentasl of DFD
Fundamentasl of DFD
 
Variations of git merging
Variations of git mergingVariations of git merging
Variations of git merging
 
Git branching policy and review comment's prefix
Git branching policy and review comment's prefixGit branching policy and review comment's prefix
Git branching policy and review comment's prefix
 
Introduction to flutter's basic concepts
Introduction to flutter's basic conceptsIntroduction to flutter's basic concepts
Introduction to flutter's basic concepts
 
WEB API Gateway
WEB API GatewayWEB API Gateway
WEB API Gateway
 
Authentication and single sign on (sso)
Authentication and single sign on (sso)Authentication and single sign on (sso)
Authentication and single sign on (sso)
 

Dernier

Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesKrzysztofKkol1
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptxVinzoCenzo
 
SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?Alexandre Beguel
 
SoftTeco - Software Development Company Profile
SoftTeco - Software Development Company ProfileSoftTeco - Software Development Company Profile
SoftTeco - Software Development Company Profileakrivarotava
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slidesvaideheekore1
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingShane Coughlan
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identityteam-WIBU
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonApplitools
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingOpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingShane Coughlan
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfkalichargn70th171
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...Bert Jan Schrijver
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 

Dernier (20)

Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptx
 
SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?
 
SoftTeco - Software Development Company Profile
SoftTeco - Software Development Company ProfileSoftTeco - Software Development Company Profile
SoftTeco - Software Development Company Profile
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slides
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identity
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingOpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 

Solid principles

  • 1. S.O.L.I.D. Principles of Object-Oriented Programming(OOP) Prepared by – Kumaresh Chandra Baruri Software Engineer
  • 2. What is S.O.L.I.D? The 5 principles of SOLID are - 1. Single-responsibility principle. 2. Open-closed principle. 3. Liskov substitution principle. 4. Interface segregation principle. 5. Dependency inversion principle. SOLID is a mnemonic device for 5 design principles of object-oriented programs (OOP) that result in readable, adaptable, and scalable code.
  • 3. Single Responsibility Principle Example: Software development team consists of - 1. Front-end designers do design, 2. The tester does testing and 3. Backend developer takes care of backend development. Here everyone has a single job or responsibility. “A class should have only one reason to change” i.e. every class should have a single responsibility or single job or single purpose.
  • 4. Single Responsibility Principle Motivations- • Maintainability. • Testability. • Flexibility and Extensibility. • Parallel Development. • Loose Coupling. How? • Single File - Single Class. • The class/function itself should only perform one task.
  • 5. Single Responsibility Principle Problematic Codes: RegisterService violates SRP as it does – 1. Register a user, 2. Connect to the database, and 3. Send an email.
  • 6. Single Responsibility Principle Solution Codes: It needs to split the class into three specific classes that each accomplish a single job – 1. RegisterService 2. UserRepository and 3. EmailService
  • 7. Open Closed Principle Example: Derive new class from existing class - 1. Developer A needs to release an update for a library or framework. 2. Developer B wants some modification or add some feature on that. 3. Then developer B is allowed to extend the existing class created by developer A which is extension. 4. Developer B is not supposed to modify the class directly. This refers to closed for modification. “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification” which means you should be able to extend a class behavior, without modifying it.
  • 8. Open Closed Principle Motivations- • Maintain stability of the existing codes. • Reduce the cost of a business change requirement. • Reduce testing of existing code. • Write new functionality without altering existing codes. • Loose coupling. How? • By Inheritance, Polymorphism and Abstraction, OCP can be accomplished.
  • 9. Open Closed Principle Problematic Codes: AreaCalculator does not follow OCP as 1. It handles only rectangle and circle. 2. No way to calculate for square or others.
  • 10. Open Closed Principle Solution Codes: Using abstraction - Using an abstract class – Shape, any area can be managed.
  • 11. Liskov Substitution Principle Motivations- • If S is a subtype of T, then objects of type T should be replaced with the objects of type S. • No type casting is required so that codebase maintainability is easier. • Without LSP, every time we add or modify a subclass and consequently to change multiple places. This is difficult and error- prone. “Derived or child classes must be substitutable for their base or parent classes” which means any class that is the child of a parent class should be usable in place of its parent without any unexpected behavior.
  • 12. Liskov Substitution Principle How? • It needs to create generic base class. • With Inheritance, Polymorphism and Abstraction, LSP can be accomplished.
  • 13. Liskov Substitution Principle Problematic Codes Orange class is breaking base class behavior - 1. The output is - apple color is orange. 2. Child class changed the behavior of base class which is opposite of LSP.
  • 14. Liskov Substitution Principle Solution Codes Based on generic base class Fruit 1. Both orange and apple implement it. 2. Parent class’s behavior is not changed.
  • 15. Interface Segregation Principle Motivations- • First, no class should be forced to implement any method(s) of an interface they don’t use. • Secondly, instead of creating large or one can say fat interfaces, create multiple smaller interfaces with the aim that the clients should only think about the methods that are of interest to them. • There is less code carried between classes. Less code means fewer bugs. “Do not force any client to implement an interface which is irrelevant to them” which means – “Many client-specific interfaces are better than one general-purpose interface.”
  • 16. Interface Segregation Principle How? • Any unused part of the method should be removed or split into a separate method into the interfaces. Client implements interface as per their demand. Problematic Codes which violates ISP
  • 17. Interface Segregation Principle Problematic Codes which violates ISP This code violates ISP in the following ways – 1. Both the classes implemented same interface IWorker abd inherited unnecessary methods. 2. FullTimeEmployee class does not need the CalculateWorkedSalary() function. 3. ContractEmployeeclass does not need the CalculateNetSalary().
  • 18. Interface Segregation Principle Solution: In order to solve the problems above – 1. It needs to split the general interface IWorker into one base interface, IBaseWorker, and two child interfaces IFullTimeWorkerSalary and IContractWorkerSalary. 2. The general interface - IBaseWorker contains methods that all workers share. 3. The child interfaces split up methods by worker type, FullTime with a salary or Contract that gets paid hourly.
  • 20. Interface Segregation Principle Solution Codes The end classes now only contain methods and properties that further their goal and thus achieve the ISP principle.
  • 21. Dependency Inversion Principle Motivations- • High-level modules should not depend on low-level modules. Instead, both should depend on abstractions (interfaces) • Abstractions should not depend on details. Details (like concrete implementations) should depend on abstractions. • If dependencies are minimized, changes will be more localized and require less work to find all affected components. The rule states that - One should depend upon abstractions, but not concretions.
  • 22. Dependency Inversion Principle How? • Create an abstraction layer for the lower-level classes. • DIP decouples high and low-level components and instead connects both to abstractions. Problematic Codes which violates DIP
  • 23. Dependency Inversion Principle Problems: • When the store asks us to enable customers to add DVDs to their shelves. • In order to fulfil the demand, it needs to – 1. Create a new DVD class. 2. Modify the Shelf class so that it can accept DVDs. 3. This also breaks OCP.
  • 24. Dependency Inversion Principle Solution: The solution is to create an abstraction layer for the lower-level classes (Book and DVD).
  • 25. Dependency Inversion Principle Solution: 1. DVD/Book implements Product interface(abstraction). 2. Shelf can reference the Product interface instead of its implementations (Book and DVD) The refactored code also allows us to introduce new product types (for instance, Magazine) later on.