SOLID Principles Part 1
-
Upload
maulik-soni -
Category
Software
-
view
195 -
download
0
Transcript of SOLID Principles Part 1
![Page 1: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/1.jpg)
IntroductionA Little background
Maulik Soni
![Page 2: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/2.jpg)
Few Terminologies
Basic OOP Cohesion Coupling
Code Smells Design Smells Technical Debt
Maulik Soni
![Page 3: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/3.jpg)
Cohesion
Cohesion refers to what the class (or module) will do Try to achieve High Cohesion
Maulik Soni
![Page 4: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/4.jpg)
Coupling
How related are two classes / modules and how dependent they are on each other Try to achieve Loose Coupling
Maulik Soni
![Page 5: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/5.jpg)
Code Smells
Duplicate code Long method Large class Temporary field Switch statements Parallel inheritance hierarchies
Maulik Soni
![Page 6: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/6.jpg)
Design Smells
Rigidity Software is difficult to change
Fragility Program breaks in many places when a change mode in a single place
Immobility Parts could be useful in other systems, but effort and risk to separate from original system is
too great.
Maulik Soni
![Page 7: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/7.jpg)
Design Smells
Viscosity Design-preserving methods are more difficult to use than the hacks Development environment is slow and inefficient
Needless complexity Contains elements that aren’t currently useful
Needless repetition System has lots of repeated code elements
Opacity A module is difficult to understand
Maulik Soni
![Page 8: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/8.jpg)
Technical Debt
The cost to fix rotting code Interest charges build over time The longer we take to remove the smells, the more it will cost
Maulik Soni
![Page 9: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/9.jpg)
End of Introduction
So Now, Let’s begin…
Maulik Soni
![Page 10: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/10.jpg)
SOLID Principles - ISRP – Single Responsibility PrincipleO/CP – Open Closed Principle
Maulik Soni
![Page 11: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/11.jpg)
What are SOLID Principles ?
Single responsibility Open-closed Liskov substitution Interface segregation Dependency inversion
Maulik Soni
First five principles named by Robert C Martin in early 2000.
![Page 12: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/12.jpg)
Maulik Soni
Why SOLID ?
When Applied together it creates a system Easy to Maintain Easy to Extend over time
To remove the Code Smells Part of overall strategy of agile and adaptive programming
![Page 13: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/13.jpg)
Maulik Soni
Why Solid ? Another View
To Deliver Fast To manage changes easily To deal with complexity
![Page 14: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/14.jpg)
Maulik Soni
Single Responsibility Principle
A software module should have one and only one responsibility
![Page 15: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/15.jpg)
Maulik Soni
Example
![Page 16: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/16.jpg)
Maulik Soni
Is SRP Violated Here ?
![Page 17: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/17.jpg)
Maulik Soni
Is SRP Violated Here ?
![Page 18: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/18.jpg)
Maulik Soni
Real world SRP
![Page 19: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/19.jpg)
Maulik Soni
SRP - Summary
SRP is the simplest of the principles, and one of the hardest to get right We tend to join responsibilities together It's usually hard to see different responsibilities
![Page 20: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/20.jpg)
Maulik Soni
Open / Closed Principle
All systems change during their life cycles avoid a cascade of changes to dependent modules When requirements change, you extend the behavior, not changing old code
![Page 21: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/21.jpg)
Maulik Soni
Open / Closed Principle
Software Entities ( Classes, Modules, Functions Etc.) Should be open for extension but closed for modification
Open for extension Behavior of the module can be extended We are able to change what the module does
Closed for modification Extending behavior does not result in changes to source, binary, or code of the module
![Page 22: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/22.jpg)
Maulik Soni
Is O/CP Violated Here ?
![Page 23: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/23.jpg)
Maulik Soni
Solution
![Page 24: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/24.jpg)
Open / Closed Rule of Thumb
Use IF/SWITCH statements if number of cases are unlikely to change
Use strategy pattern when number of cases are likely to change
Don’t code for situations that you won’t ever need More smaller class files != more complicated code
Maulik Soni
![Page 25: SOLID Principles Part 1](https://reader036.fdocuments.us/reader036/viewer/2022062316/58f19e2d1a28abdc328b4579/html5/thumbnails/25.jpg)
Maulik Soni
End of Presentation