Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27
SOLID Principle
Introducing Software Architecture & Design
Zainul Karim - 23 Introduction
| SOLID Principle 2023
Introduction - S : Single Responsibility Principle (SRP) - O : Open-Closed Principle - L : LIskov Substitution Principle - I : Interface Segregation Principle - D : Dependency Injection Principle
| SOLID Principle 2023
Single Responsibility Principle
| SOLID Principle 2023
SRP Introduction - Every software component should have one and only one responsibility. (can be a class, a method, or a module)
❌ ✅
| SOLID Principle 2023
Cohesion & Coupling - Cohesion is the degree to which the various parts of a software component are related
| SOLID Principle 2023
Cohesion & Coupling - Coupling is defined as the level of interdependency between various software components.
| SOLID Principle 2023
Reason for Change - Every software component should have one and only one responsibility reason to change.
| SOLID Principle 2023
Open Closed Principle
| SOLID Principle 2023
Introduction - Software components should be closed for modification, but open for extension
| SOLID Principle 2023
Real World Analogies and Code Snippets
| SOLID Principle 2023
Key Takeaways from the Examples - Ease of adding new features - Leads to minimal cost of developing and testing software - OCP often requires decoupling which is follows SRP - SOLID is interdependent and intertwined each other - SOLID is more effective when combined
Caution : - Do not follow OCP blindly - Huge number of classes
| SOLID Principle 2023
Liskov Subtitution Principle
| SOLID Principle 2023
Introduction - Named from Barbara Liskov who explain this principle inside book she authored. - Object should be replaceable with their subtypes without affecting the correctness of the program - We will start from inheritance, which is basic feature of any OOP language. - Inheritance is also referred to as the “Is-A” relationship.
| SOLID Principle 2023
Inheritance
Car Bird Fuel
Hatchback Car Ostrich Pertamax Turbo
| SOLID Principle 2023
Orchid class did not match Liskov Principle because it change correctness of the Bird Class that could be fly.
So Liskov Principle is more
Bird than just “Is-A” relationship.
Ostrich
| SOLID Principle 2023
Breaking the Hierarchy
Car
Racing Car
| SOLID Principle 2023
Tell, Don’t Ask
| SOLID Principle 2023
Interface Segregation Principle
| SOLID Principle 2023
Introduction ● Specific Promises: Interfaces should only has method that each thing actually needs, no more. ● No Unwanted Stuff: Don't give method you won't use ● Less Confusion and Simple: Simple methods help you understand what each thing does without confusion. ● Easy Changes: When you want to change something, you only affect what's related
No Client should be forced to depend on methods it does not use
| SOLID Principle 2023
Restructuring the Code to Follow ISP
| SOLID Principle 2023
Restructuring the Code to Follow ISP
| SOLID Principle 2023
Technique to Identify Violation - Fat Interfaces - Interface with low cohesion - Empty method implementations
| SOLID Principle 2023
Dependency Inversion Principle
| SOLID Principle 2023
Introduction - High level modules should not depend on low level modules. Both should depend on abstractions. - Abstraction should not depend on details. Details should depend on attractions